Skip to content

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

@fullheart

Description

@fullheart

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions