Static Server Function runs on every page visit, not only at build time
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 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
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start with the linked reproduction app, its README run instructions, and vite.config.ts, then inspect scripts/cache-static-assets-during-build/build.sh to understand the two-phase workaround. Trace when the Static Server Function runs during prerendering and page visits. Done means the function runs during the build and subsequent page loads serve cached output without executing it again.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- typescript, vite
- Domain
- backend, build-system
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Quiet
- Clarity
- Mostly clear
- Newbie friendliness
- 52/100