TanStackT
TanStack2mo ago
9 replies
dead-brown

Static server function not found

Hi, I am trying to use https://tanstack.com/start/latest/docs/framework/react/guide/static-server-functions#what-are-static-server-functions


But after trying it I see that client tries to get this data but receives not found

After build I see this json file in the /dist/client/__tsr/staticServerFnCache/someId.json

However this url outputs not found

http://localhost:3000/__tsr/staticServerFnCache/54459eae0cf4392adc3faaad6e0549fbaadcb877.json

import { createFileRoute, useLoaderData } from "@tanstack/react-router";
import { createServerFn } from "@tanstack/react-start";
import { staticFunctionMiddleware } from "@tanstack/start-static-server-functions";

const getBlogPosts = createServerFn({ method: "GET" })
    .middleware([staticFunctionMiddleware])
    .handler(async () => {
        console.log(
            "STATIC SERVER FUNCTION: generating blog posts at build time...",
        );

        // Simulate slow API
        await new Promise((r) => setTimeout(r, 5000));

        return [
            { title: "Blog", description: "Blog description" },
            { title: "Blog 2", description: "Blog description 2" },
            { title: "Blog 3", description: "Blog description 3" },
        ];
    });

export const Route = createFileRoute("/blog")({
    ssr: true,
    loader: async () => {
        const startTime = Date.now();
        console.log("RUNNING LOADER");

        const res = await getBlogPosts();
        const endTime = Date.now();

        console.log(`LOADER COMPLETED IN ${endTime - startTime}ms`);
        return res;
    },
    shouldReload: false,
    component: BlogPage,
});

function BlogPage() {
 return <div />
}
Was this page helpful?