TanStackT
TanStack4mo ago
3 replies
ripe-gray

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
        }
    }
}
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...
Server Entry Point | TanStack Start React Docs
Was this page helpful?