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

NotFound() In Dynamic Routes Serves Empty __Next_error__ Document Instead Of Not-Found.Tsx SSR HTML

When a server component calls notFound() inside a dynamic route, the SSR response contains an empty HTML shell with <html id="__next_error__"> instead of rendering the root layout and not-found.tsx content. This occurs because the not-found error is thrown during server rendering and caught by the outer app-render catch, which generates a minimal error document rather than re-rendering the tree with the not-found boundary active.

highConfidence 85%Next.jsAffected V16.3.6Affected V16.4.0-Canary.48

Origin Analysis

The HTTPAccessFallbackBoundary is a client-only error boundary, and React does not execute error boundaries during server rendering. Therefore, the NEXT_NOT_FOUND error thrown by notFound() escapes the Fizz shell and is caught by the app-render catch block (renderToStream and prerenderToStream). This catch sets the 404 status and renders getErrorRSCPayload with the hardcoded __next_error__ document, skipping the root layout and not-found.tsx.
1. Create a Next.js app with app/[slug]/page.tsx that calls notFound() unless slug === 'known'. 2. Add app/not-found.tsx and a root layout with <html lang="en">. 3. Build and start the app. 4. Fetch /missing (calls notFound) and /no/such/route (unmatched route). 5. Observe that /missing returns 404 with empty __next_error__ document, while /no/such/route correctly renders not-found.tsx inside the root layout.

Fixing Code Block

Edge Case Audit

This middleware is a hack and must be tailored to your specific routing structure. It can break if you have other single-segment routes such as /about, /contact, etc., or if your valid slugs change. Misconfigured rewrites may cause infinite loops or serve 404 for valid pages. It also adds middleware overhead to every request. Remove this workaround once the underlying Next.js bug is fixed; otherwise, it may mask legitimate 404 handling and conflict with future framework updates. For rollback, simply delete or comment out the middleware file.

Ecosystem Topology