Server functions that are stored in a separate file cause middlewares to throw error
Nobody has claimed this yet.
- Dominant language
- TypeScript
- Stars
- 15.1k
- Forks
- 1.9k
- Avg merge
- 1d 20h
- Merged PRs (30d)
- 143
Description
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
- 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
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Reproduce the production-only failure using server-fns/get-foo-bar.ts, middlewares/foo-bar-middleware, and routes/index.tsx, comparing development with the built server. Trace how the separate-file server function is registered and looked up when the middleware runs. Done means the /broken route works after a production build without the “Server function info not found” error.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- typescript, vite
- Domain
- api, backend, build-system
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Active
- Clarity
- Mostly clear
- Newbie friendliness
- 55/100