You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
When running a TanStack Start app on Cloudflare (Workers/Pages) with SSR, the Cloudflare environment bindings (in getContext("cloudflare")) appear not to be accessible in the SSR pipeline. They work fine for API or “server” routes, but not for SSR.
Your Example Website or App
nonexistent
Steps to Reproduce the Bug or Issue
Steps to Reproduce
Project Setup
Using TanStack Start (@tanstack/start@^1.101.2) with a config that has multiple routers (api, ssr, client, etc.).
session.server.ts (auth/session middleware for TanStack start server functions)
import{createMiddleware}from"@tanstack/start";import{getEvent,getWebRequest}from"@tanstack/start/server";import{createAuth}from"@/lib/server/auth/auth.server";exportconstsessionMiddleware=createMiddleware().server(async({ next })=>{constrequest=getWebRequest();if(!request){thrownewError("Request not found");}constevent=getEvent();constauth=createAuth(event.context.env);constsession=awaitauth.api.getSession({headers: request.headers});constisAuthenticated=!!session;returnnext({context: {
auth,
isAuthenticated,},});});
During SSR on Cloudflare, getContext("cloudflare") should contain the same environment bindings as API routes, so we can securely access secrets at runtime.
Actual Behavior
getContext("cloudflare") appears to return an empty result on SSR. The SSR pipeline does not receive the same Worker event, forcing fallback to build-time or process env.
In our app, API routes correctly receive the Cloudflare environment bindings, while our SSR route server function does not. Below are two examples that illustrate the difference.
API Route Example (Works as Expected)
The following API route (e.g., in src/entry.api.ts) uses TanStack Start's API routing. In this code, when the handler is executed, the event.context.env is populated with the correct Cloudflare bindings (e.g., secrets and other environment variables).
When you inspect the logs for this API route, you’ll see that getContext("cloudflare") (and consequently event.context.env) contains the expected Cloudflare secrets.
SSR Route / Server Function Example (Does Not Work as Expected)
import{createMiddleware}from"@tanstack/start";import{getEvent,getWebRequest}from"@tanstack/start/server";import{createAuth}from"@/lib/server/auth/auth.server";exportconstsessionMiddleware=createMiddleware().server(async({ next })=>{constrequest=getWebRequest();if(!request){thrownewError("Request not found");}constevent=getEvent();constauth=createAuth(event.context.env);constsession=awaitauth.api.getSession({headers: request.headers});constisAuthenticated=!!session;returnnext({context: {
auth,
isAuthenticated,},});});
When you inspect the logs for this SSR route / Server Function, you’ll observe that the getContext("cloudflare") is empty.
Observed Logs
For API routes (e.g. api or a router with target: "server"), getContext("cloudflare") is populated with the correct Cloudflare secrets.
For SSR routes (e.g. ssr), the getContext("cloudflare") is empty.
Theories About the Underlying Cause
Nitro/TanStack SSR adapter: Possibly the SSR request is run in a separate context that doesn’t inherit the Worker’s event.env.
Cloudflare Pages Functions: On Cloudflare Pages, SSR might not have the same event shape as normal Worker routes.
No official pass-through: The TanStack Start SSR pipeline may not explicitly forward event.env to SSR.
Any guidance on how to ensure SSR receives the Cloudflare environment at runtime would be greatly appreciated!
The text was updated successfully, but these errors were encountered:
Which project does this relate to?
Start
Describe the bug
When running a TanStack Start app on Cloudflare (Workers/Pages) with SSR, the Cloudflare environment bindings (in
getContext("cloudflare")
) appear not to be accessible in the SSR pipeline. They work fine for API or “server” routes, but not for SSR.Your Example Website or App
nonexistent
Steps to Reproduce the Bug or Issue
Steps to Reproduce
Project Setup
@tanstack/start@^1.101.2
) with a config that has multiple routers (api
,ssr
,client
, etc.).wrangler pages dev
.Code Snippets
app.config.ts
(TanStack start config)env.server.ts
(context injection middleware)entry.server.ts
(SSR entry point)entry.api.ts
(API server entry point)session.server.ts
(auth/session middleware for TanStack start server functions)$.ts
(API routes for auth redirect)package.json
(with build command I'm using on Cloudflare)wrangler.toml
(for CF deployment)Expected behavior
Expected Behavior
During SSR on Cloudflare,
getContext("cloudflare")
should contain the same environment bindings as API routes, so we can securely access secrets at runtime.Actual Behavior
getContext("cloudflare")
appears to return an empty result on SSR. The SSR pipeline does not receive the same Worker event, forcing fallback to build-time or process env.Screenshots or Videos
No response
Platform
Additional context
Additional Context: API Routes vs. SSR Routes
In our app, API routes correctly receive the Cloudflare environment bindings, while our SSR route server function does not. Below are two examples that illustrate the difference.
API Route Example (Works as Expected)
The following API route (e.g., in
src/entry.api.ts
) uses TanStack Start's API routing. In this code, when the handler is executed, theevent.context.env
is populated with the correct Cloudflare bindings (e.g., secrets and other environment variables).When you inspect the logs for this API route, you’ll see that
getContext("cloudflare")
(and consequently event.context.env) contains the expected Cloudflare secrets.SSR Route / Server Function Example (Does Not Work as Expected)
When you inspect the logs for this SSR route / Server Function, you’ll observe that the
getContext("cloudflare")
is empty.Observed Logs
api
or a router withtarget: "server"
),getContext("cloudflare")
is populated with the correct Cloudflare secrets.ssr
), thegetContext("cloudflare")
is empty.Theories About the Underlying Cause
event.env
.event.env
to SSR.Any guidance on how to ensure SSR receives the Cloudflare environment at runtime would be greatly appreciated!
The text was updated successfully, but these errors were encountered: