Skip to content
Source
Sign inSign up
TanStack/router

docs(start): explain response helper scope during SSR #8864

Openadityareddy-dev(GitHub profile, opens in a new tab)wants to merge 1 commit intomain fromrefs/pull/8864/headopened

Showing 1 changed file with 23 additions and 0 deletions.

x
0 of 1 viewed, 1 left
23 additions and 0 deletionsdocs/start/framework/react/guide/server-functions.mdModified
View file
@@ -384,6 +384,29 @@
- `setResponseHeaders(headers)` - Set multiple response headers via Headers object
- `setResponseStatus(code)` - Set the HTTP status code
​
+#### Response helpers during SSR
+​
+Response helpers modify the current HTTP request's response. When a route loader calls a server function during SSR, that function shares the document request's response context. Calling `setResponseStatus(503)` therefore makes the rendered page respond with 503, even if the loader catches an error or renders fallback data. Response headers and cookies set by the function also apply to the document response.
+​
+If a response change should apply only to a server function's RPC response, use [request middleware](./middleware.md#request-middleware) to pass the request's `handlerType` through context:
+​
+```tsx
+// src/start.ts
+import { createMiddleware, createStart } from '@tanstack/react-start'
+​
+const responseScope = createMiddleware().server(({ next, handlerType }) => {
+ return next({
+ context: { isServerFnRpc: handlerType === 'serverFn' },
+ })
+})
+​
+export const startInstance = createStart(() => ({
+ requestMiddleware: [responseScope],
+}))
+```
+​
+The server function can then read `context.isServerFnRpc` and call `setResponseStatus(503)` only when it is `true`. During a document request, `handlerType` is `'router'`, so an in-process call can return fallback data without changing the document's status. `handlerType` is available on request middleware, not directly on function middleware or server function handlers.
+​
### Streaming
​
Stream typed data from server functions to the client. See the [Streaming Data from Server Functions guide](./streaming-data-from-server-functions).