Skip to content

404 page isn't server rendered when using notFound() #62228

Description

@nikcio

Link to the code that reproduces this issue

https://github.com/nikcio/nextjs-notfound-example

To Reproduce

  1. Create a new project using npx create-next-app@latest
  2. Create a page that uses the notFound() function.
  3. Go to that page and see the response in dev tools - Here the Body is only scripts (Client rendered)
  4. Go to a different page that doesn't exist and see the response in dev tools - Here the body will be rendered (Server rendered)

Example for the page.tsx:

import { notFound } from "next/navigation";

export default function Page() {
  notFound();
}

Current vs. Expected behavior

Expected

I expected that the 404 page was server rendered both when using next dev and next start. But also most importantly server rendered when triggered using the notFound() function.

Current behavior

Examples from: https://github.com/nikcio/nextjs-notfound-example

Development (next dev)

localhost:3000/test
image

localhost:3000/my-page
image

localhost:3000/dynamic/test
image

localhost:3000/dynamic/my-page
image

All pages returns a 404 status code

Production build (next build && next start)

image

localhost:3000/test
image

localhost:3000/my-page
image

localhost:3000/dynamic/test
image

localhost:3000/dynamic/my-page
image

The static page localhost:3000/my-page returns status code 200
image

The rendered result for all requests are the default not found page:

image

Provide environment information

Operating System:
  Platform: win32
  Arch: x64
  Version: Windows 11 Enterprise
Binaries:
  Node: 20.9.0
  npm: N/A
  Yarn: N/A
  pnpm: 8.6.10
Relevant Packages:
  next: 14.1.0
  eslint-config-next: 14.1.0
  react: 18.2.0
  react-dom: 18.2.0
  typescript: 5.3.3
Next.js Config:
  output: N/A

Which area(s) are affected? (Select all that apply)

App Router, Routing (next/router, next/navigation, next/link)

Which stage(s) are affected? (Select all that apply)

next dev (local), next start (local)

Additional context

We encountered this as an issue when using next-runtime-env that adds a <script> tag to the <head> of the page. This is a problem because when the whole page is client rendered on the not found page the script tag is added after the page is loaded and the script will never be executed without an explicit call from another component.

You can replicate this using the Script component from Next and using strategy="beforeInteractive".

I've made an example here: https://github.com/nikcio/nextjs-notfound-example/tree/example/script-in-head

image

Captured in development where no 404 page is server rendered but it's the same behavior when using next start

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Linking and NavigatingRelated to Next.js linking (e.g., <Link>) and navigation.bugIssue was opened via the bug report template.

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions