vercel / vercel/next.js

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

Open
#62,228 37 comments 28 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

bug Linking and Navigating
Dominant language
JavaScript
Stars
142k
Forks
32.4k
Avg merge
2d 14h
Merged PRs (30d)
351

Description

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

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the linked reproduction and its page.tsx example using notFound(), then compare the response from that route with a nonexistent route under next dev and next start. Done means the notFound() response body is server rendered in both stages and the beforeInteractive Script or next-runtime-env script in the head is handled as expected.

Written by the indexing model from the issue text.

Assessment

Tech stack
nextjs, react, typescript
Domain
frontend, web-dev
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Active
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.