Next.js Discord

Discord Forum

not-found.tsx with multiple root layouts

Answered
Blanc de Hotot posted this in #help-forum
Open in Discord
Blanc de HototOP
So I'm using Route Groups to have multiple route layouts, how can I handle a global not-found.tsx? Is it possible at all? The default 404 page seems to work, so it seems like it should be doable
Answered by Blanc de Hotot
View full answer

29 Replies

Blanc de HototOP
well yes, I did think of that...
Blanc de HototOP
Could I make a pages/404.tsx? Does that catch notFound()?
@Blanc de Hotot well yes, I did think of that...
Kawakawa
That means you don't have a root layout in your app dir.
Blanc de HototOP
Yes that is indeed my problem
I'm using multiple route layouts
Kawakawa
Doesn't mean you can't have a root layout
:shrug:
Kawakawa
I know what you mean.
You still have a root layout, though. Right?
Blanc de HototOP
To create multiple root layouts, remove the top-level layout.js file, and add a layout.js file inside each route groups.
I have a layout inside each route group
But not one inside app/
Kawakawa
You can still use a top-level layout.tsx file...
Blanc de HototOP
can I? it explicitly says to remove the top level layout.tsx to create multiple route layouts
Kawakawa
But it implicitly also implies that you can (and probably should) have a top level layout file.
Try it 😂
Blanc de HototOP
But I don't want a top level layout, I want to have separate Root layouts
It cannot possibly implicitly imply that the correct way to make multiple Root layouts is to not make either of them the Root layout
@Blanc de Hotot It cannot possibly implicitly imply that the correct way to make *multiple Root layouts* is to not make either of them the Root layout
Kawakawa
By them saying:
"If you use multiple root layouts without a top-level layout.js file, your home page.js file should be defined in one of the route groups, For example: app/(marketing)/page.js."

They are (down to interpretation) implying that you can do it.
But hey, here's another idea - try it and see!!!!!
Blanc de HototOP
Mate, I am perfectly aware that if I put a top level root layout, it will work. However, I don't want a top level root layout, I want multiple root layouts. Can you not see how if there is a single root layout, then the children of that root layout are no longer root layouts
Kawakawa
May I ask why you need it to be a "Root Layout", and not just have a layout at the root of your route group?
Blanc de HototOP
Because the properties on html are defined differently for each group
If its not possible to have a global not found without a root layout, that is understandable, however as of right now, this is not explicitly stated in the documentation, and it also seems to be quite a big fallacy to support multiple root layouts when doing so means not supporting a custom 404 page
Blanc de HototOP
Answer