TanStack / TanStack/router

`setResponseHeaders` and `setResponseStatus` don't work in global middleware

Open
#5,407 1 comment 2 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

needed-for-start-stable
Dominant language
TypeScript
Stars
15.1k
Forks
1.9k
Avg merge
1d 20h
Merged PRs (30d)
143

Description

Which project does this relate to?

Start

Describe the bug

When setting response headers using setResponseHeaders or response status using setResponseStatus in a global middleware server handler, the headers and status are not set.

setResponseHeader works as expected.

Your Example Website or App

https://github.com/nickprinsloo/tss-example

Steps to Reproduce the Bug or Issue

Given the following start.ts:

import { createMiddleware, createStart } from "@tanstack/react-start";
import { setResponseHeaders, setResponseStatus } from "@tanstack/react-start/server";

const requestMiddleware = createMiddleware().server(({ next }) => {
  const headers = new Headers()
  headers.set("X-Hello", "World")
  headers.set("X-Foo", "Bar")
  setResponseHeaders(headers)
  setResponseStatus(404)
  return next()
})

export const startInstance = createStart(() => {
  return {
    requestMiddleware: [requestMiddleware],
  };
});

I'd expect the custom headers + status to be present on the response.

Expected behavior

As a user, I expect to see the headers and status of the response to be those set in the global middleware.

Screenshots or Videos

No response

Platform
  • Start: ^1.132.48
  • Node: v22.19.0
  • macOS Tahoe
Additional context

No response

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with start.ts in the linked reproduction and run the example to observe the response from its global requestMiddleware. Trace setResponseHeaders and setResponseStatus through the server middleware entry points; done means the response includes X-Hello and X-Foo with status 404, while setResponseHeader remains working.

Written by the indexing model from the issue text.

Assessment

Tech stack
typescript
Domain
api, backend
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
42/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.