[Start] `setResponseStatus` in a server fn called by a loader during SSR becomes the HTML document's status (since 1.169.0)

未关闭
#8,834 4 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看

维护者通常 1 天内回复

@adityareddy-dev 已经在做这个了。

开始于 2026年11月4日。

  • #8864 来自 @adityareddy-dev —— 未关闭

评估

难度
4/5
预计耗时
3-5 天
新手友好度
45/100
Issue 类型
缺陷
描述清晰度
描述清楚
活跃度
活跃
技术栈
playwright, react, typescript, vite
领域
api, backend

调研方向

Start with the named response-state flow in internal-request-response.js (setResponseStatus and applyResponseState), then trace the in-process call in createSsrRpc.js and document finalization in createStartHandler.js. Run the linked reproducer with pnpm i && pnpm test to establish the current behavior. Done means SSR document requests retain the router's status while client RPC responses still use the status set by the server function.

由索引模型根据 Issue 内容生成。

描述

Which project does this relate to?

Start

Describe the bug

A server fn sets a status for its RPC caller, for example setResponseStatus(503) when an upstream is down, and returns a fallback value (or throws, and the loader catches). When a route loader calls that fn during SSR, the fn runs in the document request's context, and the status it set replaces the document's status. The page renders its fallback correctly but is served as 503.

export const getItem = createServerFn({ method: 'GET' })
  .validator((id: string) => id)
  .handler(({ data: id }) => {
    setResponseStatus(503)
    return { ok: false, id }
  })

export const Route = createFileRoute('/items/$id')({
  loader: ({ params }) => getItem({ data: params.id }),
  component: Item,
})

On a client navigation the RPC answers 503, which is what the fn intends. On a full page load, GET /items/1 answers 503 with a fully rendered page.

The same thing happens when the fn throws after setResponseStatus(503) and the loader catches the error. Response headers set by the fn (for example cache-control) also reach the document. That is probably wanted for setCookie, so this report is about the status.

This is a regression. @tanstack/react-start 1.168.60 (start-server-core 1.169.39) answers 200. 1.169.0 (start-server-core 1.169.41, which shipped #7588) and 1.169.4 answer 503.

Complete minimal reproducer

https://github.com/Tarekkharsa/tanstack-start-server-fn-status

Steps to Reproduce the Bug
  1. pnpm i && pnpm test
  2. The test builds, runs vite preview on port 7311, fetches /items/1 and /caught/1, then clicks the same routes in Chromium to record the RPC status. It exits 1 when a document is not 200.

Output:

SSR document requests (a route loader calls a server fn that sets 503):
  FAIL GET /items/1 -> HTTP 503 (expected 200); fallback rendered: true
  FAIL GET /caught/1 -> HTTP 503 (expected 200); fallback rendered: true
Client navigation (the same server fn called as an RPC):
  info /items/1: RPC /_serverFn/8804e69444d5c... -> HTTP 503 (503 is intended here)
  info /caught/1: RPC /_serverFn/5863b083327b3... -> HTTP 503 (503 is intended here)

2/2 documents answered with the server fn's status instead of 200
Expected behavior

A status set by a server fn applies to that fn's own RPC response. When a loader calls the fn in-process during SSR, the document keeps the status the router decides (200 here, or 404 from notFound(), and so on).

Screenshots or Videos

n/a

Platform
  • Router / Start Version: @tanstack/react-start 1.169.4, @tanstack/start-server-core 1.169.45, @tanstack/react-router 1.170.46, @tanstack/router-core 1.171.39
  • React: 19.3.1
  • OS: macOS 27.0
  • Node: 24.22.1
  • Browser: Chromium 156 (Playwright 1.64.1), only for the RPC half of the test
  • Bundler: vite
  • Bundler Version: 8.3.5
Additional context

Where it happens, in the published @tanstack/[email protected] dist/esm:

  • internal-request-response.js:449-453: one event per incoming request, run with eventStorage.run(event, ...).
  • internal-request-response.js:715-726: setResponseStatus writes getResponseState(event).status on that event.
  • createSsrRpc.js:8: during SSR the fn runs in-process through getServerFnById(id, { origin: 'server' }), inside the same event.
  • createStartHandler.js:500: the document goes through finalizeResponse(response, event), and applyResponseState (internal-request-response.js:255-279) sends helperStatus ?? response.status.

So the fn's status lands on the document. The RPC path reads the same state through getSerializedResponseState() (server-functions-handler.js:331), which is the intended use.

Possible fixes:

  1. When a server fn runs in-process (origin server, not a server-fn request), save responseState.status/statusText before the call and restore them after. Headers and cookies still merge into the document as they do now.

  2. Or give the fn's handler and function middleware a public way to know whether it runs as an RPC. Today an app can only check internals (x-tsr-serverFn header plus TSS_SERVER_FN_BASE), or pass handlerType from a request middleware through context:

    const handlerTypeMiddleware = createMiddleware().server(({ next, handlerType }) =>
      next({ context: { isServerFnRpc: handlerType === 'serverFn' } }),
    )
    export const startInstance = createStart(() => ({ requestMiddleware: [handlerTypeMiddleware] }))
    // in the fn: if (context.isServerFnRpc) setResponseStatus(503)
    

    I checked this workaround against the reproducer: documents answer 200 and the RPCs still answer 503.

Related, not duplicates:

  • #7588: response reconciliation, the change that made helper state override the returned response.
  • #6785 (open PR): keeps the status when a handler throws after setResponseStatus.
  • #7647 (open): keeping a middleware-set status drops middleware-set headers.
  • #7914 (open): response-context headers in non-2xx serialized server fn responses.
  • #5107, #5407 (closed): setResponseStatus ignored with throw and in global middleware.

This issue was drafted with an AI assistant and checked by me against the reproducer.

主要语言
TypeScript
星标
15.3k
派生
1.9k
平均合并
1 天 5 小时
30 天内合并 PR
46

环境准备

在 Codespaces 中打开

在浏览器里用你自己的 GitHub 账号启动这个项目的开发容器。

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

TanStack/router 的其他 Issue

查看 TanStack/router 的全部 Issue

相似的 Issue

更多 TypeScript Issue

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。