-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
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