@rangojs/router 0.0.0-experimental.39 → 0.0.0-experimental.3b1deca8

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (89) hide show
  1. package/dist/bin/rango.js +8 -3
  2. package/dist/vite/index.js +292 -204
  3. package/package.json +1 -1
  4. package/skills/cache-guide/SKILL.md +32 -0
  5. package/skills/caching/SKILL.md +45 -4
  6. package/skills/loader/SKILL.md +53 -43
  7. package/skills/parallel/SKILL.md +126 -0
  8. package/skills/route/SKILL.md +31 -0
  9. package/skills/router-setup/SKILL.md +52 -2
  10. package/skills/typesafety/SKILL.md +10 -0
  11. package/src/browser/debug-channel.ts +93 -0
  12. package/src/browser/event-controller.ts +5 -0
  13. package/src/browser/navigation-bridge.ts +1 -5
  14. package/src/browser/navigation-client.ts +84 -27
  15. package/src/browser/navigation-transaction.ts +11 -9
  16. package/src/browser/partial-update.ts +50 -9
  17. package/src/browser/prefetch/cache.ts +57 -5
  18. package/src/browser/prefetch/fetch.ts +30 -21
  19. package/src/browser/prefetch/queue.ts +92 -20
  20. package/src/browser/prefetch/resource-ready.ts +77 -0
  21. package/src/browser/react/Link.tsx +9 -1
  22. package/src/browser/react/NavigationProvider.tsx +32 -3
  23. package/src/browser/rsc-router.tsx +109 -57
  24. package/src/browser/scroll-restoration.ts +31 -34
  25. package/src/browser/segment-reconciler.ts +6 -1
  26. package/src/browser/server-action-bridge.ts +12 -0
  27. package/src/browser/types.ts +17 -1
  28. package/src/build/route-types/router-processing.ts +12 -2
  29. package/src/cache/cache-runtime.ts +15 -11
  30. package/src/cache/cache-scope.ts +48 -7
  31. package/src/cache/cf/cf-cache-store.ts +453 -11
  32. package/src/cache/cf/index.ts +5 -1
  33. package/src/cache/document-cache.ts +17 -7
  34. package/src/cache/index.ts +1 -0
  35. package/src/cache/taint.ts +55 -0
  36. package/src/context-var.ts +72 -2
  37. package/src/debug.ts +2 -2
  38. package/src/deps/browser.ts +1 -0
  39. package/src/route-definition/dsl-helpers.ts +32 -7
  40. package/src/route-definition/helpers-types.ts +6 -5
  41. package/src/route-definition/redirect.ts +2 -2
  42. package/src/route-map-builder.ts +7 -1
  43. package/src/router/find-match.ts +4 -2
  44. package/src/router/handler-context.ts +31 -8
  45. package/src/router/intercept-resolution.ts +2 -0
  46. package/src/router/lazy-includes.ts +4 -1
  47. package/src/router/loader-resolution.ts +7 -1
  48. package/src/router/logging.ts +5 -2
  49. package/src/router/manifest.ts +9 -3
  50. package/src/router/match-middleware/background-revalidation.ts +30 -2
  51. package/src/router/match-middleware/cache-lookup.ts +66 -9
  52. package/src/router/match-middleware/cache-store.ts +53 -10
  53. package/src/router/match-middleware/intercept-resolution.ts +9 -7
  54. package/src/router/match-middleware/segment-resolution.ts +8 -5
  55. package/src/router/match-result.ts +22 -6
  56. package/src/router/metrics.ts +6 -1
  57. package/src/router/middleware-types.ts +6 -2
  58. package/src/router/middleware.ts +4 -3
  59. package/src/router/router-context.ts +6 -1
  60. package/src/router/segment-resolution/fresh.ts +130 -17
  61. package/src/router/segment-resolution/helpers.ts +29 -24
  62. package/src/router/segment-resolution/loader-cache.ts +1 -0
  63. package/src/router/segment-resolution/revalidation.ts +352 -290
  64. package/src/router/segment-wrappers.ts +2 -0
  65. package/src/router/types.ts +1 -0
  66. package/src/router.ts +6 -1
  67. package/src/rsc/handler.ts +28 -2
  68. package/src/rsc/loader-fetch.ts +7 -2
  69. package/src/rsc/progressive-enhancement.ts +4 -1
  70. package/src/rsc/rsc-rendering.ts +4 -1
  71. package/src/rsc/server-action.ts +2 -0
  72. package/src/rsc/types.ts +7 -1
  73. package/src/segment-system.tsx +140 -4
  74. package/src/server/context.ts +102 -13
  75. package/src/server/request-context.ts +59 -12
  76. package/src/ssr/index.tsx +1 -0
  77. package/src/types/handler-context.ts +120 -22
  78. package/src/types/loader-types.ts +4 -4
  79. package/src/types/route-entry.ts +7 -0
  80. package/src/types/segments.ts +2 -0
  81. package/src/urls/path-helper.ts +1 -1
  82. package/src/vite/discovery/state.ts +0 -2
  83. package/src/vite/plugin-types.ts +0 -83
  84. package/src/vite/plugins/expose-action-id.ts +1 -3
  85. package/src/vite/plugins/performance-tracks.ts +235 -0
  86. package/src/vite/plugins/version-plugin.ts +13 -1
  87. package/src/vite/rango.ts +148 -209
  88. package/src/vite/router-discovery.ts +0 -8
  89. package/src/vite/utils/banner.ts +3 -3
