AI & Agent Dev Bug Sandbox logo
AI & Agent Dev Bug Sandbox
Back to Radar

404 Page Isn'T Server Rendered When Using NotFound()

When calling notFound() inside a page component in Next.js App Router, the initial server response contains only client-side script tags, not the server-rendered Not Found page HTML. This affects both development and production, including static pages returning incorrect 200 status in production.

highConfidence 85%Next.jsAffected V14.1.0

Origin Analysis

In Next.js App Router, the notFound() function throws a special error that is caught by the framework's error boundary, but for statically optimized pages the Not Found boundary is not rendered on the server. Instead, the client receives a generic client-side rendered shell, and the not-found UI is only hydrated on the client. This design flaw prevents server-side rendering of the 404 page and leads to incorrect HTTP status codes for static routes.
1. Create a new Next.js project using `npx create-next-app@latest`. 2. In app/page.tsx (or any page), call `notFound()` from `next/navigation`. 3. Start the app with `next dev` or `next build && next start`. 4. Open the page in a browser and inspect the network response. The response body contains only script tags, not the rendered HTML of the 404 page. 5. Compare with a non-existent route: the default Not Found page is server-rendered.

Fixing Code Block

Edge Case Audit

This workaround disables static optimization for the route, potentially increasing server load and response time. It is incompatible with `output: 'export'` (static export). If a future Next.js version fixes the underlying bug, this workaround should be removed to restore static optimization. Rollback: remove the `export const dynamic` line from the page.

Ecosystem Topology