Server functions that are stored in a separate file cause middlewares to throw error #7213
Send to my agentWhich 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#
- 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" };
},
);
- 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 });
},
);
- 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>;
}
- Start the development server and visit the route
- See that the page is working as expected
- Build the application
- Start the production server and visit the same page
- 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#
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
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
testServerFnsimply calls another server function internally, and you’ll get the same error.The most annoying part is that it works fine in development.
├── @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
Further investigation reveals that you should never call one server function from another, even if the documentation suggests otherwise
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?
we are working on this yes
After some investigation, looks like the same trigger here. The
createServerFndefined in one file, called from a route loader that internally calls anothercreateServerFnfrom a different file. Production-only;vite devis 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-startI 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.
I have this issue as well, calling a serverfunction from the middleware
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.
@schiller-manuel tried on the latest stable version, still having this issue
@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 😄
@petervmeijgaard done! :)
Sheraff(GitHub profile, opens in a new tab) linked this to #8817
Sign in to join this conversation.
Same error, my last working versions are: