TanStack / TanStack/router

Static Server Function runs on every page visit, not only at build time

Open
#6,787 5 comments 5 reactions 0 assignees View on GitHub

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
  1. Install Docker (used to simulate production environment with mocking the CMS api)
  2. Clone the linked example app
  3. 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
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

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. 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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.