@@ -10,14 +10,21 @@ declare global {
10
10
  /**
11
11
  * Cloudflare Edge Cache Store
12
12
  *
13
- * Production cache store using Cloudflare's Cache API.
14
- * Handles SWR atomically - get() checks staleness and marks REVALIDATING in one operation.
13
+ * Production cache store using Cloudflare's Cache API (L1) with optional
14
+ * KV persistence (L2).
15
+ *
16
+ * L1 (Cache API): Per-colo, fast, ephemeral. Handles SWR atomically.
17
+ * L2 (KV): Global, persistent, ~50ms reads. Auto-warms cold colos.
18
+ *
19
+ * Read flow: L1 hit → serve | L1 miss → L2 hit → serve + promote to L1 | both miss → render
20
+ * Write flow: L1 write + L2 write (both via waitUntil)
15
21
  *
16
22
  * Features:
17
23
  * - Extended TTL for SWR window (max-age = ttl + swr)
18
24
  * - Staleness via x-edge-cache-stale-at header
19
- * - Atomic REVALIDATING status for thundering herd prevention
25
+ * - Atomic REVALIDATING status for thundering herd prevention (L1 only)
20
26
  * - Non-blocking writes via waitUntil
27
+ * - KV L2 for cross-colo cache persistence
21
28
  */
22
29
 
23
30
  import type {
@@ -68,6 +75,67 @@ export interface ExecutionContext {
68
75
  passThroughOnException(): void;
69
76
  }
70
77
 
78
+ /**
79
+ * Minimal Cloudflare KV Namespace interface.
80
+ * Avoids hard dependency on @cloudflare/workers-types.
81
+ */
82
+ export interface KVNamespace {
83
+ get(key: string, options?: { type?: string }): Promise<any>;
84
+ put(
85
+ key: string,
86
+ value: string,
87
+ options?: { expirationTtl?: number },
88
+ ): Promise<void>;
89
+ delete(key: string): Promise<void>;
90
+ }
91
+
92
+ /**
93
+ * KV envelope for segment cache entries.
94
+ * @internal
95
+ */
96
+ interface KVSegmentEnvelope {
97
+ /** Cached segment data */
98
+ d: CachedEntryData;
99
+ /** When entry becomes stale (ms epoch) */
100
+ s: number;
101
+ /** When entry hard-expires (ms epoch) */
102
+ e: number;
103
+ }
104
+
105
+ /**
106
+ * KV envelope for function cache entries ("use cache").
107
+ * @internal
108
+ */
109
+ interface KVItemEnvelope {
110
+ /** RSC-serialized return value */
111
+ v: string;
112
+ /** Handle data */
113
+ h?: Record<string, Record<string, unknown[]>>;
114
+ /** When entry becomes stale (ms epoch) */
115
+ s: number;
116
+ /** When entry hard-expires (ms epoch) */
117
+ e: number;
118
+ }
119
+
120
+ /**
121
+ * KV envelope for document cache entries.
122
+ * @internal
123
+ */
124
+ interface KVResponseEnvelope {
125
+ /** Response body as base64-encoded string (safe for binary payloads) */
126
+ b: string;
127
+ /** HTTP status code */
128
+ st: number;
129
+ /** HTTP status text */
130
+ stx: string;
131
+ /** Serialized headers as key-value pairs */
132
+ hd: [string, string][];
133
+ /** When entry becomes stale (ms epoch) */
134
+ s: number;
135
+ /** When entry hard-expires (ms epoch) */
136
+ e: number;
137
+ }
138
+
71
139
  export interface CFCacheStoreOptions<TEnv = unknown> {
72
140
  /**
73
141
  * Cache namespace. If not provided, uses caches.default (recommended).
@@ -98,6 +166,20 @@ export interface CFCacheStoreOptions<TEnv = unknown> {
98
166
  */
99
167
  ctx: ExecutionContext;
100
168
 
169
+ /**
170
+ * Optional KV namespace for L2 cache persistence.
171
+ *
172
+ * When provided, KV acts as a global fallback behind the per-colo Cache API.
173
+ * On L1 miss, KV is checked and hits are promoted back to L1.
174
+ * On writes, data is persisted to both L1 and KV.
175
+ *
176
+ * @example
177
+ * ```typescript
178
+ * new CFCacheStore({ ctx: env.ctx, kv: env.CACHE_KV })
179
+ * ```
180
+ */
181
+ kv?: KVNamespace;
182
+
101
183
  /**
102
184
  * Cache version string override. When this changes, all cached entries are
103
185
  * effectively invalidated (new keys won't match old entries).
@@ -163,6 +245,7 @@ export class CFCacheStore<TEnv = unknown> implements SegmentCacheStore<TEnv> {
163
245
  private readonly baseUrl: string;
164
246
  private readonly waitUntil?: (fn: () => Promise<void>) => void;
165
247
  private readonly version?: string;
248
+ private readonly kv?: KVNamespace;
166
249
 
167
250
  constructor(options: CFCacheStoreOptions<TEnv>) {
168
251
  if (!options.ctx) {
@@ -179,6 +262,7 @@ export class CFCacheStore<TEnv = unknown> implements SegmentCacheStore<TEnv> {
179
262
  this.version = options.version ?? VERSION;
180
263
  this.keyGenerator = options.keyGenerator;
181
264
  this.waitUntil = (fn) => options.ctx.waitUntil(fn());
265
+ this.kv = options.kv;
182
266
  }
183
267
 
184
268
  /**
@@ -232,6 +316,10 @@ export class CFCacheStore<TEnv = unknown> implements SegmentCacheStore<TEnv> {
232
316
  return caches.default;
233
317
  }
234
318
 
319
+ // ============================================================================
320
+ // Segment Cache Methods
321
+ // ============================================================================
322
+
235
323
  /**
236
324
  * Get cached entry data by key.
237
325
  *
@@ -240,7 +328,8 @@ export class CFCacheStore<TEnv = unknown> implements SegmentCacheStore<TEnv> {
240
328
  * - If already REVALIDATING (and recent), returns shouldRevalidate: false
241
329
  * - If fresh, returns shouldRevalidate: false
242
330
  *
243
- * The atomic mark prevents thundering herd - only first request triggers revalidation.
331
+ * On L1 miss, falls back to KV (L2) if configured.
332
+ * KV hits are promoted to L1 in the background.
244
333
  */
245
334
  async get(key: string): Promise<CacheGetResult | null> {
246
335
  try {
@@ -249,7 +338,7 @@ export class CFCacheStore<TEnv = unknown> implements SegmentCacheStore<TEnv> {
249
338
  const response = await cache.match(request);
250
339
 
251
340
  if (!response) {
252
- return null;
341
+ return this.kvGetSegment(key);
253
342
  }
254
343
 
255
344
  // Read status headers
@@ -292,6 +381,7 @@ export class CFCacheStore<TEnv = unknown> implements SegmentCacheStore<TEnv> {
292
381
  /**
293
382
  * Store entry data with TTL and optional SWR window.
294
383
  * Uses waitUntil for non-blocking write when available.
384
+ * When KV is configured, also persists to L2.
295
385
  */
296
386
  async set(
297
387
  key: string,
@@ -308,7 +398,8 @@ export class CFCacheStore<TEnv = unknown> implements SegmentCacheStore<TEnv> {
308
398
  const totalTtl = ttl + swrWindow;
309
399
  const staleAt = Date.now() + ttl * 1000;
310
400
 
311
- const response = new Response(JSON.stringify(data), {
401
+ const body = JSON.stringify(data);
402
+ const response = new Response(body, {
312
403
  headers: {
313
404
  "Content-Type": "application/json",
314
405
  "Cache-Control": `public, max-age=${totalTtl}`,
@@ -328,18 +419,35 @@ export class CFCacheStore<TEnv = unknown> implements SegmentCacheStore<TEnv> {
328
419
  // Blocking fallback
329
420
  await putPromise;
330
421
  }
422
+
423
+ // L2: persist to KV
424
+ this.kvSetSegment(key, data, staleAt, totalTtl);
331
425
  } catch (error) {
332
426
  console.error("[CFCacheStore] set failed:", error);
333
427
  }
334
428
  }
335
429
 
336
430
  /**
337
- * Delete a cached entry
431
+ * Delete a cached entry from L1 and L2.
338
432
  */
339
433
  async delete(key: string): Promise<boolean> {
340
434
  try {
341
435
  const cache = await this.getCache();
342
- return await cache.delete(this.keyToRequest(key));
436
+ const result = await cache.delete(this.keyToRequest(key));
437
+
438
+ // L2: delete from KV
439
+ if (this.kv && this.waitUntil) {
440
+ const kvKey = this.toKVKey(key);
441
+ this.waitUntil(async () => {
442
+ try {
443
+ await this.kv!.delete(kvKey);
444
+ } catch {
445
+ // KV delete failures are non-critical
446
+ }
447
+ });
448
+ }
449
+
450
+ return result;
343
451
  } catch (error) {
344
452
  console.error("[CFCacheStore] delete failed:", error);
345
453
  return false;
@@ -353,6 +461,7 @@ export class CFCacheStore<TEnv = unknown> implements SegmentCacheStore<TEnv> {
353
461
  /**
354
462
  * Get a cached Response by key (for document-level caching).
355
463
  * Returns the response and whether it should be revalidated (SWR).
464
+ * Falls back to KV (L2) on L1 miss.
356
465
  */
357
466
  async getResponse(
358
467
  key: string,
@@ -363,7 +472,7 @@ export class CFCacheStore<TEnv = unknown> implements SegmentCacheStore<TEnv> {
363
472
  const response = await cache.match(request);
364
473
 
365
474
  if (!response || response.status !== 200) {
366
- return null;
475
+ return this.kvGetResponse(key);
367
476
  }
368
477
 
369
478
  // Check staleness
@@ -382,6 +491,7 @@ export class CFCacheStore<TEnv = unknown> implements SegmentCacheStore<TEnv> {
382
491
 
383
492
  /**
384
493
  * Store a Response with TTL and optional SWR window (for document-level caching).
494
+ * When KV is configured, also persists to L2.
385
495
  */
386
496
  async putResponse(
387
497
  key: string,
@@ -398,12 +508,19 @@ export class CFCacheStore<TEnv = unknown> implements SegmentCacheStore<TEnv> {
398
508
  const totalTtl = ttl + swrWindow;
399
509
  const staleAt = Date.now() + ttl * 1000;
400
510
 
511
+ // Clone body for potential KV write before consuming it for L1
512
+ const [l1Body, kvBody] = this.kv
513
+ ? response.body
514
+ ? response.body.tee()
515
+ : [null, null]
516
+ : [response.body, null];
517
+
401
518
  // Clone and add cache headers
402
519
  const headers = new Headers(response.headers);
403
520
  headers.set("Cache-Control", `public, max-age=${totalTtl}`);
404
521
  headers.set(CACHE_STALE_AT_HEADER, String(staleAt));
405
522
 
406
- const toCache = new Response(response.body, {
523
+ const toCache = new Response(l1Body, {
407
524
  status: response.status,
408
525
  statusText: response.statusText,
409
526
  headers,
@@ -420,6 +537,36 @@ export class CFCacheStore<TEnv = unknown> implements SegmentCacheStore<TEnv> {
420
537
  // Blocking fallback
421
538
  await putPromise;
422
539
  }
540
+
541
+ // L2: persist to KV (KV requires expirationTtl >= 60s)
542
+ if (this.kv && this.waitUntil && totalTtl >= 60) {
543
+ const kvKey = this.toKVKey(`doc:${key}`);
544
+ const headersArray: [string, string][] = [];
545
+ response.headers.forEach((v, k) => headersArray.push([k, v]));
546
+ // Read body as ArrayBuffer and encode to base64 to preserve binary payloads
547
+ const bodyBuf = kvBody
548
+ ? await new Response(kvBody).arrayBuffer()
549
+ : new ArrayBuffer(0);
550
+ const bodyBase64 = bufferToBase64(bodyBuf);
551
+
552
+ this.waitUntil(async () => {
553
+ try {
554
+ const envelope: KVResponseEnvelope = {
555
+ b: bodyBase64,
556
+ st: response.status,
557
+ stx: response.statusText,
558
+ hd: headersArray,
559
+ s: staleAt,
560
+ e: staleAt + swrWindow * 1000,
561
+ };
562
+ await this.kv!.put(kvKey, JSON.stringify(envelope), {
563
+ expirationTtl: totalTtl,
564
+ });
565
+ } catch (error) {
566
+ console.error("[CFCacheStore] KV putResponse failed:", error);
567
+ }
568
+ });
569
+ }
423
570
  } catch (error) {
424
571
  console.error("[CFCacheStore] putResponse failed:", error);
425
572
  }
@@ -432,6 +579,7 @@ export class CFCacheStore<TEnv = unknown> implements SegmentCacheStore<TEnv> {
432
579
  /**
433
580
  * Get a cached function result by key.
434
581
  * Follows the same SWR pattern as get() for segment caching.
582
+ * Falls back to KV (L2) on L1 miss.
435
583
  */
436
584
  async getItem(key: string): Promise<CacheItemResult | null> {
437
585
  try {
@@ -439,7 +587,7 @@ export class CFCacheStore<TEnv = unknown> implements SegmentCacheStore<TEnv> {
439
587
  const request = this.keyToRequest(`fn:${key}`);
440
588
  const response = await cache.match(request);
441
589
 
442
- if (!response) return null;
590
+ if (!response) return this.kvGetItem(key);
443
591
 
444
592
  const staleAt = Number(
445
593
  response.headers.get(CACHE_STALE_AT_HEADER) ?? "0",
@@ -485,6 +633,7 @@ export class CFCacheStore<TEnv = unknown> implements SegmentCacheStore<TEnv> {
485
633
 
486
634
  /**
487
635
  * Store a function result with TTL and optional SWR window.
636
+ * When KV is configured, also persists to L2.
488
637
  */
489
638
  async setItem(
490
639
  key: string,
@@ -519,11 +668,35 @@ export class CFCacheStore<TEnv = unknown> implements SegmentCacheStore<TEnv> {
519
668
  } else {
520
669
  await putPromise;
521
670
  }
671
+
672
+ // L2: persist to KV (KV requires expirationTtl >= 60s)
673
+ if (this.kv && this.waitUntil && totalTtl >= 60) {
674
+ const kvKey = this.toKVKey(`fn:${key}`);
675
+ this.waitUntil(async () => {
676
+ try {
677
+ const envelope: KVItemEnvelope = {
678
+ v: value,
679
+ h: options?.handles,
680
+ s: staleAt,
681
+ e: staleAt + swrWindow * 1000,
682
+ };
683
+ await this.kv!.put(kvKey, JSON.stringify(envelope), {
684
+ expirationTtl: totalTtl,
685
+ });
686
+ } catch (error) {
687
+ console.error("[CFCacheStore] KV setItem failed:", error);
688
+ }
689
+ });
690
+ }
522
691
  } catch (error) {
523
692
  console.error("[CFCacheStore] setItem failed:", error);
524
693
  }
525
694
  }
526
695
 
696
+ // ============================================================================
697
+ // Key Helpers
698
+ // ============================================================================
699
+
527
700
  /**
528
701
  * Convert string key to Request object for CF Cache API.
529
702
  * Includes version in URL if specified (for cache invalidation on code changes).
@@ -537,4 +710,273 @@ export class CFCacheStore<TEnv = unknown> implements SegmentCacheStore<TEnv> {
537
710
  method: "GET",
538
711
  });
539
712
  }
713
+
714
+ /**
715
+ * Convert string key to KV key string.
716
+ * Uses same version prefix as Cache API for consistent invalidation.
717
+ * @internal
718
+ */
719
+ private toKVKey(key: string): string {
720
+ const versionPath = this.version ? `v/${this.version}/` : "";
721
+ return `${versionPath}${key}`;
722
+ }
723
+
724
+ // ============================================================================
725
+ // KV L2 Helpers
726
+ // ============================================================================
727
+
728
+ /**
729
+ * KV fallback for segment cache reads.
730
+ * Returns null if KV is not configured, entry is missing, or expired.
731
+ * Promotes hits to L1 via waitUntil.
732
+ * @internal
733
+ */
734
+ private async kvGetSegment(key: string): Promise<CacheGetResult | null> {
735
+ if (!this.kv) return null;
736
+
737
+ try {
738
+ const kvKey = this.toKVKey(key);
739
+ const raw = await this.kv.get(kvKey, { type: "json" });
740
+ if (!raw) return null;
741
+
742
+ const envelope = raw as KVSegmentEnvelope;
743
+ const now = Date.now();
744
+
745
+ // Hard-expired — treat as miss
746
+ if (now > envelope.e) return null;
747
+
748
+ const shouldRevalidate = now > envelope.s;
749
+
750
+ // Promote to L1 in background
751
+ this.promoteSegmentToL1(key, envelope);
752
+
753
+ return { data: envelope.d, shouldRevalidate };
754
+ } catch (error) {
755
+ console.error("[CFCacheStore] KV get failed:", error);
756
+ return null;
757
+ }
758
+ }
759
+
760
+ /**
761
+ * Write segment data to KV.
762
+ * @internal
763
+ */
764
+ private kvSetSegment(
765
+ key: string,
766
+ data: CachedEntryData,
767
+ staleAt: number,
768
+ totalTtl: number,
769
+ ): void {
770
+ // KV requires expirationTtl >= 60s. Skip write for short-lived entries.
771
+ if (!this.kv || !this.waitUntil || totalTtl < 60) return;
772
+
773
+ const kvKey = this.toKVKey(key);
774
+ const swrWindow = totalTtl * 1000 - (staleAt - Date.now());
775
+ const expiresAt = staleAt + swrWindow;
776
+
777
+ this.waitUntil(async () => {
778
+ try {
779
+ const envelope: KVSegmentEnvelope = {
780
+ d: data,
781
+ s: staleAt,
782
+ e: expiresAt,
783
+ };
784
+ await this.kv!.put(kvKey, JSON.stringify(envelope), {
785
+ expirationTtl: totalTtl,
786
+ });
787
+ } catch (error) {
788
+ console.error("[CFCacheStore] KV set failed:", error);
789
+ }
790
+ });
791
+ }
792
+
793
+ /**
794
+ * Promote segment data from KV to L1 Cache API.
795
+ * @internal
796
+ */
797
+ private promoteSegmentToL1(key: string, envelope: KVSegmentEnvelope): void {
798
+ if (!this.waitUntil) return;
799
+
800
+ this.waitUntil(async () => {
801
+ try {
802
+ const now = Date.now();
803
+ const remainingTtl = Math.max(1, Math.floor((envelope.e - now) / 1000));
804
+ const cache = await this.getCache();
805
+ const request = this.keyToRequest(key);
806
+
807
+ const response = new Response(JSON.stringify(envelope.d), {
808
+ headers: {
809
+ "Content-Type": "application/json",
810
+ "Cache-Control": `public, max-age=${remainingTtl}`,
811
+ [CACHE_STALE_AT_HEADER]: String(envelope.s),
812
+ [CACHE_STATUS_HEADER]: "HIT",
813
+ },
814
+ });
815
+
816
+ await cache.put(request, response);
817
+ } catch (error) {
818
+ console.error("[CFCacheStore] L1 promote failed:", error);
819
+ }
820
+ });
821
+ }
822
+
823
+ /**
824
+ * KV fallback for function cache reads.
825
+ * @internal
826
+ */
827
+ private async kvGetItem(key: string): Promise<CacheItemResult | null> {
828
+ if (!this.kv) return null;
829
+
830
+ try {
831
+ const kvKey = this.toKVKey(`fn:${key}`);
832
+ const raw = await this.kv.get(kvKey, { type: "json" });
833
+ if (!raw) return null;
834
+
835
+ const envelope = raw as KVItemEnvelope;
836
+ const now = Date.now();
837
+
838
+ if (now > envelope.e) return null;
839
+
840
+ const shouldRevalidate = now > envelope.s;
841
+
842
+ // Promote to L1
843
+ this.promoteItemToL1(key, envelope);
844
+
845
+ return {
846
+ value: envelope.v,
847
+ handles: envelope.h,
848
+ shouldRevalidate,
849
+ };
850
+ } catch (error) {
851
+ console.error("[CFCacheStore] KV getItem failed:", error);
852
+ return null;
853
+ }
854
+ }
855
+
856
+ /**
857
+ * Promote function cache data from KV to L1.
858
+ * @internal
859
+ */
860
+ private promoteItemToL1(key: string, envelope: KVItemEnvelope): void {
861
+ if (!this.waitUntil) return;
862
+
863
+ this.waitUntil(async () => {
864
+ try {
865
+ const now = Date.now();
866
+ const remainingTtl = Math.max(1, Math.floor((envelope.e - now) / 1000));
867
+ const cache = await this.getCache();
868
+ const request = this.keyToRequest(`fn:${key}`);
869
+
870
+ const body = JSON.stringify({ value: envelope.v, handles: envelope.h });
871
+ const response = new Response(body, {
872
+ headers: {
873
+ "Content-Type": "application/json",
874
+ "Cache-Control": `public, max-age=${remainingTtl}`,
875
+ [CACHE_STALE_AT_HEADER]: String(envelope.s),
876
+ [CACHE_STATUS_HEADER]: "HIT",
877
+ },
878
+ });
879
+
880
+ await cache.put(request, response);
881
+ } catch (error) {
882
+ console.error("[CFCacheStore] L1 item promote failed:", error);
883
+ }
884
+ });
885
+ }
886
+
887
+ /**
888
+ * KV fallback for document cache reads.
889
+ * @internal
890
+ */
891
+ private async kvGetResponse(
892
+ key: string,
893
+ ): Promise<{ response: Response; shouldRevalidate: boolean } | null> {
894
+ if (!this.kv) return null;
895
+
896
+ try {
897
+ const kvKey = this.toKVKey(`doc:${key}`);
898
+ const raw = await this.kv.get(kvKey, { type: "json" });
899
+ if (!raw) return null;
900
+
901
+ const envelope = raw as KVResponseEnvelope;
902
+ const now = Date.now();
903
+
904
+ if (now > envelope.e) return null;
905
+
906
+ const shouldRevalidate = now > envelope.s;
907
+
908
+ // Reconstruct Response (decode base64 → binary)
909
+ const headers = new Headers(envelope.hd);
910
+ const bodyBuffer = base64ToBuffer(envelope.b);
911
+ const response = new Response(bodyBuffer, {
912
+ status: envelope.st,
913
+ statusText: envelope.stx,
914
+ headers,
915
+ });
916
+
917
+ // Promote to L1
918
+ this.promoteResponseToL1(key, envelope);
919
+
920
+ return { response, shouldRevalidate };
921
+ } catch (error) {
922
+ console.error("[CFCacheStore] KV getResponse failed:", error);
923
+ return null;
924
+ }
925
+ }
926
+
927
+ /**
928
+ * Promote document cache data from KV to L1.
929
+ * @internal
930
+ */
931
+ private promoteResponseToL1(key: string, envelope: KVResponseEnvelope): void {
932
+ if (!this.waitUntil) return;
933
+
934
+ this.waitUntil(async () => {
935
+ try {
936
+ const now = Date.now();
937
+ const remainingTtl = Math.max(1, Math.floor((envelope.e - now) / 1000));
938
+ const cache = await this.getCache();
939
+ const request = this.keyToRequest(`doc:${key}`);
940
+
941
+ const headers = new Headers(envelope.hd);
942
+ headers.set("Cache-Control", `public, max-age=${remainingTtl}`);
943
+ headers.set(CACHE_STALE_AT_HEADER, String(envelope.s));
944
+
945
+ const bodyBuffer = base64ToBuffer(envelope.b);
946
+ const response = new Response(bodyBuffer, {
947
+ status: envelope.st,
948
+ statusText: envelope.stx,
949
+ headers,
950
+ });
951
+
952
+ await cache.put(request, response);
953
+ } catch (error) {
954
+ console.error("[CFCacheStore] L1 response promote failed:", error);
955
+ }
956
+ });
957
+ }
958
+ }
959
+
960
+ // ============================================================================
961
+ // Base64 Helpers (binary-safe response body encoding for KV)
962
+ // ============================================================================
963
+
964
+ /** Encode ArrayBuffer to base64 string. */
965
+ function bufferToBase64(buffer: ArrayBuffer): string {
966
+ const bytes = new Uint8Array(buffer);
967
+ let binary = "";
968
+ for (let i = 0; i < bytes.length; i++) {
969
+ binary += String.fromCharCode(bytes[i]!);
970
+ }
971
+ return btoa(binary);
972
+ }
973
+
974
+ /** Decode base64 string to ArrayBuffer. */
975
+ function base64ToBuffer(base64: string): ArrayBuffer {
976
+ const binary = atob(base64);
977
+ const bytes = new Uint8Array(binary.length);
978
+ for (let i = 0; i < binary.length; i++) {
979
+ bytes[i] = binary.charCodeAt(i);
980
+ }
981
+ return bytes.buffer;
540
982
  }
@@ -10,7 +10,11 @@
10
10
  */
11
11
 
12
12
  // Public API
13
- export { CFCacheStore, type CFCacheStoreOptions } from "./cf-cache-store.js";
13
+ export {
14
+ CFCacheStore,
15
+ type CFCacheStoreOptions,
16
+ type KVNamespace,
17
+ } from "./cf-cache-store.js";
14
18
 
15
19
  // Header constants for debugging and inspection
16
20
  export {
@@ -13,6 +13,7 @@
13
13
 
14
14
  import type { MiddlewareFn, MiddlewareContext } from "../router/middleware.js";
15
15
  import { getRequestContext } from "../server/request-context.js";
16
+ import { mayNeedSSR } from "../rsc/ssr-setup.js";
16
17
  import { sortedSearchString } from "./cache-key-utils.js";
17
18
  import { runBackground } from "./background-task.js";
18
19
 
@@ -204,18 +205,24 @@ export function createDocumentCacheMiddleware<TEnv = any>(
204
205
  ): Promise<Response> {
205
206
  const url = ctx.url;
206
207
 
208
+ // Use the original request URL for _rsc* param detection and cache key
209
+ // differentiation. ctx.url is stripped of _rsc* params by the middleware
210
+ // pipeline (stripInternalParams), so _rsc_partial, _rsc_segments, etc.
211
+ // are not visible on ctx.url in production.
212
+ const rawUrl = new URL(ctx.request.url);
213
+
207
214
  // Only cache GET requests — mutations and other methods must not be cached
208
215
  if (ctx.request.method !== "GET") {
209
216
  return next();
210
217
  }
211
218
 
212
219
  // Skip RSC action requests (mutations shouldn't be cached)
213
- if (url.searchParams.has("_rsc_action")) {
220
+ if (rawUrl.searchParams.has("_rsc_action")) {
214
221
  return next();
215
222
  }
216
223
 
217
224
  // Skip loader requests (have their own caching)
218
- if (url.searchParams.has("_rsc_loader")) {
225
+ if (rawUrl.searchParams.has("_rsc_loader")) {
219
226
  return next();
220
227
  }
221
228
 
@@ -241,9 +248,12 @@ export function createDocumentCacheMiddleware<TEnv = any>(
241
248
  return next();
242
249
  }
243
250
 
244
- // Determine request type for cache key differentiation
245
- const isPartial = url.searchParams.has("_rsc_partial");
246
- const typeLabel = isPartial ? "RSC" : "HTML";
251
+ // Determine request type for cache key differentiation.
252
+ // Uses rawUrl for _rsc* param checks and mayNeedSSR for Accept-based
253
+ // detection. Full-document RSC fetches must not share the HTML cache slot.
254
+ const isPartial = rawUrl.searchParams.has("_rsc_partial");
255
+ const isRscRequest = !mayNeedSSR(ctx.request, rawUrl);
256
+ const typeLabel = isRscRequest ? "RSC" : "HTML";
247
257
 
248
258
  // Track whether next() has been called so the catch block knows
249
259
  // whether it is safe to fall through to the handler.
@@ -254,10 +264,10 @@ export function createDocumentCacheMiddleware<TEnv = any>(
254
264
  // gracefully to the origin handler instead of rejecting the request.
255
265
  // This is a deliberate fail-open-to-origin policy: the fallback is
256
266
  // "serve uncached from origin", not "use a different cache key".
257
- const clientSegments = url.searchParams.get("_rsc_segments") || "";
267
+ const clientSegments = rawUrl.searchParams.get("_rsc_segments") || "";
258
268
  const segmentHash =
259
269
  isPartial && clientSegments ? `:${hashSegmentIds(clientSegments)}` : "";
260
- const typeSuffix = isPartial ? ":rsc" : ":html";
270
+ const typeSuffix = isRscRequest ? ":rsc" : ":html";
261
271
 
262
272
  let searchSuffix = "";
263
273
  if (!keyGenerator) {