feat(prerender): seed KV cache with pre-rendered routes at deploy time#734
Closed
raed04 wants to merge 1 commit intocloudflare:mainfrom
Closed
feat(prerender): seed KV cache with pre-rendered routes at deploy time#734raed04 wants to merge 1 commit intocloudflare:mainfrom
raed04 wants to merge 1 commit intocloudflare:mainfrom
Conversation
commit: |
554d801 to
43295ba
Compare
Adds automatic KV cache population during `vinext deploy`. After pre-rendering completes and before wrangler deploys the worker, pre-rendered HTML/RSC files are uploaded to Workers KV via the Cloudflare bulk REST API so pages are cache HITs from the first request. - New module: seed-kv-cache.ts reads vinext-prerender.json, constructs correct ISR cache keys via isrCacheKey(), base64-encodes RSC data, and uploads in batches of 10,000 pairs - Integrates as Step 6c in deploy.ts between prerender and wrangler deploy - Automatic when CLOUDFLARE_API_TOKEN is set and VINEXT_CACHE KV namespace exists in wrangler config; opt-out via --no-seed-cache - Non-fatal: seeding failures log a warning but don't block deployment - Security: path traversal guard, TOCTOU-safe file reads, capped error messages, Cloudflare success:false response checking - Static routes omit expiration_ttl to match runtime KVCacheHandler behavior - 27 unit tests covering key format, binary RSC, TTL clamping, unicode paths, batch splitting, API error handling, and mixed manifests Closes cloudflare#562
43295ba to
fc51d75
Compare
Contributor
|
Hey bro, I've already attempted this #670 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
vinext deployso pre-rendered pages are cache HITs from the first requestvinext-prerender.jsonand uploads HTML/RSC entries to Workers KV via the Cloudflare bulk REST APIKVCacheHandler.get():cache:app:<buildId>:<pathname>:htmlHow it works
vinext-prerender.jsonmanifest and pre-rendered HTML/RSC files fromdist/server/prerendered-routes/isrCacheKey()(same function the runtime uses)KVCacheHandlerserialization formatAutomatic when:
CLOUDFLARE_API_TOKENenv var is set andVINEXT_CACHEKV namespace exists in wrangler config.Opt-out:
vinext deploy --no-seed-cacheNon-fatal: Seeding failures log a warning but never block deployment.
Key design decisions
cache:app:<buildId>:<pathname>:htmlmatches whatKVCacheHandler.get()reads at runtime (unlike TPR which usescache:<pathname>— a known pre-existing bug)expiration_ttl— matches runtimeKVCacheHandler.set()behavior (no expiry for static pages)10x revalidateTTL clamped to[60s, 30d]Security
prerenderDirreadFileSync(noexistsSync+readFileSyncrace)success: falseresponses (HTTP 200 with semantic errors)Files
packages/vinext/src/cloudflare/seed-kv-cache.tspackages/vinext/src/deploy.ts--no-seed-cacheflagpackages/vinext/src/cli.tsnoSeedCacheflag to deploy optionstests/seed-kv-cache.test.tsTest plan
isrCacheKey()revalidateAt, noexpiration_ttlrevalidateAt, TTL clamping (min 60s)revalidate=0treated as staticsuccess:falserejectionvp fmt --checkCloses #562