Repository navigation
Description
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
Activity
floatDreamWithSong commentedon Mar 17, 2026
@fullheart Hi, I encounter a similar issue in my project, and I notice that the tanstack prerender data is generated in
distafter nitro build finished, and the configuration below only works whendistalready have the prerender data.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 !
fullheart commentedon Mar 18, 2026
@floatDreamWithSong Wow, thanks for your workaround. I like your solution more than mine. I'll give it a try.
fullheart commentedon Mar 18, 2026
@tannerlinsley Does your PR #6940 also solve the problem I described in this ticket?
fullheart commentedon Apr 22, 2026
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: #7239
fullheart commentedon Aug 19, 2026
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:Cause:
.server()callsctx.next()unconditionally —TSS_CLIENT_OUTPUT_DIRonly gates writing the cache, and only the client half ever reads it. So every server-side request executes the handler; even the prerendered/pageanswers 500 once the CMS is gone.Here's the fix we've been running in production, as a
patch-packagefile — save it aspatches/@tanstack+start-static-server-functions+1.167.29.patchand runnpx patch-package:With that,
/does-not-existanswers 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.Ugzuzg commentedon Oct 4, 2026
I'm using this patch, which isn't necessarily correct, but works for my use case.
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.