Multiple root layouts and root not-found
?
#50034
Replies: 35 comments 33 replies
-
lol I am trying to solve this exact problem currently. +1 for this |
Beta Was this translation helpful? Give feedback.
-
In the root of your app with the not-found.tsx use global-error.tsx instead of layout.tsx. The global-error.tsx uses html and body tags just as layout.tsx. This should allow use of non-found.tsx in the root directory without a layout.tsx. |
Beta Was this translation helpful? Give feedback.
-
This is still an issue in Next.js 13.4.4. I hope they will find some better solution for this. |
Beta Was this translation helpful? Give feedback.
-
In addition to With this setup,
Edit: Can confirm the workaround to use a dynamic catch-all route also works with the internationalization example:
|
Beta Was this translation helpful? Give feedback.
-
I juste managed to get my not-found route working in a group layout using the trick mentioned here: |
Beta Was this translation helpful? Give feedback.
-
I confirm this does not work and created a Stackblitz Reproduction |
Beta Was this translation helpful? Give feedback.
-
Well, most of this functionality related to not-found.tsx modules is now broken in canary version 13.4.10. Only root not-found.tsx without CSS styles is rendered. I was on ver. 13.4.12 and reverted back to 13.4.9 until this issue is resolved. |
Beta Was this translation helpful? Give feedback.
-
I can confirm that not-found rendering inside route groups works again in v.13.4.16. |
Beta Was this translation helpful? Give feedback.
-
I'm on Version 13.4.16, still not working for me.. 😑 |
Beta Was this translation helpful? Give feedback.
-
@victorandree any updates? is it solved? |
Beta Was this translation helpful? Give feedback.
-
I think the best guys for this would be @icyJoseph . Could you please drop some advice? |
Beta Was this translation helpful? Give feedback.
-
Beta Was this translation helpful? Give feedback.
-
Tried to put |
Beta Was this translation helpful? Give feedback.
-
When using the solution with a catch-all-route for notFound. Parallel routing with only a layout file breaks and tries to render the catch-all-route. Workaround is to add page.tsx that returns null... There is a lot of workarounds with Nextjs.. hope this do not become a pattern... |
Beta Was this translation helpful? Give feedback.
-
I'm working with Next v14.0.2 and this still seems to be an issue. I've tried the catch-all technique with my internationlized app as shown below and it works as long as the URL takes into account the "lng". For example localhost/fr/abc routes to my not-found page ✅ If I put the catch-all route outside of [lng] folder, neither of them works. Is there another workaround that I'm missing to get this to work ? |
Beta Was this translation helpful? Give feedback.
-
This issue still exists, I am using v14.0.2. The |
Beta Was this translation helpful? Give feedback.
-
Same issue for me, unbelievable it is so hard to be able to show a custom 404 page if you have i18n in your app |
Beta Was this translation helpful? Give feedback.
-
SImilar issue for me, I can't add a custom layout in [...not-found]. |
Beta Was this translation helpful? Give feedback.
-
any updates on this? |
Beta Was this translation helpful? Give feedback.
-
Having this same issue myself on v14.2.4 |
Beta Was this translation helpful? Give feedback.
-
any updates ??? |
Beta Was this translation helpful? Give feedback.
-
i faced this issue for about one month. But finally fixed. This equally caused hydration errors in my NextJS app. here's what i did:
|
Beta Was this translation helpful? Give feedback.
-
I will not implement this now because none of the work-arounds seem reliable IMO, instead add this to the backlog. But to my future self:
|
Beta Was this translation helpful? Give feedback.
-
I am trying for months to find a good fix for this. The main problem for me is this [...not-found]/page.js, SEO wise all the invalid routes return a 200 because the not-found catch all route is a valid route, this causes Soft 404 errors on google search console. Removing the catch all route will result in only redirecting to not-found.js when calling notFound() which won't cover all the cases so it will default to the black next js 404 page. Did someone find a solution to use this workaround and also change the status code to 404 ? import { notFound } from 'next/navigation'; export const metadata = { export default function NotFoundDummy() { |
Beta Was this translation helpful? Give feedback.
-
The proposed workaround does not work if your application requires a catch-all at the root. These will conflict.
An optional catch all will also result in an error:
|
Beta Was this translation helpful? Give feedback.
-
bump, i've the same problem too. |
Beta Was this translation helpful? Give feedback.
-
bump, this still happens on next 15.0.2 💡 |
Beta Was this translation helpful? Give feedback.
-
Hi everyone, I'm working with Next.js 15.0.3 and wanted to share a potential solution to the issue regarding not-found.tsx and group-specific layouts. As I’m not a professional, please consider this a conceptual approach and use it with caution. Feedback is welcome! My App Structure: app Steps to Implement: 1. Create a Custom Catch-All Route for Each Group
2. Add not-found.tsx in Each Group
Why This Works: The notFound() function ensures that unmatched routes return a proper 404 HTTP status, which is essential for SEO. I hope this helps others! As mentioned earlier, please use this code with caution, and feel free to share any improvements or feedback. |
Beta Was this translation helpful? Give feedback.
-
@leerob / @vercel: It would be nice if you could put labels on the issues that are the most commented/viewed/popular. Maybe these already exist in that case disregard this post. If not then I am thinking it would be nice to see a label on each of these issues. For example you could add "In Progress", "Releasing in Q1 2025", "In Roadmap", or something similar... just so that we as devs know when to expect these features. |
Beta Was this translation helpful? Give feedback.
-
Note
As of May 2024, @victorandree (the author) hasn't been testing this issue or listed workarounds for newer versions of Next.js in a while. Please refer to the replies for up-to-date information.
Multiple root layouts are a highlighted feature of the "Route Groups" page in the Next.js documentation. On the other hand, a "root
app/not-found.js
" file is required to handle "global" 404 errors. This globalnot-found.js
requires a single root layout.How can we then support a global 404 page while using route groups to create multiple root layouts? I don't think we can.
Feature request: Unlike
app/global-error.js
, which handles "global errors", theapp/not-found.js
requires a root layout. IMO, there should be aapp/global-not-found.js
that works in a similar way. Or, to avoid another file convention, a way to handlenotFound
errors in a normal error handler.Workaround: There's 2 possible workarounds:
(app1)/[...not-found]/page.tsx
page that callsnotFound()
. This will trigger the the closestnot-found.tsx
to render. Thanks @ginifizz!pages/404.js
for now.App directory layouts
Basic setup where you have a public website and admin section, where the admin section uses a different root layout (maybe to skip on analytics, use an enterprise CSS framework):
Adding
app/not-found.tsx
to the root directory does not work. It'll crash with this message:Adding an
app/layout.tsx
will break things in strange ways.Adding
not-found.tsx
to the route groups (e.g.(admin)/not-found.tsx
) will simply not work. The pages aren't found.Workaround with dynamic catch-all route
Warning
This workaround is broken in 13.4.10 through 13.4.13. It works in newer versions of Next.js. The
(app1)/not-found.tsx
page will render without any root layout (i.e. with an "empty" root layout).You can add a dynamic catch-all route to catch all and in this route you can call
notFound
. This triggers the closestnot-found.tsx
.In
(app1)/[...not-found]/page.tsx
:(You want to actually call
notFound()
here and not just render a 404, in order for the 404 status to be set correctly).Beta Was this translation helpful? Give feedback.
All reactions