Custom 404 for the whole site
- App Router
app/not-found.js, which also handles any unmatched URL- Pages Router
pages/404.js, statically generated at build time
Build a Next.js 404 with not-found.js and notFound() in the App Router or pages/404.js, and learn when a not-found response still returns 200.
In the Next.js App Router, add app/not-found.js for the 404 UI and call notFound() from next/navigation when data is missing. Next.js returns a 404 status before streaming starts, and injects <meta name="robots" content="noindex" /> either way. In the Pages Router, create pages/404.js, which is statically generated at build time.
Four quick questions, then a straight answer.
From your answers, this is quietly costing you traffic and nobody has the hours to chase it. Scaplet finds every affected page, drafts the fixes and tracks how AI answers mention you.
A data platform shipped 46 fixes without a ticket.
This looks handled. Re-check after a redesign, a migration or a big content push; that is when it breaks.
Your site is small and you have the time. Work through the steps on this page, starting with the pages that get the most traffic.
app/not-found.js, which also handles any unmatched URLpages/404.js, statically generated at build timenotFound() in the render path<Error statusCode={...} /> from next/error yourselfnot-found.js in that route segmentglobal-not-found.js (experimental)pages/500.js or pages/_error.js notFound() throws a NEXT_HTTP_ERROR_FALLBACK;404 error and terminates rendering of the route segment, so calling it is enough.
A call left in an un-awaited promise throws where nothing catches it and no not-found UI renders. In development the server logs ⨯ unhandledRejection: NEXT_HTTP_ERROR_FALLBACK;404.
notFound() can be invoked in Server Components, Server Functions, and Route Handlers.
The root app/not-found.js supports exporting metadata or generateMetadata to set the title and other head tags.
The default not-found UI follows the operating system color scheme and doesn't read an app-level theme, so supply your own not-found.js for control.
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>
)
}import { notFound } from 'next/navigation'
export default async function Profile({
params,
}: {
params: Promise<{ id: string }>
}) {
const { id } = await params
const user = await fetchUser(id)
if (!user) {
notFound()
}
// ...
}404 status code, but after streaming starts the status code remains 200 and the not-found UI renders in the response. Next.js injects <meta name="robots" content="noindex" /> in both cases, which keeps the URL out of search results even at 200. Some crawlers may label those responses as soft 404s.Streaming starts when a Suspense fallback renders, such as a loading.tsx, or when a Server Component suspends under a <Suspense> boundary. Calling notFound() before that point preserves the 404. If you need a real 404 for compliance or analytics, run the check before the body streams. For a 404, Next.js says you can check the slug in Proxy, then rewrite to a not-found route or return a 404 response, and to keep Proxy checks fast.
Google's reasoning is straightforward: it doesn't index URLs that return a 4xx status code, while a 2xx response whose content looks like an error message appears as a soft 404 in Search Console. For deleted pages with a replacement, a redirect is the better answer; see Next.js redirects.
global-not-found.js is experimental. Next.js documents it for apps with multiple root layouts, or a root layout built from top-level dynamic segments such as app/[country]/layout.tsx, where one layout can't compose a global 404. It bypasses normal rendering, so import your global styles and fonts in the file, and it must return a full HTML document with <html> and <body>.import type { NextConfig } from 'next'
const nextConfig: NextConfig = {
experimental: {
globalNotFound: true,
},
}
export default nextConfigpages/404.js is statically generated at build time, and you can use getStaticProps in it.pages/_error.js is only used in production. In development you get an error with the call stack.Error from next/error and pass statusCode. The Error component does not support getStaticProps or getServerSideProps.export default function Custom404() {
return <h1>404 - Page Not Found</h1>
}Run it on a made-up URL and on a missing dynamic slug such as /user/does-not-exist. Next.js says it returns a 404 status before streaming starts, so that is what you want to see for the dynamic slug. If the dynamic route prints 200, look for a Suspense boundary or loading.tsx above the check, and confirm the robots meta tag is present either way.
curl -s -o /dev/null -w "%{http_code}\n" https://example.com/does-not-exist-123
curl -s https://example.com/does-not-exist-123 | grep -i 'name="robots"'2 questions people ask next, answered from the sources on this page.
In the App Router, add app/not-found.js. In the Pages Router, create pages/404.js, which Next.js statically generates at build time.
No. Before streaming starts Next.js returns 404. After streaming starts the status stays 200, though it still injects a robots noindex meta tag.
Run the free audit, then approve the fixes. Every change can be undone.