Static Server Function runs on every page visit, not only at build time
Which project does this relate to?
Start
Describe the bug
When I build a route using the staticFunctionMiddleware middleware with a Static Server Function, I encounter the following problem. The Static Server Function is executed every time a page is visited. This means that any static data you want to cache from a CMS, for example, is fetched again each time the page is opened in the browser. This behaviour is different from that described in the documentation:
Static server functions are server functions that are executed at build time and cached as static assets when using prerendering/static-generation
And I activated pre-rendering like mentioned in the docs, see my vite.config.ts:
export default defineConfig({
plugins: [
tanstackStart({ prerender: { enabled: true } }),
nitro({
publicAssets: [
// This is a temporary fix to https://github.com/TanStack/router/issues/5368
{
dir: "dist/client/__tsr",
baseURL: "/__tsr",
maxAge: 0
}
]
}),
react()
]
});
Your Example Website or App
https://github.com/fullheart/tanstack-start-static-server-function-build-workaround
Steps to Reproduce the Bug or Issue
- Install Docker (used to simulate production environment with mocking the CMS api)
- Clone the linked example app
- Run commands defined in example app's README: https://github.com/fullheart/tanstack-start-static-server-function-build-workaround/blob/main/README.md#run-the-reproduction-of-the-problem
Expected behavior
I expect that the Static Server Function is only executed during the build process. After the build, TanStack Start should serve the cached output of the Static Server Function and should never execute the Static Server Function on page load.
Screenshots or Videos
More details in the README.
Platform
- Router / Start Version: 1.161.3
- OS: Linux
- Browser: Brave
- Browser Version: https://www.whatsmybrowser.org/b/R1RT0
- Bundler: vite
- Bundler Version: 7.3.1
Additional context
I can work around the problem using this script for a two-phase build: https://github.com/fullheart/tanstack-start-static-server-function-build-workaround/blob/main/scripts/cache-static-assets-during-build/build.sh
A few days ago, I asked for help on Discord: https://discord.com/channels/719702312431386674/1475582650851790848
@fullheart Hi, I encounter a similar issue in my project, and I notice that the tanstack prerender data is generated in dist after nitro build finished, and the configuration below only works when dist already have the prerender data.
nitro({
publicAssets: [
// This is a temporary fix to https://github.com/TanStack/router/issues/5368
{
dir: "dist/client/__tsr",
baseURL: "/__tsr",
maxAge: 0
}
]
}),
So we may need to manually copy the prerender data into the nitro output directory : ( I notice that TanstackStart provide a hook that works when prerender success, here is the code. I wish it can be helpful to you !
import path from "node:path/posix";
import { nitro } from "nitro/vite";
// ... other code
tanstackStart({
prerender: {
enabled: true,
crawlLinks: false,
onSuccess: async () => {
const fs = await import("node:fs/promises");
const src = path.resolve("dist/client/__tsr");
const dest = path.resolve(".output/public/__tsr");
try {
await fs.cp(src, dest, { recursive: true });
console.log("[fix] Copied __tsr after prerender");
} catch {
console.warn("[fix] __tsr not found");
}
},
},
}),
I notice that TanstackStart provide a hook that works when prerender success, here is the code. I wish it can be helpful to you !
@floatDreamWithSong Wow, thanks for your workaround. I like your solution more than mine. I'll give it a try.
@tannerlinsley Does your PR https://github.com/TanStack/router/pull/6940 also solve the problem I described in this ticket?
This problem seems to have been resolved in the latest version ("@tanstack/react-start": "^1.167.42").
I've removed my workarounds and it's working; see the sample app here: https://github.com/fullheart/tanstack-start-issue-selective-ssg/tree/main/ssg-working
But I have still a problem with SSG. see new issue: https://github.com/TanStack/router/issues/7239
Correcting my "seems resolved" above: only the serving side was fixed. The handler still executes at request time on current versions (1.167.29).
Reproducer (v2 of the original, no Docker): https://github.com/fullheart/tanstack-start-issue-6787-v2
The mock CMS only runs during npm run build. Afterwards:
curl -i http://localhost:3000/does-not-exist
# 500 — and the server log prints:
[6787] handler executed (NODE_ENV=production, TSS_CLIENT_OUTPUT_DIR=unset)
Cause: .server() calls ctx.next() unconditionally — TSS_CLIENT_OUTPUT_DIR only gates writing the cache, and only the client half ever reads it. So every server-side request executes the handler; even the prerendered /page answers 500 once the CMS is gone.
Here's the fix we've been running in production, as a patch-package file — save it as patches/@tanstack+start-static-server-functions+1.167.29.patch and run npx patch-package:
diff --git a/node_modules/@tanstack/start-static-server-functions/dist/esm/staticFunctionMiddleware.js b/node_modules/@tanstack/start-static-server-functions/dist/esm/staticFunctionMiddleware.js
index c6fecf9..d65905f 100644
--- a/node_modules/@tanstack/start-static-server-functions/dist/esm/staticFunctionMiddleware.js
+++ b/node_modules/@tanstack/start-static-server-functions/dist/esm/staticFunctionMiddleware.js
@@ -76,6 +76,16 @@ var staticFunctionMiddleware = createMiddleware({ type: "function" }).client(asy
}
return ctx.next();
}).server(async (ctx) => {
+ // PATCHED — https://github.com/TanStack/router/issues/6787
+ // "Static server functions are executed at build time" is the whole contract, but upstream
+ // runs the handler on every live request too. Only the build sets TSS_CLIENT_OUTPUT_DIR, so
+ // a call arriving without it is a request for an address the build never rendered: answer
+ // nothing and let the route decide, rather than execute a handler that was never meant to
+ // run at request time.
+ if (process.env.NODE_ENV === "production" && !process.env.TSS_CLIENT_OUTPUT_DIR) return {
+ result: void 0,
+ context: ctx.context
+ };
const response = await ctx.next();
if (process.env.NODE_ENV === "production" && process.env.TSS_CLIENT_OUTPUT_DIR) await addItemToCache({
functionId: ctx.serverFnMeta.id,
With that, /does-not-exist answers 404 with zero handler executions. One thing to weigh if this becomes the default: it removes the current silent live fallback — which matches the docs, but is a behaviour change.
I'm using this patch, which isn't necessarily correct, but works for my use case.
diff --git a/dist/esm/staticFunctionMiddleware.js b/dist/esm/staticFunctionMiddleware.js
index 3f9b7db31b55634034ac348ef8812b62aa0ff922..458489e3a381c2751227752d12abe6b9762e2e54 100644
--- a/dist/esm/staticFunctionMiddleware.js
+++ b/dist/esm/staticFunctionMiddleware.js
@@ -49,14 +49,22 @@ async function addItemToCache({ functionId, data, response }) {
await fs.writeFile(filePath, stringifiedResult, "utf-8");
}
var fetchItem = async ({ data, functionId }) => {
- const url = await getStaticCacheUrl({
- functionId,
- hash: jsonToFilenameSafeString(data)
- });
- return fetch(url, { method: "GET" }).then((r) => r.json()).then((d) => fromJSON(d, { plugins: getSerovalPlugins(defaultSerovalDeserializerPlugins) }));
+ const url = await getStaticCacheUrl({
+ functionId,
+ hash: jsonToFilenameSafeString(data)
+ });
+ if (typeof document !== "undefined") {
+ return fetch(url, { method: "GET" }).then((r) => r.json()).then((d) => fromJSON(d, { plugins: getSerovalPlugins(defaultSerovalDeserializerPlugins) }));
+ } else {
+ try {
+ return await fs.readFile(path.join('./public', url), 'utf-8').then(v => JSON.parse(v)).then((d) => fromJSON(d, { plugins: getSerovalPlugins(defaultSerovalDeserializerPlugins) }));
+ } catch {
+ return null;
+ }
+ }
};
var staticFunctionMiddleware = createMiddleware({ type: "function" }).client(async (ctx) => {
- if (process.env.NODE_ENV === "production" && typeof document !== "undefined") {
+ if (process.env.NODE_ENV === "production") {
const response = await fetchItem({
functionId: ctx.serverFnMeta.id,
data: ctx.data
For some reason, the package chooses to ignore the existing assets during SSR, which causes the call into the server middleware. This, in turn, causes an uncoditional call into the server function.
I only want to run the server function during the build and prerender step and never at runtime. The patch above helps me get that behaviour.