Status and streaming errors
What you need to know
HTTP status is chosen before the response headers are committed. After a response has begun streaming, a later rendering failure cannot retroactively replace the status with 404 or 500. The UI and the transport status must be tested separately.
By the end, you can: explain status constraints after headers; design error regression tests.
Check existence before constructing the response
// app/api/articles/[id]/route.ts — complete synthetic read exampleexport async function GET(_:Request,ctx:{params:Promise<{id:string}>}){ const {id}=await ctx.params; if(id!=='fern') return Response.json({error:'Not found'},{status:404}); return Response.json({id,title:'Fern walk'});}Expected behavior: GET with fern returns 200 and a public article. An unknown ID returns 404 before any body is streamed.
How it works
For an App Router page, notFound() terminates the affected segment and renders the not-found UI. A missing resource discovered after a streamed response begins may produce that UI with HTTP 200 and a noindex marker; inspect the actual route, HTML and status rather than assuming. If the HTTP status itself is a contract, decide existence or access before any relevant fallback commits headers, including ancestor loading boundaries. For API routes, validate and authorize before constructing a successful streaming Response. A midstream operational failure needs a defined protocol-level error or incomplete response; a success status alone is not proof of completion. Avoid streaming any sensitive content before authorization. Metadata can have different delivery paths depending on crawler and framework behavior, so verify the relevant user agent rather than claiming universal SEO behavior. The local transport fixture deliberately demonstrates that a committed 200 cannot become 500 by changing server-side status after the first write.
Model the status commitment
Before headers: choose 404, then send Not found.After headers 200 and SHELL: error occurs; send safe failure marker or terminate.Observed HTTP status stays 200; consumer must check completion.Expected behavior: The transport status and completion marker are separate. A real Next.js error boundary or not-found segment has its own documented behavior.
Common mistake
Checking for a missing record only after sending its protected data cannot repair the disclosure. If a non-200 status matters, move the existence and access decision before the response is committed; do not rely on a nested error screen to change headers.
Practice and verification
Test an early missing route and a late failure as separate cases. Assert status, UI/JSON, noindex where relevant, absence of private data, and completion behavior. For framework tests, inspect any parent loading UI that could commit the response earlier.
Scope: App Router, proposed Next.js 16.x and React 19, Cache Components disabled. Framework snippets are staged, not executed; local policy, SQLite and HTTP transport evidence is separate. Exact patched dependencies and platform contracts are pending.
Further reading: Loading · Security Data · Self Host