Server Side Rendered HTML Response should return HTTP 200 when loader rejects
Which project does this relate to?
Start
Describe the bug
When loader rejects the server side rendered response returns HTTP 500 even though the HTML was successfully rendered.
I would expect it to be 200 because html was successfully server side rendered and returned. http 500 is more for server failure and rejections of loaders are expected, it is not internal server error.
Your Example Website or App
https://github.com/valerii15298/tss-repros/tree/error-component-override
Steps to Reproduce the Bug or Issue
Start the app Open Browser network devtools Go to http://localhost:3000 See that response for html content returns 500
Expected behavior
Should return 200
Screenshots or Videos
https://discord.com/channels/719702312431386674/1477778007018963014/1477791443463573748
Platform
- Router / Start Version: 1.165.0
Workaround
This seems to work:
start.ts file:
import { createMiddleware, createStart } from "@tanstack/react-start";
export const fixResponseHtmlStatus = createMiddleware().server(
async ({ next }) => {
const resp = await next();
if (
resp.response.status === 500 &&
resp.response.headers.get("content-type") === "text/html; charset=utf-8"
) {
resp.response = new Response(resp.response.body, {
headers: resp.response.headers,
status: 200,
});
}
return resp;
},
);
export const startInstance = createStart(() => ({
requestMiddleware: [fixResponseHtmlStatus],
}));
Not sure if it is best approach, maybe there is a better way...
I looked into this and found some context that might be helpful.
The current 500 behavior was intentionally introduced in #5541 (fixing #5535), where the opposite was the problem — loader errors were always returning 200.
The core logic (router.ts L2413-2424) checks
matches.some(d => d.status === 'error') and unconditionally sets 500, regardless of whether an errorComponent successfully rendered the page.
So the tension is between two layers:
- Application layer: the loader failed (error)
- HTTP layer: the response was successfully delivered with valid HTML (success)
This seems like a design decision that needs maintainer input. A few possible directions I can see:
- Distinguish between "error boundary rendered successfully → 200" vs "no error boundary, render failed → 500"
- Let users control the status code per-route (e.g.
errorStatusCodeoption) or from withinerrorComponent - Keep 500 as-is and document that users should use middleware to override (as the workaround in this thread does)
Happy to help implement whichever direction.