T
TanStack4w ago
narrow-beige

server.ts context declaration merging conflict between react-router and react-start?

Hi - hopefully I understand correctly but I'm trying to pass in a custom context to my server.ts fetch handler. The docs say to do this https://tanstack.com/start/latest/docs/framework/react/guide/server-entry-point#request-context
import handler, { type ServerEntry } from "@tanstack/react-start/server-entry"

type MyRequestContext = {
hello: string
foo: number
}

declare module "@tanstack/react-start" {
interface Register {
server: {
requestContext: MyRequestContext
}
}
}

export default {
fetch(request, opts) {
return handler.fetch(request, { context: { } }) // <----- I don't get any autocomplete on types here...
},
} satisfies ServerEntry
import handler, { type ServerEntry } from "@tanstack/react-start/server-entry"

type MyRequestContext = {
hello: string
foo: number
}

declare module "@tanstack/react-start" {
interface Register {
server: {
requestContext: MyRequestContext
}
}
}

export default {
fetch(request, opts) {
return handler.fetch(request, { context: { } }) // <----- I don't get any autocomplete on types here...
},
} satisfies ServerEntry
The context isn't extended in the handler above...it stays as BaseContext I noticed that the type used for Register is actually in the @tanstack/react-routerlibrary, so if modify the declare module above to use that instead, I DO get typesafety....but then my server route context is undefined
export const Route = createFileRoute("/(api)/api/test")({
server: {
handlers: {
GET: async ({ context }) => {
// context is undefined here
},
},
},
})
export const Route = createFileRoute("/(api)/api/test")({
server: {
handlers: {
GET: async ({ context }) => {
// context is undefined here
},
},
},
})
If I keep the module declaration to react-start, then the context IS defined in the server route, but NOT in the server.ts fetch context. Should I file a bug report for this or is this expected behavior and I just need to make sure my types in server.ts are correct and have autocomplete for all my server routes? Thanks!
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...
2 Replies
optimistic-gold
optimistic-gold3w ago
sure please create a bug report
narrow-beige
narrow-beigeOP3w ago
okay cool will do - thanks for confirming

Did you find this page helpful?