A custom 404 page in Next.js (up to version 16, at the time of writing) is surprisingly tricky once you use route groups. Next.js expects a not-found.jsx next to the layout of a route. But what if you have several route groups, each with its own layout?
Say we have two of them, each with a layout and a not-found.jsx:
app
├── (admin)
│ ├── admin
│ ├── layout.jsx
│ └── not-found.jsx
├── (main)
│ ├── foo
│ ├── page.jsx
│ ├── not-found.jsx
│ └── layout.jsx
└── favicon.ico
When the user requests a missing page inside a group (e.g. /foo/404), Next.js renders that group’s custom 404, as expected. A group’s not-found.jsx kicks in whenever a route in that group calls notFound().
Now request a URL that doesn’t match any group, like /404. The app falls back to the default Next.js 404 page, which looks nothing like the rest of your site.
So why not put a not-found.jsx in the root (/src/app)? Next.js won’t build it:
Build error: not-found.jsx doesn’t have a root layout. To fix this error, make sure every page has a root layout.
What about a minimal “mock” root layout?
// src/app/layout.jsx
export default function RootLayout({ children }) {
return children;
}
That gets past the build and fails at runtime:
Runtime Error: Missing and
tags in the root layout. Read more at https://nextjs.org/docs/messages/missing-root-layout-tags
I tried many more variations, with no luck, until I came across a workaround in a post by huozhi from the Next.js team: https://x.com/huozhi/status/1921690791694446919. It’s an experimental option added in Next.js 15.4, and by now the docs mention exactly this case, an app with multiple root layouts.
Enable globalNotFound in next.config.mjs:
// next.config.mjs
export default {
experimental: {
globalNotFound: true
}
};
Then remove any root layout.jsx or not-found.jsx and create global-not-found.jsx instead:
app
├── (admin)
│ ├── admin
│ ├── layout.jsx
│ └── not-found.jsx
├── (main)
│ ├── foo
│ ├── page.jsx
│ ├── not-found.jsx
│ └── layout.jsx
├── favicon.ico
└── global-not-found.jsx
Next.js uses this file for any URL that doesn’t match a route at all. It skips your layouts entirely, so the file has to return a complete HTML document. A self-contained version could look like this:
export default function GlobalNotFound() {
return (
<html lang="en">
<head>
<meta charSet="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<meta name="robots" content="noindex" />
<title>404 - Page Not Found</title>
<style dangerouslySetInnerHTML={{ __html: `
body {
margin: 0;
height: 100vh;
display: flex;
justify-content: center;
align-items: center;
font-family: Arial, sans-serif;
background: #f0f0f0;
color: #333;
text-align: center;
}
h1 { font-size: 6rem; margin: 0; color: #ff4d4f; }
p { font-size: 1.25rem; margin: 0.5rem 0 1.5rem; }
a {
display: inline-block;
padding: 0.5rem 1rem;
text-decoration: none;
color: white;
background: #1890ff;
border-radius: 5px;
transition: background 0.2s;
}
a:hover { background: #40a9ff; }
`}} />
</head>
<body>
<div>
<h1>404</h1>
<p>Page not found</p>
<a href="/">Go to homepage</a>
</div>
</body>
</html>
);
}
Two lines in there aren’t really needed. Next.js adds <meta name="robots" content="noindex" /> to every 404 response by itself, and the title can come from a metadata export, as on any other page.
The page works, renders entirely on the server and depends on nothing else in the app. That’s also the problem. Because it bypasses your layouts, it doesn’t share your header, footer, global styles or fonts unless you import them yourself.
Can we reuse what a route group already has? We can. Create the 404 content as a regular page inside a group, e.g. src/app/(main)/404/page.jsx, and import it into global-not-found.jsx together with the group’s layout:
import NotFoundPage from './(main)/404/page';
import MainLayout from './(main)/layout';
export default function GlobalNotFound() {
return (
<MainLayout>
<NotFoundPage />
</MainLayout>
);
}
(main)/layout.jsx is a root layout, so it already renders <html> and <body> and brings in the group’s styles. The global 404 looks exactly like the rest of the site.
There’s a catch though. Route groups don’t show up in the URL, so (main)/404/page.jsx is also a real route at /404, and it answers with status 200. In my opinion it’s cleaner to keep the 404 content as a plain component outside app/ (say src/ui/components/NotFound) and import that instead. Then nothing extra gets routed.
With that in place, every 404 is styled. Each group’s not-found.jsx handles notFound() calls inside the group, and global-not-found.jsx handles URLs that match nothing at all. The one thing left to watch is the experimental flag, so check the release notes when you upgrade Next.js.