In this pull request you can add `static: true` to any server function to have it cache the result at prerender time and behave like a static SPA, just like React Static!
https://github.com/TanStack/router/pull/2926
https://github.com/TanStack/router/pull/2926
Comments
can you use createServerFn without SSR ? I don't really benefit from SSR (internal dashboard that has a BFF already) but I still would love to skip the boilerplate/glue-code of having to create a dedicated endpoint+mapping that in a tanstack query factory
- Cached during prerender
- Fetches JSON at built runtime, so you can preload, too!
- Takes advantage of new middleware
- Serializes complex values and even errors!
For bonus points, you could obey cache-control semantics and syntax. In fact cache-control with a pluggable storage layer (FS, S3, Redis) would be an awesome middleware. Happy to contribute if you want it.
cache-control: public, immutable
With my idea, you could do
cache-control: public, max-age=86400, stale-while-revalidate=600
and the server would rebuild the page once per day
- Default static/dynamic
- Functional static/dynamic
- Imperative static/dynamic
Essentially, this means you can decide on the fly if you want a server fn invocation to be static. Just needs to be consistent between prerender and runtime :)
React Router based it on (create)MemoryRouter, but Ryan (IIRC) confirmed to me that they'd drop native in v7. I'd love to see a viable alternative to the Navigator paradigm in RN routing.
- Solid
- Preact
- React Native
- Svelte
So it's coming soon!