Skip to content

setResponseHeaders and setResponseStatus don't work in global middleware #5407

@nickprinsloo

Description

@nickprinsloo

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

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions