TanStackT
TanStack3mo ago
10 replies
then-purple

Set HTTP status code from loaders

Happy to build a full example repo to explain this further, but hopefully should be straightforward.

I am calling some external APIs in my loader, and sometimes they fail. Right now what happens is the errorComponent renders fine, but the HTTP status code is 200, and there's noway to change that.

Tried setResponseStatus but that just gets ignored. Right now the only way I can get around this is a funky way of setting a custom header, then checking for that header in a custom Start handler on the server, then setting the response status code from there.

Seems a bit weird, I'm sure there's a good reason, but would like some insight on this if possible.

I know it doesn't really matter, but a lot of scenarios like sites behind Cloudflare, those 200s get cached and the page is then cached with an error component on it even if the API ends up working later on.
Was this page helpful?