router icon indicating copy to clipboard operation
router copied to clipboard

Server Side Rendered HTML Response should return HTTP 200 when loader rejects

Open valerii15298 opened this issue 9 months ago • 2 comments

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

valerii15298 avatar Mar 01 '26 22:03 valerii15298

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...

valerii15298 avatar Mar 02 '26 00:03 valerii15298

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:

  1. Distinguish between "error boundary rendered successfully → 200" vs "no error boundary, render failed → 500"
  2. Let users control the status code per-route (e.g. errorStatusCode option) or from within errorComponent
  3. 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.

kyu-rong avatar May 01 '26 17:05 kyu-rong