Skip to content
Source
Sign inSign up
TanStack/router

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

Send to my agent
Openpetervmeijgaard(GitHub profile, opens in a new tab) opened this issue 12 comments

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

  1. Same error, my last working versions are:

    "@tanstack/react-start": "1.167.16",
    "@tanstack/react-router": "1.168.10"
  2. 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. ├── @tanstack/devtools-vite@0.6.0 ├── @tanstack/eslint-config@0.4.0 ├── @tanstack/react-devtools@0.10.2 ├── @tanstack/react-query-devtools@5.100.1 ├── @tanstack/react-query@5.100.1 ├── @tanstack/react-router-devtools@1.166.13 ├── @tanstack/react-router-ssr-query@1.166.11 ├── @tanstack/react-router@1.168.26 ├── @tanstack/react-start@1.167.52 ├── @tanstack/router-plugin@1.167.29 ├── @tanstack/zod-adapter@1.166.9

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

  5. 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. 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.

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

  8. I reproduced this on @tanstack/react-start@1.169.3, @tanstack/react-router@1.170.45, 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.

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

  10. @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 😄

  11. @petervmeijgaard done! :)

Sign in to join this conversation.