Skip to content

refactor(www): swap static server fns for CDN cache headers - #235

Merged
mehdibha merged 4 commits into
mainfrom
claude/peaceful-ritchie-4e28a6
Jun 12, 2026
Merged

mehdibha merged 4 commits into
mainfrom
claude/peaceful-ritchie-4e28a6

Conversation

@mehdibha

@mehdibha mehdibha commented Jun 11, 2026 •

Copy link
Copy Markdown
Owner

Why

@tanstack/start-static-server-functions is officially experimental ("Static Server Functions are experimental!" in the TanStack docs), thinly maintained (~0.06% of Start's downloads, no README/tests, open core-mechanism bugs #7479, #6787 with no maintainer replies), and our integration had a live bug: the baked __tsr JSONs are served with cache-control: max-age=31557600, immutable while their filenames are input-addressed (slug-based, not content-based), so returning visitors kept year-old docs data on client-side navigations after content deploys. Its middleware also has no fallback on cache miss — a client-side nav to a removed/unbaked URL threw into the error boundary instead of rendering NotFound. This repo already needed two workarounds for its build plumbing (a pnpm patch, the __tsr copy hack).

Full assessment with sources: docs/research/2026-06-11-static-server-functions.md.

What

  • serverLoader (docs page data) and getPageTree are now plain GET server functions that set Cache-Control: public, max-age=0, must-revalidate, s-maxage=31536000 on their success path only — Vercel's CDN caches the response and purges on every deploy, so data is always fresh; not-found responses stay uncached. This is the same pattern tanstack.com uses for its own docs.
  • Delete www/src/lib/github.ts — dead code, nothing imports it (was the third staticFunctionMiddleware consumer).
  • Drop the @tanstack/start-static-server-functions dependency.
  • Remove the obsolete __tsr copy step from build:postprocess (prerender writes directly into .vercel/output/static since fix: write static server function cache to correct output directory with Nitro TanStack/router#6940; verified on a current build).

Prerendering is unaffected — direct visits keep serving build-time static HTML; only the data fetch behind client-side navigations changes (static JSON file → CDN-cached server fn response).

Verification

  • pnpm check, pnpm typecheck, full pnpm build pass; prerender emits all docs pages, no __tsr output remains.
  • On the built app (vite preview): client-side navs Button → Select → Table → Tooltip render correctly with one /_serverFn/ GET each; success responses carry the cache header, not-found responses carry none and serialize isNotFound in-band (NotFound UI restored on bad client-side navs).
  • Before/after CDN measurements on the Vercel preview deployment: appended to the research doc — steady-state CDN-HIT parity with the static JSONs; the cost is one ~0.3–0.5 s warm invocation per page per region per deploy (~2 s cold), paid by the first visitor.

🤖 Generated with Claude Code

Replace the experimental @tanstack/start-static-server-functions
middleware with plain GET server functions that set Cache-Control on
their success path; Vercel's CDN caches responses until the next deploy
purge. Also delete the unused lib/github.ts and the obsolete __tsr copy
step in build:postprocess. Rationale and evidence in
docs/research/2026-06-11-static-server-functions.md.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
@github-actions github-actions Bot added documentation Improvements or additions to documentation area: www The dotui.org app: site, landing, docs pages labels Jun 11, 2026
@changeset-bot

changeset-bot Bot commented Jun 11, 2026 •

Copy link
Copy Markdown

⚠️ No Changeset found

Latest commit: 899dbe4

Merging this PR will not cause a version bump for any packages. If these changes should not result in a new version, you're good to go. If these changes should result in a version bump, you need to add a changeset.

Click here to learn what changesets are, and how to add one.

Click here if you're a maintainer who wants to add a changeset to this PR

@vercel

vercel Bot commented Jun 11, 2026 •

Copy link
Copy Markdown

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated (UTC)
dotui Ready Ready Preview, Comment Jun 12, 2026 12:16am

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
@mehdibha

Copy link
Copy Markdown
Owner Author

Performance: measured before/after

TTFB via curl on a pre-warmed connection (mimics a client-side nav reusing the page's HTTP/2 connection), 8 runs per URL across 9 docs pages + the page tree, single vantage point (FRA edge). Before = production dotui.org (static server functions). After = this PR's preview deployment (dotui-h3sj4t9fm). Direct visits serve the same prerendered HTML in both architectures — only the client-side-navigation data fetch changed.

Data fetch per client-side docs nav Before (static JSON file) After (server fn + s-maxage)
Steady state (CDN HIT), median 57–91 ms 66–114 ms
First request per page/region/deploy n/a (always static) 315–466 ms (warm function)
Worst case: fresh deploy, cold function n/a 1.7–2.1 s (first two invocations only)
Payload size (loader page) 625 B 635 B
Direct visit HTML (sanity) 63 ms median, HIT 77 ms median, HIT

Verified on the deployed preview:

  • x-vercel-cache: MISS exactly once per URL, then HIT with age advancing; the browser receives cache-control: public, max-age=0, must-revalidate — the year-long immutable browser cache on input-addressed JSONs (stale docs after deploys) is gone.
  • notFound responses carry no cache header, stay uncached on repeat, and serialize isNotFound in-band — client-side navs to bad URLs render NotFound instead of the error boundary.
  • Response bodies content-equivalent to the old baked JSONs (same fields incl. rawContent).

Bottom line: steady-state nav latency is at parity; the cost is one ~0.3–0.5 s invocation per docs page per region per deploy (≈2 s if cold), paid by the first visitor only — in exchange for always-fresh data, a working not-found path, and dropping the experimental dependency plus its build hacks. Full methodology and caveats in docs/research/2026-06-11-static-server-functions.md.

🤖 Generated with Claude Code

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
Review follow-ups: the getPageTree comment now states that the header lands
on 2xx HTML documents during SSR (revisit if per-request SSR lands under
_app); the research doc records why the shipped header omits the sketch's
stale-while-revalidate=60.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
@mehdibha
mehdibha merged commit e0ca5b1 into main Jun 12, 2026
10 checks passed
@mehdibha
mehdibha deleted the claude/peaceful-ritchie-4e28a6 branch June 12, 2026 13:05

This branch was successfully deployed

1 active deployment
Preview — 899dbe4a Deployed Jun 12, 2026 by vercel[bot]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area: www The dotui.org app: site, landing, docs pages documentation Improvements or additions to documentation

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant