T
TanStack15h ago
ratty-blush

Request Context's properties are null when defined in Server Entry Point

I am trying to follow Server Entry Point and i try to define request context there, as defined. However in middleware for my Route i expect to get the values of context properties that i have set, but they are all null.
// src/routes/server-entry-point.tsx
export const Route = createFileRoute('/server-entry-point')({
component: RouteComponent,
server: {
middleware: [
createMiddleware().server(async ({ next, context }) => {
console.log('context.foo', context.foo)
console.log('context.hello', context.hello)

return await next()
}),
],
},
})

// src/server.ts
import handler from '@tanstack/react-start/server-entry'

export type MyRequestContext = {
hello: string
foo: number
}

export default {
fetch(request: Request) {
console.log(
'Processing request from handler defined in server entry point.'
)
// We need to define module augmentation to allow passing context.
return handler.fetch(request, { context: { hello: 'world', foo: 123 } })
},
}

// src/types.d.ts
import { MyRequestContext } from './server'

declare module '@tanstack/react-start' {
interface Register {
server: {
requestContext: MyRequestContext
}
}
}
// src/routes/server-entry-point.tsx
export const Route = createFileRoute('/server-entry-point')({
component: RouteComponent,
server: {
middleware: [
createMiddleware().server(async ({ next, context }) => {
console.log('context.foo', context.foo)
console.log('context.hello', context.hello)

return await next()
}),
],
},
})

// src/server.ts
import handler from '@tanstack/react-start/server-entry'

export type MyRequestContext = {
hello: string
foo: number
}

export default {
fetch(request: Request) {
console.log(
'Processing request from handler defined in server entry point.'
)
// We need to define module augmentation to allow passing context.
return handler.fetch(request, { context: { hello: 'world', foo: 123 } })
},
}

// src/types.d.ts
import { MyRequestContext } from './server'

declare module '@tanstack/react-start' {
interface Register {
server: {
requestContext: MyRequestContext
}
}
}
Server Entry Point | TanStack Start React Docs
Server Entry Point [!NOTE] The server entry point is optional out of the box. If not provided, TanStack Start will automatically handle the server entry point for you using the below as a default. Thi...
1 Reply
rare-sapphire
rare-sapphire8h ago
This is happening for me too. There's an example repo made by someone else here: https://github.com/backpine/tanstack-start-on-cloudflare which reproduces the issue
GitHub
GitHub - backpine/tanstack-start-on-cloudflare
Contribute to backpine/tanstack-start-on-cloudflare development by creating an account on GitHub.

Did you find this page helpful?