T
TanStack5w ago
underlying-yellow

createFileRoute() doesn't pass context from global middleware -- FIXED

EDIT: I figured out this issue is fixed in a commit a few days ago: https://github.com/TanStack/router/commit/41cc7e19ef6f40538cd621d54ca7a478e301a703 I have a global middleware that adds a session to context. It's included like this:
export const startInstance = createStart(() => {
return {
// middleware applied to all requests (API and server functions)
requestMiddleware: [authMiddleware],
}
})
export const startInstance = createStart(() => {
return {
// middleware applied to all requests (API and server functions)
requestMiddleware: [authMiddleware],
}
})
It works perfectly on server function routes. But on a file route, the context is an empty object, despite having proper types:
export const Route = createFileRoute("/api/chat")({
server: {
handlers: {
POST: async ({ context }) => {
// context is {} here, but type shows it should have `session` key.
}
}
}
})
export const Route = createFileRoute("/api/chat")({
server: {
handlers: {
POST: async ({ context }) => {
// context is {} here, but type shows it should have `session` key.
}
}
}
})
The global authMiddleware actually runs on this route, but the context is lost. If I explicitly add the auth middleware to the createFileRoute's server object, the context is filled properly. However, in that case authMiddleware runs twice on this route. When I trace the innards, it looks like the handleServerRoute() function in createStartHandle.js doesn't accept the middleware's context and runs its own middleware chain which doesn't include the global middleware. Is it possible there's a bug here? It seems like if the global middleware runs, the context should be passed through.
GitHub
fix: pass server context to handler (#5601) · TanStack/router@41cc7e1
Co-authored-by: Samy Alzahrani <samyalzahrani@MacBook-Air.local>
0 Replies
No replies yetBe the first to reply to this messageJoin

Did you find this page helpful?