N Next.js 中文
原文 ↗
Next.js 文档/API Reference / API 参考/File-system conventions / 文件系统约定

not-found.js

not-found.js 文件的 API 参考。

更新于 2026-11-06Next.js 16.4.6

Next.js 提供了两种约定来处理未找到的情况:

  • not-found.js:当你在路由段中调用 notFound 函数时使用。
  • global-not-found.js:用于为整个应用中未匹配的路由定义全局 404 页面。它在路由层面处理,不依赖于渲染布局或页面。

not-found.js

当在路由段中抛出 notFound 函数时,not-found 文件会渲染 UI。

在流式渲染开始之前,Next.js 返回 404 状态码。流式渲染开始后,状态码仍为 200,not-found UI 会在响应中渲染。在两种情况下,Next.js 都会注入 <meta name="robots" content="noindex" /> 标签。详情请参阅状态码。

TypeScript

app/not-found.tsxtsx
import Link from 'next/link'

export default function NotFound() {
  return (
    <div>
      <h2>Not Found</h2>
      <p>Could not find requested resource</p>
      <Link href="/">Return Home</Link>
    </div>
  )
}

JavaScript

app/blog/not-found.jsjsx
import Link from 'next/link'

export default function NotFound() {
  return (
    <div>
      <h2>Not Found</h2>
      <p>Could not find requested resource</p>
      <Link href="/">Return Home</Link>
    </div>
  )
}

在组件层次结构中,not-found.js 渲染在 loading.js 和 page.js 之间。它被同一段中来自 loading.js 的 <Suspense> 边界和来自 error.js 的错误边界包裹。

值得了解:

默认的 not-found UI 通过 prefers-color-scheme 遵循操作系统的配色方案,而不会读取应用级主题(例如 <html> 上的 class 或 data-theme 属性)。由于它渲染在根布局内部,要匹配显式主题,最快的方法是在全局样式表中添加一对特异性更高的规则,并将其作用域限定为你的主题选择器——例如 html[data-theme='light'] body 和 html[data-theme='dark'] body。如果希望完全控制标记结构,请提供你自己的 not-found.js。

global-not-found.js(实验性)

global-not-found.js 文件让你可以为整个应用定义 404 页面。与在路由级别工作的 not-found.js 不同,它用于请求的 URL 完全不匹配任何路由的情况。Next.js 跳过渲染并直接返回这个全局页面。

global-not-found.js 文件会绕过应用的正常渲染,这意味着你需要导入 404 页面所需的任何全局样式、字体或其他依赖。这也包括你的主题:由于 global-not-found.js 绕过了布局,操作系统配色方案是默认 UI 唯一能感知的信号,因此请在这个文件中应用你的主题(class 或属性)。

值得了解:

使用精简版的全局样式以及更简单的字体族,可以提升此页面的性能。

当你无法使用 layout.js 和 not-found.js 的组合来构建 404 页面时,global-not-found.js 会很有用。这可能发生在两种情况下:

  • 你的应用有多个根布局(例如 app/(admin)/layout.tsx 和 app/(shop)/layout.tsx),因此没有单一的布局可以组合成全局 404 页面。
  • 你的根布局使用顶层动态段定义(例如 app/[country]/layout.tsx),这会让组合出一致的 404 页面更加困难。

要启用它,请在 next.config.ts 中添加 globalNotFound 标志:

next.config.tstsx
import type { NextConfig } from 'next'

const nextConfig: NextConfig = {
  experimental: {
    globalNotFound: true,
  },
}

export default nextConfig

然后,在 app 目录的根目录中创建一个文件:app/global-not-found.js:

TypeScript

app/global-not-found.tsxtsx
// Import global styles and fonts
import './globals.css'
import { Inter } from 'next/font/google'
import type { Metadata } from 'next'

const inter = Inter({ subsets: ['latin'] })

export const metadata: Metadata = {
  title: '404 - Page Not Found',
  description: 'The page you are looking for does not exist.',
}

