Skip to content

Commit 373f3e1

Browse files
authored
fix: implications of global-not-found skipping 'normal' rendering (#82399)
Fixes: #82379 Also fixing the Metadata snippets at the bottom of the page
1 parent ea08bf2 commit 373f3e1

File tree

1 file changed

+74
-6
lines changed

1 file changed

+74
-6
lines changed

docs/01-app/03-api-reference/03-file-conventions/not-found.mdx

Lines changed: 74 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,11 @@ export default function NotFound() {
4444

4545
The `global-not-found.js` file lets you define a 404 page for your entire application. Unlike `not-found.js`, which works at the route level, this is used when a requested URL doesn't match any route at all. Next.js **skips rendering** and directly returns this global page.
4646

47-
This is useful when you can't build a 404 page using a combination of `layout.js` and `not-found.js`. This can happen in two cases:
47+
The `global-not-found.js` file bypasses your app's normal rendering, which means you'll need to import any global styles, fonts, or other dependencies that your 404 page requires.
48+
49+
> **Good to know**: A smaller version of your global styles, and a simpler font family could improve performance of this page.
50+
51+
`global-not-found.js` is useful when you can't build a 404 page using a combination of `layout.js` and `not-found.js`. This can happen in two cases:
4852

4953
- Your app has multiple root layouts (e.g. `app/(admin)/layout.tsx` and `app/(shop)/layout.tsx`), so there's no single layout to compose a global 404 from.
5054
- Your root layout is defined using top-level dynamic segments (e.g. `app/[country]/layout.tsx`), which makes composing a consistent 404 page harder.
@@ -66,9 +70,45 @@ export default nextConfig
6670
Then, create a file in the root of the `app` directory: `app/global-not-found.js`:
6771

6872
```tsx filename="app/global-not-found.tsx" switcher
73+
// Import global styles and fonts
74+
import './globals.css'
75+
import { Inter } from 'next/font/google'
76+
import type { Metadata } from 'next'
77+
78+
const inter = Inter({ subsets: ['latin'] })
79+
80+
export const metadata: Metadata = {
81+
title: '404 - Page Not Found',
82+
description: 'The page you are looking for does not exist.',
83+
}
84+
85+
export default function GlobalNotFound() {
86+
return (
87+
<html lang="en" className={inter.className}>
88+
<body>
89+
<h1>404 - Page Not Found</h1>
90+
<p>This page does not exist.</p>
91+
</body>
92+
</html>
93+
)
94+
}
95+
```
96+
97+
```jsx filename="app/global-not-found.js" switcher
98+
// Import global styles and fonts
99+
import './globals.css'
100+
import { Inter } from 'next/font/google'
101+
102+
const inter = Inter({ subsets: ['latin'] })
103+
104+
export const metadata = {
105+
title: '404 - Page Not Found',
106+
description: 'The page you are looking for does not exist.',
107+
}
108+
69109
export default function GlobalNotFound() {
70110
return (
71-
<html>
111+
<html lang="en" className={inter.className}>
72112
<body>
73113
<h1>404 - Page Not Found</h1>
74114
<p>This page does not exist.</p>
@@ -140,18 +180,46 @@ If you need to use Client Component hooks like `usePathname` to display content
140180

141181
For `global-not-found.js`, you can export a `metadata` object or a [`generateMetadata`](/docs/app/api-reference/functions/generate-metadata) function to customize the `<title>`, `<meta>`, and other head tags for your 404 page:
142182

183+
> **Good to know**: Next.js automatically injects `<meta name="robots" content="noindex" />` for pages that return a 404 status code, including `global-not-found.js` pages.
184+
143185
```tsx filename="app/global-not-found.tsx" switcher
186+
import type { Metadata } from 'next'
187+
188+
export const metadata: Metadata = {
189+
title: 'Not Found',
190+
description: 'The page you are looking for does not exist.',
191+
}
192+
193+
export default function GlobalNotFound() {
194+
return (
195+
<html lang="en">
196+
<body>
197+
<div>
198+
<h1>Not Found</h1>
199+
<p>The page you are looking for does not exist.</p>
200+
</div>
201+
</body>
202+
</html>
203+
)
204+
}
205+
```
206+
207+
```jsx filename="app/global-not-found.js" switcher
144208
export const metadata = {
145209
title: 'Not Found',
146210
description: 'The page you are looking for does not exist.',
147211
}
148212

149213
export default function GlobalNotFound() {
150214
return (
151-
<div>
152-
<h1>Not Found</h1>
153-
<p>The page you are looking for does not exist.</p>
154-
</div>
215+
<html lang="en">
216+
<body>
217+
<div>
218+
<h1>Not Found</h1>
219+
<p>The page you are looking for does not exist.</p>
220+
</div>
221+
</body>
222+
</html>
155223
)
156224
}
157225
```

0 commit comments

Comments
 (0)