Cache Architecture
vinext’s caching system has two layers:1
CacheHandler (Storage Layer)
A pluggable key-value store matching Next.js 16’s
CacheHandler interface. Handles get/set operations with metadata.2
ISR Layer (Semantics)
Sits above the CacheHandler and implements stale-while-revalidate, background regeneration, and tag-based invalidation.
CacheHandler Interface
TheCacheHandler is a simple key-value store:
packages/vinext/src/shims/cache.ts
ISR Layer Implementation
The ISR layer wraps the CacheHandler:packages/vinext/src/server/isr-cache.ts
Stale-While-Revalidate
ISR returns stale content immediately while regenerating in the background:Deduplication
Multiple concurrent requests for the same stale key only trigger one regeneration:packages/vinext/src/server/isr-cache.ts
Pages Router ISR
Enable ISR by returningrevalidate from getStaticProps:
pages/blog/[slug].tsx
Fallback Modes
fallback: 'blocking':
- Request
/posts/2(not pre-rendered) - vinext renders the page on-demand
- Caches the result with
revalidateTTL - Future requests serve from cache with stale-while-revalidate
App Router ISR
Enable ISR with route segment config:app/blog/[slug]/page.tsx
Dynamic Rendering
Control when pages render:app/dashboard/page.tsx
Tag-Based Revalidation
Invalidate cache entries by tag:app/posts/[id]/page.tsx
app/actions.ts
Path-Based Revalidation
"use cache" Directive
Next.js 16 introduced "use cache" for granular caching:
Function-Level Caching
app/components/RecentPosts.tsx
File-Level Caching
app/lib/posts.ts
Cache Profiles
Define reusable cache durations:next.config.js
Cloudflare KV Cache Handler
For production on Cloudflare Workers, use the KV cache handler:worker/index.ts
Binding KV Namespace
Add towrangler.jsonc:
wrangler.jsonc
KV Implementation
packages/vinext/src/cloudflare/kv-cache-handler.ts
Custom Cache Handlers
Implement theCacheHandler interface for other backends:
lib/redis-cache.ts
Cache Key Generation
vinext generates cache keys from the router type and pathname:packages/vinext/src/server/isr-cache.ts
pages:/→ root pagepages:/blog/hello-world→ blog postapp:/dashboard/analytics→ app routeapp:__hash:a3f2b91c→ long pathname (hashed)
Next Steps
Deployment
Deploy to Cloudflare Workers with KV cache
Server Components
Learn about RSC rendering
Server Actions
Mutate data and revalidate cache
Architecture
Understand vinext’s architecture