export default function GlobalNotFound() {
  return (
    <html lang="en" className={inter.className}>
      <body>
        <h1>404 - Page Not Found</h1>
        <p>This page does not exist.</p>
      </body>
    </html>
  )
}

JavaScript

app/global-not-found.jsjsx
// Import global styles and fonts
import './globals.css'
import { Inter } from 'next/font/google'

const inter = Inter({ subsets: ['latin'] })

export const metadata = {
  title: '404 - Page Not Found',
  description: 'The page you are looking for does not exist.',
}

export default function GlobalNotFound() {
  return (
    <html lang="en" className={inter.className}>
      <body>
        <h1>404 - Page Not Found</h1>
        <p>This page does not exist.</p>
      </body>
    </html>
  )
}

与 not-found.js 不同,此文件必须返回完整的 HTML 文档,包括 <html> 和 <body> 标签。

参考

Props

not-found.js 或 global-not-found.js 组件不接受任何 props。

须知:

  • 除了捕获预期的 notFound() 错误外,根 app/not-found.js 和 app/global-not-found.js 文件还会处理整个应用中所有未匹配的 URL。这意味着当用户访问应用未处理的 URL 时,将向其展示导出的 UI。
  • Next.js 会随其所在分段下的每个路由一起发送每个 not-found.js 文件,而不仅是发送给未找到的页面,并在这些页面上加载它导入的客户端组件。对于根 app/not-found.js 而言,即应用的每个页面,因此在那里应优先使用服务器组件和 <Link>,而不是交互式 UI。

示例

数据获取

默认情况下,not-found 是一个服务器组件。你可以将其标记为 async 以获取并显示数据:

TypeScript

app/not-found.tsxtsx
import Link from 'next/link'
import { headers } from 'next/headers'

export default async function NotFound() {
  const headersList = await headers()
  const domain = headersList.get('host')
  const data = await getSiteData(domain)
  return (
    <div>
      <h2>Not Found: {data.name}</h2>
      <p>Could not find requested resource</p>
      <p>
        View <Link href="/blog">all posts</Link>
      </p>
    </div>
  )
}

JavaScript

app/not-found.jsxjsx
import Link from 'next/link'
import { headers } from 'next/headers'

export default async function NotFound() {
  const headersList = await headers()
  const domain = headersList.get('host')
  const data = await getSiteData(domain)
  return (
    <div>
      <h2>Not Found: {data.name}</h2>
      <p>Could not find requested resource</p>
      <p>
        View <Link href="/blog">all posts</Link>
      </p>
    </div>
  )
}

如果你需要使用像 usePathname 这样的客户端组件 Hook 来根据路径显示内容,则必须改为在客户端获取数据。

元数据

根 app/not-found.js 和 app/global-not-found.js 都支持导出 metadata 对象或 generateMetadata 函数,以自定义 404 页面的 <title>、<meta> 以及其他 head 标签:

须知:

对于返回 404 状态码的页面(包括 global-not-found.js 页面),Next.js 会自动注入 <meta name="robots" content="noindex" />。

TypeScript

app/global-not-found.tsxtsx
import type { Metadata } from 'next'

export const metadata: Metadata = {
  title: 'Not Found',
  description: 'The page you are looking for does not exist.',
}

export default function GlobalNotFound() {
  return (
    <html lang="en">
      <body>
        <div>
          <h1>Not Found</h1>
          <p>The page you are looking for does not exist.</p>
        </div>
      </body>
    </html>
  )
}

JavaScript

app/global-not-found.jsjsx
export const metadata = {
  title: 'Not Found',
  description: 'The page you are looking for does not exist.',
}

export default function GlobalNotFound() {
  return (
    <html lang="en">
      <body>
        <div>
          <h1>Not Found</h1>
          <p>The page you are looking for does not exist.</p>
        </div>
      </body>
    </html>
  )
}

版本历史

版本 变更
v15.4.0 引入 global-not-found.js(实验性)。
v13.3.0 根 app/not-found 处理全局未匹配的 URL。
v13.0.0 not-found 已引入。