next.js: 404 page isn't server rendered when using notFound()
Link to the code that reproduces this issue
https://github.com/nikcio/nextjs-notfound-example
To Reproduce
- Create a new project using
npx create-next-app@latest - Create a page that uses the
notFound()function. - Go to that page and see the response in dev tools - Here the Body is only scripts (Client rendered)
- 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
localhost:3000/my-page
localhost:3000/dynamic/test
localhost:3000/dynamic/my-page
All pages returns a 404 status code
Production build (next build && next start)
localhost:3000/test
localhost:3000/my-page
localhost:3000/dynamic/test
localhost:3000/dynamic/my-page
The static page
localhost:3000/my-pagereturns status code 200
The rendered result for all requests are the default not found page:
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
Captured in development where no 404 page is server rendered but it’s the same behavior when using
next start
About this issue
- Original URL
- State: open
- Created 3 years ago
- Reactions: 23
- Comments: 26 (5 by maintainers)
We’re also being bit by this.
In an attempt to migrate from pages to app directory, we were forced to move over our error pages as well, but since we’re localizing our app using
app/[locale]the default catch all 404 didn’t work. We’ve then triedapp/[locale]/[...catchall]/page.tsxthat returnsnotFound(), and then correctly hits ourapp/[locale]/not-found.tsxbut it’s not SSR, and causes a bunch of flashing of content when loaded.If we add a
loading.tsxall layouts render fine, but the error content is still only loaded client side, also causing flashing.I also have that case when calling notFound in RSC. It renders not-found.tsx correctly; however, it does not return a 404 code. It’s causing problems with SEO somehow!
The problem in my case is that it returns 200 even though rendering not-found.tsx
Anyway to solve that?
Im doing the same and same happends @vercel please fix this asap.
That is what happend
I have a not-found.tsx file
We’re building a website with strong requirements for progressive enhancement and ran into problems with this behavior.
We need to have all information and (most) interactive functionality available to users without Javascript enabled. But right now our 404 pages (triggered by
notFound()) are blank.Would love to see a more robust solution for server-side error handling. Especially when the rest of Next.js is pretty good for progressive enhancement.
I think I’ve crossed this one too, or something related.
I have an async function to generate the metadata, the page is a RSC async too.
Returning
notFoundon RSC gives 200 OK, but correctly renders thenot-found.tsx- however 200 OK prevents removing pages from Google Index through Google Search Console or it’s API.Returning
notFoundongenerateMetadatagives 404 Not Found as expected but renders an empty screen.Anyway to solve that?
This issue is reproductible on Nextjs website
Check this 404 for example: https://nextjs.org/docs/app/getting-started/page-not-exist
The page is not server side rendered
Note that this is not the case here https://nextjs.org/notexist
I had a similar problem caused by returning
notFound()withingenerateMetadata(). Seems like that is a very bad thing to do; would be nice if the docs were updated, or (ideally) a linting error warns you not to do this.upd pls
I also found that if we add a custom
not-found.jspage then it render the custom page but doesn’t return the 404 error…we are currently temporarly returning a metatag of no index on this not-found page but it would be nice to fix the issue asap 😃
In my case
The not-found page are not Server Side Render and it’s impossible to Translate in a simple way. Every day we need to found a Complicated Workaround to fix Next issues. It’s frustrating!
@huozhi any updates on this? Still the same issue on 14.2.25 (using app router), when throwing notFound() in the generateMetaData() it gets a 404 document, but as everyone here stated it wont render on the server. And if i remove the notFound() throw in generateMetaData() i get a 500 document error instead. This dosen’t seem to be correct behavior? Note that i do still throw the notFound() in the export Page function also. @felipedeboni did you manage to get a solution to this?
make sure you have
app/not-found.tsxwhich handlesnotFound(), non-existing routes and/404few solutions I came up while encountering this issue:
middleware.tsfile,return NextResponse.rewrite(new URL('/404', req.url))redirect('/404')app/layout.tsx, <body> child components are wrapped in<Suspense>– if yes, remove global Suspense@huozhi on the last version, it add the
no-indexso there is no need to add it manually. I reverted this change of mine as now unnecessaryHi @huozhi I’ve just tested this again against the
14.2.0-canary.46and it seems not all cases have been solved by your PR.The updated reproduction repo can be found here: https://github.com/nikcio/nextjs-notfound-example/tree/canary-46-test Branch:
canary-46-testResponses on routes
Development (
next dev)localhost:3000/test [NOW WORKS]
(Default page not found - The page doesn’t exist)
Response
localhost:3000/my-page [STILL DOESN’T SERVER RENDER]
(The page is set to return not found using the
notFound()function fromnext/navigation)Response
localhost:3000/dynamic/test [NOW WORKS]
(Default page not found under a dynamic layout - The page doesn’t exist)
Response
localhost:3000/dynamic/my-page [STILL DOESN’T SERVER RENDER]
(The page is set to return not found using the
notFound()function fromnext/navigation- The page is under a route with aforce-dynamiclayout)Response
Notes:
All pages now correctly return a 404 response.
Production build (
next build && next start)localhost:3000/test [STILL WORKS]
(Default page not found - The page doesn’t exist)
Response
localhost:3000/my-page [STILL DOESN’T SERVER RENDER]
(The page is set to return not found using the
notFound()function fromnext/navigation)Response
localhost:3000/dynamic/test [STILL WORKS]
(Default page not found under a dynamic layout - The page doesn’t exist)
Response
localhost:3000/dynamic/my-page [STILL DOESN’T SERVER RENDER]
(The page is set to return not found using the
notFound()function fromnext/navigation- The page is under a route with aforce-dynamiclayout)Response
Notes:
All pages now correctly return a 404 response.