TanStack / TanStack/router

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

Open
#7,213 8 comments 3 reactions 0 assignees View on GitHub

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

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. 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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.