Skip to content

Server functions that are stored in a separate file cause middlewares to throw error #7213

In progress

Description

@petervmeijgaard

Which project does this relate to?

Start

Describe the bug

When a middleware is using a server function that's placed in a different file, using said middleware in a different server function will cause the following error:

Server function info not found for [ID]

It's working fine when the server function is defined in the same file as the middleware

Your Example Website or App

Steps to Reproduce the Bug or Issue

  1. Add server function in a new file (i.e. ./server-fns/get-foo-bar.ts)
import { createServerFn } from "@tanstack/react-start";

export const getFooBar = createServerFn({ method: "GET" }).handler(
  async () => {
    return { foo: "bar" };
  },
);
  1. Use this server function in a middleware that's stored in a separate file (i.e. ./middlewares/foo-bar-middleware)
import { createMiddleware } from "@tanstack/react-start";

import { getFooBar } from "#/server-fns/get-foo-bar";

export const fooBarMiddleware = createMiddleware({ type: "function" }).server(
  async ({ next }) => {
    const context = await getFooBar();

    return next({ context });
  },
);
  1. Use the middleware in a server function that's used within a route (i.e. ./routes/index.tsx)
import { createFileRoute } from "@tanstack/react-router";

import { fooBarMiddleware } from "#/middleware/foo-bar-middleware";
import { createServerFn } from "@tanstack/react-start";

const getFooBar = createServerFn({ method: "GET" })
  .middleware([fooBarMiddleware])
  .handler(async ({ context }) => {
    console.log(context);

    return context.foo;
  });

export const Route = createFileRoute("/broken")({
  component: RouteComponent,
  loader: async () => {
    const foo = await getFooBar();

    return { foo };
  },
});

function RouteComponent() {
  const foo = Route.useLoaderData({
    select: (ctx) => ctx.foo,
  });

  return <h1>{foo}</h1>;
}
  1. Start the development server and visit the route
  2. See that the page is working as expected
  3. Build the application
  4. Start the production server and visit the same page
  5. See that the page is broken and the following error is shown:

Server function info not found for [ID]

Expected behavior

As a user, I expected that this would work and that no error was thrown. However, this results in an production breaking exception

Screenshots or Videos

Image

Platform

  • Router / Start Version: 1.168.22
  • OS: Linux
  • Browser: Zen (Firefox based)
  • Browser Version: 1.19.8b (Firefox 149.0.2) (64-bit)
  • Bundler: Vite
  • Bundler Version: 8.0.8

Additional context

No response

Activity

  1. John-Hejzlar commented on Apr 26, 2026

    @John-Hejzlar

    Same error, my last working versions are:

    "@tanstack/react-start": "1.167.16",
    "@tanstack/react-router": "1.168.10"
    
  2. noubase commented on Apr 30, 2026

    @noubase

    Got the same issue, even without using middleware. Just call:

    void queryClient.ensureQueryData(queryOptions({
      queryKey: ['a'],
      queryFn: testServerFn,
      staleTime: 100000,
    }))

    in a loader for any route.

    export const testServerFn = createServerFn({ method: 'GET' }).handler(async () => {
      const blockId = await getProductUpsaleBlock({ data: { productId: 12 } })
      return [blockId]
    })

    This testServerFn simply calls another server function internally, and you’ll get the same error.

    The most annoying part is that it works fine in development.

  3. noubase commented on Apr 30, 2026

    @noubase

    ├── @tanstack/[email protected]
    ├── @tanstack/[email protected]
    ├── @tanstack/[email protected]
    ├── @tanstack/[email protected]
    ├── @tanstack/[email protected]
    ├── @tanstack/[email protected]
    ├── @tanstack/[email protected]
    ├── @tanstack/[email protected]
    ├── @tanstack/[email protected]
    ├── @tanstack/[email protected]
    ├── @tanstack/[email protected]

  4. noubase commented on Apr 30, 2026

    @noubase

    Further investigation reveals that you should never call one server function from another, even if the documentation suggests otherwise

  5. aJV99 commented on May 11, 2026

    @aJV99

    I doubt it since the last comment was two weeks ago but do we know if any fixes are being worked on? I am wondering if I should consider pinning my versions or make long term fixes moving away from server functions calling one another?

  6. schiller-manuel commented on May 11, 2026

    @schiller-manuel
    Collaborator

    we are working on this yes

  7. aJV99 commented on May 11, 2026

    @aJV99

    After some investigation, looks like the same trigger here. The createServerFn defined in one file, called from a route loader that internally calls another createServerFn from a different file. Production-only; vite dev is fine.

    For me the symptom is a silent hang rather than the thrown "Server function info not found". The resolver's dynamic import for the cross-file handler chunk never resolves, so the loader sits forever and the page never renders.

    Pinning to the versions @John-Hejzlar mentioned didn't help. What did: downgrading Vite from 8 to 7 (vite@^7.3.3, @vitejs/plugin-react@^5.2.0). Every @tanstack/react-start I tried hangs on Vite 8 and works on Vite 7, which points at this maybe being Vite-8 regression rather than a TanStack-version one - I'm not fully sure.

    Node 24, Linux, node-server Nitro preset.

  8. added a commit that references this issue on Aug 3, 2026
    b84d6c0
  9. tomh4 commented on Sep 3, 2026

    @tomh4

    I have this issue as well, calling a serverfunction from the middleware

  10. John-Hejzlar commented on Oct 27, 2026

    @John-Hejzlar

    I reproduced this on @tanstack/[email protected], @tanstack/[email protected], and Vite 8.3.4 in a production Docker build.

    What worked for me was moving shared logic out of createServerFn handlers into createServerOnlyFn helpers. Middleware now calls those helpers directly. Where the browser still needs access, I keep a createServerFn wrapper that calls the same helper.

  11. tomh4 commented on Nov 2, 2026

    @tomh4

    @schiller-manuel tried on the latest stable version, still having this issue

  12. petervmeijgaard commented on Nov 2, 2026

    @petervmeijgaard
    Author

    @tomh4 please add a 👍🏻 reaction to the original issue if you're still experiencing this issue. This will indicate that more than 3 people are experiencing this and would like to have this fixed 😄

  13. tomh4 commented on Nov 2, 2026

    @tomh4
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions