Which project does this relate to?#
Start
Describe the bug#
When global middleware is registered per the instructions in https://tanstack.com/start/latest/docs/framework/react/middleware#global-request-middleware and referenced by server function, it ends up being executed multiple times.
Your Example Website or App#
https://codesandbox.io/p/devbox/elated-ioana-8mjsp4
Steps to Reproduce the Bug or Issue#
- Add a middleware function to the request middleware in
start.ts:
export const loggingMiddleware = createMiddleware().server(async ({ next }) => {
const request = getRequest();
console.log("request", request.url, crypto.randomUUID());
return next();
});
export const startInstance = createStart(() => {
return {
requestMiddleware: [loggingMiddleware],
};
});
- reference that middleware in a serverfn:
export const serverFn = createServerFn()
.middleware([loggingMiddleware])
.handler(async () => {
return { foo: "bar" };
});
- Call that serverfn in a beforeLoad context and fully reload the page
- Notice that three requests are logged with the same request url
Expected behavior#
I expect a single log per request url
Screenshots or Videos#
No response
Platform#
- Router / Start Version: 1.132.7
Additional context#
No response
Based on my experience it's not even related to global middleware or request middleware. Even standard server function middleware is called two times during single server function call.
What I noticed additionally is that it only happens during SSR request. When server function is called from client, middleware is correctly called only once.
Adding following code to app created using
npm create @tanstack/start@latest:causes following output during SSR
and during client navigation