Skip to content

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

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Fields

    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions