Skip to content

Start: global request middleware is executed multiple times for single request #5239

@andybons

Description

@andybons

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

  1. 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],
  };
});
  1. reference that middleware in a serverfn:
export const serverFn = createServerFn()
  .middleware([loggingMiddleware])
  .handler(async () => {
    return { foo: "bar" };
  });
  1. Call that serverfn in a beforeLoad context and fully reload the page
  2. 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

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions