next.js: Not-found broken with multiple root layouts
Link to the code that reproduces this issue
https://codesandbox.io/p/devbox/blissful-fog-nflrh6
To Reproduce
- Open sandbox and click
not found page - It will redirect to the generic nextjs 404 page
Current vs. Expected behavior
Generic 404 Nextjs error page is shown when I navigate to any unknow urls.
I expect to be redirected to my custom 404 UI handler
Verify canary release
- I verified that the issue exists in the latest Next.js canary release
Provide environment information
Operating System:
Platform: darwin
Arch: x64
Version: Darwin Kernel Version 22.6.0: Wed Jul 5 22:21:56 PDT 2023; root:xnu-8796.141.3~6/RELEASE_X86_64
Binaries:
Node: 20.9.0
npm: 10.1.0
Yarn: N/A
pnpm: 8.9.0
Relevant Packages:
next: 14.0.3
eslint-config-next: 13.5.6
react: 18.2.0
react-dom: 18.2.0
typescript: 5.2.2
Next.js Config:
output: N/A
Which area(s) are affected? (Select all that apply)
App Router, Routing (next/router, next/navigation, next/link)
Additional context
Good to know: In addition to catching expected notFound() errors, the root app/not-found.js file also handles any unmatched URLs for your whole application. This means users that visit a URL that is not handled by your app will be shown the UI exported by the app/not-found.js file.
https://nextjs.org/docs/app/api-reference/file-conventions/not-found
According to the docs in order to be able to handle any unmatched URLs in the application we are bound to the fixed path for the not-found UI that must be placed at the root of the app -> app/not-found.js
This breaks not-found custom UI handling as per provided example.
I’ve 2 root layout groups https://nextjs.org/docs/app/building-your-application/routing/route-groups#creating-multiple-root-layouts
- Auth group (which is publicly accessible)
- Main group (which is accessible if user is authenticated and basically contains the whole app routing)
With this setup and not having app root not-found file it’s not possible to be able to handle 404 with custom UI as any 404 url handling is bound to the root app/not-found.tsx.
I believe this should not be bound to the fixed path but rather the top most existing not-found file and if no file exist then generic 404 should intercept. This also applies for the error.tsx I believe.
About this issue
- Original URL
- State: open
- Created 3 years ago
- Reactions: 16
- Comments: 15 (3 by maintainers)
Commits related to this issue
- fix(next): global not-found not working on multi-root layouts (#63053) ## Why? For multi-root layouts (route groups on the root with their root layouts, no layout file on the root), it is not pos... — committed to vercel/next.js by devjiwonchoi 3 years ago
- [not-found] Add global-not-found convention (#78783) ### Feature Introducing a new convention `global-not-found.js`, which let you build your own 404 page within app router. This convention is sim... — committed to vercel/next.js by huozhi 2 years ago
I’ve created a devbox to recreate the issue.
With this file structure:
None of the not-found components are rendered. Only the generic next.js one is.
Here’s the devbox.
TLDR;
With the latest cannary (14.3.0-canary.6), which includes the fix, the following structure still doesn’t show the custom
not-foundcomponent in the main pages.I can confirm that it now lets you have a
not-foundin the root (/app), without a root layout (RootLayout) in the root (/app), which is great. However, for my use case (and any people using Payload 3.0), who want their websites to have a custom not-found - it still doesn’t solve the problem.What we need: the ability for each group route (including a group route, regardless if it’s a root (homepage), or a sub-page (/admin)) to have a custom not-found. Currently, even with your fix, if I want to have a custom not-found component for my homepage (the (app) group route from the screenshot in my previous comment), the only way I can do that is adding a
not-foundcomponent to the root/appfolder. This has a problem: I’m not able to use the root layout that’s within my(app)group route. I was really expecting that anot-foundwithin a group route (even though it’s for the root of the website) will just work, just like amanifest.tsornot-foundin other group routes that are for sub-routes works in that group route.Also, even with that fix, next.js doesn’t seem to care about the
not-foundcomponent in my(app)group route. Maybe because it’s for the root of the website, but inside a route group folder?What worked for me if I don’t want my root not-found page applying to missing pages in sub layouts, what I did was to create a fallback in each layouts this way
\app\unique-layout\[...fallback]\page.tsxMake sure to include the square brackets for fallback, else it won’t work
Please see https://github.com/vercel/next.js/issues/55191#issuecomment-1985794575
Seems this is same/similar issue. How come multiple root layouts can’t have it’s own 404/error handlers…
https://github.com/vercel/next.js/issues/54980