Skip to content

feat: lazy per-route cache seeding for Workers#653

Open
NathanDrake2406 wants to merge 2 commits intocloudflare:mainfrom
NathanDrake2406:feat/seed-workers-memory-cache
Open

feat: lazy per-route cache seeding for Workers#653
NathanDrake2406 wants to merge 2 commits intocloudflare:mainfrom
NathanDrake2406:feat/seed-workers-memory-cache

Conversation

@NathanDrake2406
Copy link
Contributor

Summary

  • On Workers with MemoryCacheHandler (the default when KV is not configured), pre-rendered routes now lazily seed the memory cache on first request via env.ASSETS.fetch()
  • Seeding is per-route (not eager startup) to respect Workers' ephemeral isolate model and 128MB memory limit
  • Concurrent cold hits for the same route are deduped via an in-flight promise map

Pre-requisite

Depends on #645 for the manifest format changes (buildId, router, trailingSlash fields).

How it works

  1. Deploy time (deploy.ts): After prerendering, copies vinext-prerender.json and HTML/RSC files to dist/client/__prerender/ so they're deployed as static assets
  2. Runtime (seed-cache-workers.ts): On each request, checks if the route has prerendered data. On first miss, fetches HTML/RSC from the assets binding and populates MemoryCacheHandler. Subsequent requests in the same isolate get cache HITs
  3. Generated worker entry: Calls seedRouteFromAssets() before delegating to the RSC handler

Changes

File Change
src/server/seed-cache-workers.ts New — lazy per-route seeding with manifest caching + concurrent dedup
src/deploy.ts Copy prerender files to assets dir + add seeding call to generated worker entry
package.json Export ./server/seed-cache-workers
tests/seed-cache-workers.test.ts 9 unit tests

Scope

This is Workers memory stub support only, not the final durable caching story. For persistent caching, users should use KVCacheHandler (opt-in) with TPR for deploy-time KV population. Issue #562 tracks the generalised remote cache seeding.

Ref #561

Test plan

  • 9 unit tests: basic seeding, index route, dynamic routes, missing manifest, non-prerendered routes, already-cached no-op, concurrent dedup, manifest caching, missing RSC graceful degradation
  • 216 deploy tests pass unchanged
  • Type-aware lint clean

@pkg-pr-new
Copy link

pkg-pr-new bot commented Mar 22, 2026

Open in StackBlitz

npm i https://pkg.pr.new/vinext@653

commit: 8011d47

@NathanDrake2406 NathanDrake2406 force-pushed the feat/seed-workers-memory-cache branch from 3e588f6 to 1b23050 Compare March 23, 2026 01:56
NathanDrake2406 added a commit to NathanDrake2406/vinext that referenced this pull request Mar 23, 2026
- Only seed routes with router: "app" — Pages Router uses different
  keys (pages:...) and value shape (kind: "PAGES"), so seeding them
  as APP_PAGE entries would be unreachable at runtime.
- Routes without a router field are skipped defensively (pre-cloudflare#653
  manifests lack this field, but also lack buildId so populateKV
  would already skip).
- Remove --app-prefix CLI flag — it writes prefixed keys without
  wiring the same prefix into the generated runtime, creating
  silently unreadable cache entries. Keep appPrefix on the
  programmatic PopulateKVOptions for advanced users who configure
  both sides manually.
NathanDrake2406 added a commit to NathanDrake2406/vinext that referenced this pull request Mar 23, 2026
- Only seed routes with router: "app" — Pages Router uses different
  keys (pages:...) and value shape (kind: "PAGES"), so seeding them
  as APP_PAGE entries would be unreachable at runtime.
- Routes without a router field are skipped defensively (pre-cloudflare#653
  manifests lack this field, but also lack buildId so populateKV
  would already skip).
- Remove --app-prefix CLI flag — it writes prefixed keys without
  wiring the same prefix into the generated runtime, creating
  silently unreadable cache entries. Keep appPrefix on the
  programmatic PopulateKVOptions for advanced users who configure
  both sides manually.
NathanDrake2406 added a commit to NathanDrake2406/vinext that referenced this pull request Mar 23, 2026
- Only seed routes with router: "app" — Pages Router uses different
  keys (pages:...) and value shape (kind: "PAGES"), so seeding them
  as APP_PAGE entries would be unreachable at runtime.
- Routes without a router field are skipped defensively (pre-cloudflare#653
  manifests lack this field, but also lack buildId so populateKV
  would already skip).
- Remove --app-prefix CLI flag — it writes prefixed keys without
  wiring the same prefix into the generated runtime, creating
  silently unreadable cache entries. Keep appPrefix on the
  programmatic PopulateKVOptions for advanced users who configure
  both sides manually.
On Cloudflare Workers with MemoryCacheHandler (the default when KV is
not configured), pre-rendered routes now seed the memory cache on first
request via the assets binding instead of triggering a full re-render.

- Add seed-cache-workers.ts with lazy per-route seeding, manifest
  caching per isolate, and concurrent request dedup
- Copy prerender files to dist/client/__prerender/ during deploy so
  they're available via env.ASSETS.fetch()
- Add seeding call to generated App Router worker entry
- 9 unit tests covering seeding, dedup, manifest caching, and
  graceful degradation
The Node.js and Workers seed-cache modules duplicated manifest types,
revalidateCtx, and cache value construction. Extract to
seed-cache-shared.ts to prevent drift between the two strategies.

Also fixes stale JSDoc referencing "manifestPromise" (renamed to
"loadedPromise").
@NathanDrake2406 NathanDrake2406 force-pushed the feat/seed-workers-memory-cache branch from 1b23050 to 8011d47 Compare March 23, 2026 15:29
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant