@timber-js/app 0.2.0-alpha.12 → 0.2.0-alpha.13

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.
@@ -1 +1 @@
1
- {"version":3,"file":"entries.d.ts","sourceRoot":"","sources":["../../src/plugins/entries.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;GAWG;AAEH,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,MAAM,CAAC;AAInC,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,YAAY,CAAC;AA6GhD;;;;;GAKG;AACH,wBAAgB,yBAAyB,CAAC,IAAI,EAAE,MAAM,GAAG,MAAM,GAAG,IAAI,CAUrE;AAED;;;;;;;;GAQG;AACH,wBAAgB,6BAA6B,CAAC,mBAAmB,EAAE,MAAM,GAAG,IAAI,GAAG,MAAM,CAwBxF;AAED;;;;GAIG;AACH,wBAAgB,aAAa,CAAC,GAAG,EAAE,aAAa,GAAG,MAAM,CAwExD"}
1
+ {"version":3,"file":"entries.d.ts","sourceRoot":"","sources":["../../src/plugins/entries.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;GAWG;AAEH,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,MAAM,CAAC;AAInC,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,YAAY,CAAC;AA4GhD;;;;;GAKG;AACH,wBAAgB,yBAAyB,CAAC,IAAI,EAAE,MAAM,GAAG,MAAM,GAAG,IAAI,CAUrE;AAED;;;;;;;;GAQG;AACH,wBAAgB,6BAA6B,CAAC,mBAAmB,EAAE,MAAM,GAAG,IAAI,GAAG,MAAM,CAwBxF;AAED;;;;GAIG;AACH,wBAAgB,aAAa,CAAC,GAAG,EAAE,aAAa,GAAG,MAAM,CAwExD"}
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/server/rsc-entry/index.ts"],"names":[],"mappings":"AAgFA;;;;;GAKG;AACH,wBAAgB,0BAA0B,CAAC,OAAO,EAAE,CAAC,KAAK,EAAE,KAAK,EAAE,KAAK,EAAE,MAAM,KAAK,IAAI,GAAG,IAAI,CAE/F;AAkaD,OAAO,EAAE,uBAAuB,EAAE,MAAM,gCAAgC,CAAC;AAIzE,OAAO,EAAE,gBAAgB,EAAE,MAAM,8BAA8B,CAAC;8BAtQ3C,OAAO,KAAG,OAAO,CAAC,QAAQ,CAAC;AAwQhD,wBAAiE"}
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/server/rsc-entry/index.ts"],"names":[],"mappings":"AA2EA;;;;;GAKG;AACH,wBAAgB,0BAA0B,CAAC,OAAO,EAAE,CAAC,KAAK,EAAE,KAAK,EAAE,KAAK,EAAE,MAAM,KAAK,IAAI,GAAG,IAAI,CAE/F;AAoZD,OAAO,EAAE,uBAAuB,EAAE,MAAM,gCAAgC,CAAC;AAIzE,OAAO,EAAE,gBAAgB,EAAE,MAAM,8BAA8B,CAAC;8BAtQ3C,OAAO,KAAG,OAAO,CAAC,QAAQ,CAAC;AAwQhD,wBAAiE"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@timber-js/app",
3
- "version": "0.2.0-alpha.12",
3
+ "version": "0.2.0-alpha.13",
4
4
  "description": "Vite-native React framework for Cloudflare Workers — correct HTTP semantics, real status codes, pages that work without JavaScript",
5
5
  "keywords": [
6
6
  "cloudflare-workers",
@@ -79,6 +79,11 @@
79
79
  "publishConfig": {
80
80
  "access": "public"
81
81
  },
82
+ "scripts": {
83
+ "build": "vite build --config vite.lib.config.ts && tsc --emitDeclarationOnly --project tsconfig.json --outDir dist",
84
+ "typecheck": "tsgo --noEmit",
85
+ "prepublishOnly": "pnpm run build"
86
+ },
82
87
  "dependencies": {
83
88
  "@opentelemetry/api": "^1.9.0",
84
89
  "@opentelemetry/context-async-hooks": "^2.6.0",
@@ -117,9 +122,5 @@
117
122
  },
118
123
  "engines": {
119
124
  "node": ">=20.0.0"
120
- },
121
- "scripts": {
122
- "build": "vite build --config vite.lib.config.ts && tsc --emitDeclarationOnly --project tsconfig.json --outDir dist",
123
- "typecheck": "tsgo --noEmit"
124
125
  }
125
- }
126
+ }
package/src/cli.ts CHANGED
File without changes
package/src/index.ts CHANGED
@@ -146,34 +146,6 @@ export interface TimberUserConfig {
146
146
  /** CSS z-index. Default: 1600. */
147
147
  zIndex?: number;
148
148
  };
149
- /**
150
- * Response-level caching and deduplication.
151
- *
152
- * When enabled, concurrent requests to the same URL share a single render
153
- * (singleflight), and recently rendered responses are reused from a short-TTL
154
- * LRU cache without re-executing the RSC-to-SSR pipeline.
155
- *
156
- * Set to `false` to disable entirely. Default: enabled with sensible defaults.
157
- *
158
- * See design/31-benchmarking.md for performance context.
159
- */
160
- responseCache?:
161
- | false
162
- | {
163
- /** Maximum number of entries in the LRU cache. Default: 150. */
164
- maxSize?: number;
165
- /** TTL for cached entries in milliseconds. Default: 5000 (5s). */
166
- ttlMs?: number;
167
- /**
168
- * When true (default), requests with Cookie or Authorization headers
169
- * bypass the cache entirely. This prevents sharing user-specific
170
- * responses across requests.
171
- *
172
- * Set to false to cache all responses regardless of auth state.
173
- * Only do this if your pages are truly public and don't vary by user.
174
- */
175
- publicOnly?: boolean;
176
- };
177
149
  }
178
150
 
179
151
  /**
@@ -110,7 +110,6 @@ function generateConfigModule(ctx: PluginContext): string {
110
110
  slowRequestMs: ctx.config.slowRequestMs ?? 3000,
111
111
  cookieSecrets,
112
112
  topLoader: ctx.config.topLoader,
113
- responseCache: ctx.config.responseCache,
114
113
  debug: ctx.config.debug ?? false,
115
114
  };
116
115
 
@@ -63,11 +63,6 @@ import {
63
63
  isRscPayloadRequest,
64
64
  } from './helpers.js';
65
65
  import { parseClientStateTree } from '#/server/state-tree-diff.js';
66
- import {
67
- createResponseCache,
68
- resolveResponseCacheConfig,
69
- type ResponseCache,
70
- } from '#/server/response-cache.js';
71
66
  import { buildRscPayloadResponse } from './rsc-payload.js';
72
67
  import { renderRscStream } from './rsc-stream.js';
73
68
  import { renderSsrResponse } from './ssr-renderer.js';
@@ -164,17 +159,6 @@ async function createRequestHandler(manifest: typeof routeManifest, runtimeConfi
164
159
 
165
160
  const typedBuildManifest = buildManifest as BuildManifest;
166
161
 
167
- // Initialize response-level caching and singleflight deduplication.
168
- // See design/31-benchmarking.md for performance motivation.
169
- const responseCacheRaw = (runtimeConfig as Record<string, unknown>).responseCache as
170
- | { maxSize?: number; ttlMs?: number; publicOnly?: boolean }
171
- | false
172
- | undefined;
173
- const responseCacheConfig = resolveResponseCacheConfig(responseCacheRaw);
174
- const responseCache: ResponseCache | null = responseCacheConfig
175
- ? createResponseCache(responseCacheConfig)
176
- : null;
177
-
178
162
  const pipelineConfig: PipelineConfig = {
179
163
  proxyLoader: manifest.proxy?.load,
180
164
  matchRoute,
@@ -205,17 +189,14 @@ async function createRequestHandler(manifest: typeof routeManifest, runtimeConfi
205
189
  _requestHeaderOverlay: Headers,
206
190
  interception?: InterceptionContext
207
191
  ) => {
208
- const doRender = () =>
209
- renderRoute(req, match, responseHeaders, clientBootstrap, clientJsDisabled, interception);
210
-
211
- // Response cache wraps the render with singleflight + LRU.
212
- // Interception requests (modals) are excluded — they depend on
213
- // X-Timber-URL which makes caching semantics ambiguous.
214
- if (responseCache && !interception) {
215
- const isRsc = (req.headers.get('Accept') ?? '').includes('text/x-component');
216
- return responseCache.getOrRender(req, isRsc, doRender);
217
- }
218
- return doRender();
192
+ return renderRoute(
193
+ req,
194
+ match,
195
+ responseHeaders,
196
+ clientBootstrap,
197
+ clientJsDisabled,
198
+ interception
199
+ );
219
200
  },
220
201
  renderNoMatch: async (req: Request, responseHeaders: Headers) => {
221
202
  return renderNoMatchPage(req, manifest.root, responseHeaders, clientBootstrap);
package/LICENSE DELETED
@@ -1,8 +0,0 @@
1
- DONTFUCKINGUSE LICENSE
2
-
3
- Copyright (c) 2025 Daniel Saewitz
4
-
5
- This software may not be used, copied, modified, merged, published,
6
- distributed, sublicensed, or sold by anyone other than the copyright holder.
7
-
8
- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND.
@@ -1,54 +0,0 @@
1
- /**
2
- * Render-level response deduplication and short-TTL LRU cache.
3
- *
4
- * Two layers of optimization:
5
- *
6
- * 1. **Singleflight** — concurrent requests to the same URL share a single
7
- * render. Uses createSingleflight() from cache/singleflight.ts.
8
- *
9
- * 2. **LRU cache** — recently rendered responses are reused without
10
- * re-executing the RSC-to-SSR pipeline. Entries have a short TTL
11
- * (default 5s) and the cache has a bounded size (default 150 entries).
12
- *
13
- * Cache keys are compound: pathname + search + isRscPayload + Vary'd headers.
14
- * Only GET requests are cached. Responses with Set-Cookie, Cache-Control:
15
- * no-store/private, or error/redirect status codes are never cached.
16
- * When `publicOnly` is true (default), requests with Cookie or Authorization
17
- * headers bypass the cache entirely.
18
- *
19
- * See design/02-rendering-pipeline.md, design/31-benchmarking.md.
20
- */
21
- export interface ResponseCacheConfig {
22
- /** Maximum number of entries in the LRU cache. Default: 150. */
23
- maxSize?: number;
24
- /** TTL for cached entries in milliseconds. Default: 5000 (5s). */
25
- ttlMs?: number;
26
- /**
27
- * When true (default), requests with Cookie or Authorization headers
28
- * bypass the cache entirely. This prevents sharing user-specific
29
- * responses across requests.
30
- */
31
- publicOnly?: boolean;
32
- }
33
- export interface ResolvedResponseCacheConfig {
34
- maxSize: number;
35
- ttlMs: number;
36
- publicOnly: boolean;
37
- }
38
- export declare function resolveResponseCacheConfig(config?: ResponseCacheConfig | false): ResolvedResponseCacheConfig | null;
39
- export interface ResponseCache {
40
- /**
41
- * Wrap a render function with singleflight dedup + LRU caching.
42
- * Returns the cached Response or executes the render function.
43
- */
44
- getOrRender(req: Request, isRscPayload: boolean, renderFn: () => Promise<Response>): Promise<Response>;
45
- /** Number of entries currently in the LRU cache. */
46
- readonly size: number;
47
- /** Clear all cached entries. */
48
- clear(): void;
49
- }
50
- /**
51
- * Create a response cache with singleflight deduplication and LRU caching.
52
- */
53
- export declare function createResponseCache(config: ResolvedResponseCacheConfig): ResponseCache;
54
- //# sourceMappingURL=response-cache.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"response-cache.d.ts","sourceRoot":"","sources":["../../src/server/response-cache.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;GAmBG;AAMH,MAAM,WAAW,mBAAmB;IAClC,gEAAgE;IAChE,OAAO,CAAC,EAAE,MAAM,CAAC;IACjB,kEAAkE;IAClE,KAAK,CAAC,EAAE,MAAM,CAAC;IACf;;;;OAIG;IACH,UAAU,CAAC,EAAE,OAAO,CAAC;CACtB;AAED,MAAM,WAAW,2BAA2B;IAC1C,OAAO,EAAE,MAAM,CAAC;IAChB,KAAK,EAAE,MAAM,CAAC;IACd,UAAU,EAAE,OAAO,CAAC;CACrB;AAED,wBAAgB,0BAA0B,CACxC,MAAM,CAAC,EAAE,mBAAmB,GAAG,KAAK,GACnC,2BAA2B,GAAG,IAAI,CASpC;AA+ED,MAAM,WAAW,aAAa;IAC5B;;;OAGG;IACH,WAAW,CACT,GAAG,EAAE,OAAO,EACZ,YAAY,EAAE,OAAO,EACrB,QAAQ,EAAE,MAAM,OAAO,CAAC,QAAQ,CAAC,GAChC,OAAO,CAAC,QAAQ,CAAC,CAAC;IAErB,oDAAoD;IACpD,QAAQ,CAAC,IAAI,EAAE,MAAM,CAAC;IAEtB,gCAAgC;IAChC,KAAK,IAAI,IAAI,CAAC;CACf;AAuDD;;GAEG;AACH,wBAAgB,mBAAmB,CAAC,MAAM,EAAE,2BAA2B,GAAG,aAAa,CAyMtF"}
@@ -1,410 +0,0 @@
1
- /**
2
- * Render-level response deduplication and short-TTL LRU cache.
3
- *
4
- * Two layers of optimization:
5
- *
6
- * 1. **Singleflight** — concurrent requests to the same URL share a single
7
- * render. Uses createSingleflight() from cache/singleflight.ts.
8
- *
9
- * 2. **LRU cache** — recently rendered responses are reused without
10
- * re-executing the RSC-to-SSR pipeline. Entries have a short TTL
11
- * (default 5s) and the cache has a bounded size (default 150 entries).
12
- *
13
- * Cache keys are compound: pathname + search + isRscPayload + Vary'd headers.
14
- * Only GET requests are cached. Responses with Set-Cookie, Cache-Control:
15
- * no-store/private, or error/redirect status codes are never cached.
16
- * When `publicOnly` is true (default), requests with Cookie or Authorization
17
- * headers bypass the cache entirely.
18
- *
19
- * See design/02-rendering-pipeline.md, design/31-benchmarking.md.
20
- */
21
-
22
- import { createSingleflight } from '#/cache/singleflight.js';
23
-
24
- // ─── Configuration ─────────────────────────────────────────────────────────
25
-
26
- export interface ResponseCacheConfig {
27
- /** Maximum number of entries in the LRU cache. Default: 150. */
28
- maxSize?: number;
29
- /** TTL for cached entries in milliseconds. Default: 5000 (5s). */
30
- ttlMs?: number;
31
- /**
32
- * When true (default), requests with Cookie or Authorization headers
33
- * bypass the cache entirely. This prevents sharing user-specific
34
- * responses across requests.
35
- */
36
- publicOnly?: boolean;
37
- }
38
-
39
- export interface ResolvedResponseCacheConfig {
40
- maxSize: number;
41
- ttlMs: number;
42
- publicOnly: boolean;
43
- }
44
-
45
- export function resolveResponseCacheConfig(
46
- config?: ResponseCacheConfig | false
47
- ): ResolvedResponseCacheConfig | null {
48
- // Explicitly disabled
49
- if (config === false) return null;
50
-
51
- return {
52
- maxSize: config?.maxSize ?? 150,
53
- ttlMs: config?.ttlMs ?? 5000,
54
- publicOnly: config?.publicOnly ?? true,
55
- };
56
- }
57
-
58
- // ─── Cache Entry ───────────────────────────────────────────────────────────
59
-
60
- interface CacheEntry {
61
- /** The cached response body as an ArrayBuffer (already consumed). */
62
- body: ArrayBuffer;
63
- /** Response status code. */
64
- status: number;
65
- /** Response headers (serialized). */
66
- headers: [string, string][];
67
- /** Timestamp when this entry was created. */
68
- createdAt: number;
69
- /**
70
- * The Vary header value from the original response, if any.
71
- * Used to build variant-aware cache keys for subsequent requests.
72
- */
73
- vary: string | null;
74
- }
75
-
76
- // ─── LRU Cache ─────────────────────────────────────────────────────────────
77
-
78
- /**
79
- * Simple LRU cache backed by a Map (insertion order = access order).
80
- * On get, we delete and re-insert to move the entry to the end (most recent).
81
- * On eviction, we delete from the beginning (least recent).
82
- */
83
- class LruCache {
84
- private readonly map = new Map<string, CacheEntry>();
85
- private readonly maxSize: number;
86
- private readonly ttlMs: number;
87
-
88
- constructor(maxSize: number, ttlMs: number) {
89
- this.maxSize = maxSize;
90
- this.ttlMs = ttlMs;
91
- }
92
-
93
- get(key: string): CacheEntry | undefined {
94
- const entry = this.map.get(key);
95
- if (!entry) return undefined;
96
-
97
- // Check TTL
98
- if (Date.now() - entry.createdAt > this.ttlMs) {
99
- this.map.delete(key);
100
- return undefined;
101
- }
102
-
103
- // Move to end (most recently used)
104
- this.map.delete(key);
105
- this.map.set(key, entry);
106
- return entry;
107
- }
108
-
109
- set(key: string, entry: CacheEntry): void {
110
- // If key exists, remove to re-insert at end
111
- this.map.delete(key);
112
-
113
- // Evict oldest if at capacity
114
- if (this.map.size >= this.maxSize) {
115
- const oldest = this.map.keys().next().value;
116
- if (oldest !== undefined) {
117
- this.map.delete(oldest);
118
- }
119
- }
120
-
121
- this.map.set(key, entry);
122
- }
123
-
124
- get size(): number {
125
- return this.map.size;
126
- }
127
-
128
- clear(): void {
129
- this.map.clear();
130
- }
131
- }
132
-
133
- // ─── Response Cache ────────────────────────────────────────────────────────
134
-
135
- export interface ResponseCache {
136
- /**
137
- * Wrap a render function with singleflight dedup + LRU caching.
138
- * Returns the cached Response or executes the render function.
139
- */
140
- getOrRender(
141
- req: Request,
142
- isRscPayload: boolean,
143
- renderFn: () => Promise<Response>
144
- ): Promise<Response>;
145
-
146
- /** Number of entries currently in the LRU cache. */
147
- readonly size: number;
148
-
149
- /** Clear all cached entries. */
150
- clear(): void;
151
- }
152
-
153
- // ─── Cache-Control parsing ─────────────────────────────────────────────────
154
-
155
- /**
156
- * Check if a Cache-Control header value contains directives that forbid
157
- * storing the response in a shared cache. We check for `no-store` and
158
- * `private` — both indicate the response must not be reused.
159
- *
160
- * This is intentionally simple: we don't parse `max-age`, `s-maxage`,
161
- * `must-revalidate`, etc. This is a short-TTL render cache, not an HTTP
162
- * cache — we just need to respect explicit "don't cache this" signals.
163
- */
164
- function hasCacheControlNoStore(headerValue: string | null): boolean {
165
- if (!headerValue) return false;
166
- // Split on comma, trim whitespace, check for no-store or private directives.
167
- // Case-insensitive per HTTP spec.
168
- const lower = headerValue.toLowerCase();
169
- return lower.includes('no-store') || lower.includes('private');
170
- }
171
-
172
- // ─── Vary header handling ──────────────────────────────────────────────────
173
-
174
- /**
175
- * Parse a Vary header value into a sorted list of header names.
176
- * Returns null if there is no Vary header or it's empty.
177
- * Returns ['*'] if Vary: * (meaning the response varies on everything —
178
- * effectively uncacheable).
179
- */
180
- function parseVaryHeader(headerValue: string | null): string[] | null {
181
- if (!headerValue) return null;
182
- const trimmed = headerValue.trim();
183
- if (trimmed === '') return null;
184
- if (trimmed === '*') return ['*'];
185
-
186
- // Split on comma, normalize to lowercase, sort for deterministic keys
187
- return trimmed
188
- .split(',')
189
- .map((h) => h.trim().toLowerCase())
190
- .filter((h) => h.length > 0)
191
- .sort();
192
- }
193
-
194
- /**
195
- * Build a Vary-aware suffix for the cache key. For each header name in the
196
- * Vary list, append the request's value for that header. This ensures that
197
- * requests with different Accept-Language (for example) get different cache
198
- * entries.
199
- */
200
- function buildVarySuffix(req: Request, varyHeaders: string[]): string {
201
- return varyHeaders.map((h) => `${h}=${req.headers.get(h) ?? ''}`).join('&');
202
- }
203
-
204
- // ─── Factory ───────────────────────────────────────────────────────────────
205
-
206
- /**
207
- * Create a response cache with singleflight deduplication and LRU caching.
208
- */
209
- export function createResponseCache(config: ResolvedResponseCacheConfig): ResponseCache {
210
- const lru = new LruCache(config.maxSize, config.ttlMs);
211
- const singleflight = createSingleflight();
212
-
213
- /**
214
- * Known Vary headers per path. When a response includes a Vary header,
215
- * we store the parsed header names so that subsequent requests to the
216
- * same path can include the Vary'd header values in their cache key
217
- * BEFORE rendering (i.e., on cache lookup, not just after the first
218
- * render). This avoids the "first request always misses" problem for
219
- * Vary'd responses.
220
- */
221
- const knownVaryHeaders = new Map<string, string[]>();
222
-
223
- function buildCacheKey(req: Request, isRscPayload: boolean): string | null {
224
- // Never cache non-GET requests. POST/PUT/DELETE have side effects and
225
- // may carry per-request state (e.g., form flash data via ALS) that makes
226
- // the rendered output unique even for the same URL.
227
- if (req.method !== 'GET') return null;
228
-
229
- // When publicOnly is true, skip caching for authenticated requests
230
- if (config.publicOnly) {
231
- if (req.headers.has('Cookie') || req.headers.has('Authorization')) {
232
- return null;
233
- }
234
- }
235
-
236
- const url = new URL(req.url);
237
- // Include search params in the cache key. Pages that use searchParams
238
- // (e.g., ?sort=asc, ?page=2) produce different output per query string.
239
- let key = `${url.pathname}${url.search}:${isRscPayload ? 'rsc' : 'html'}`;
240
-
241
- // If we've seen a Vary header for this path before, include the varied
242
- // request header values in the key so different variants get different
243
- // cache entries.
244
- const pathKey = `${url.pathname}:${isRscPayload ? 'rsc' : 'html'}`;
245
- const varyHeaders = knownVaryHeaders.get(pathKey);
246
- if (varyHeaders) {
247
- if (varyHeaders[0] === '*') {
248
- // Vary: * means the response varies on everything — uncacheable
249
- return null;
250
- }
251
- key += ':' + buildVarySuffix(req, varyHeaders);
252
- }
253
-
254
- return key;
255
- }
256
-
257
- /**
258
- * Check if a response is cacheable.
259
- * Responses with Set-Cookie headers, Cache-Control: no-store/private,
260
- * error/redirect status codes, or Vary: * are never cached.
261
- */
262
- function isCacheable(response: Response): boolean {
263
- // Don't cache error responses
264
- if (response.status >= 400) return false;
265
-
266
- // Don't cache redirects
267
- if (response.status >= 300 && response.status < 400) return false;
268
-
269
- // Don't cache responses with Set-Cookie (user-specific state)
270
- if (response.headers.has('Set-Cookie')) return false;
271
-
272
- // Respect Cache-Control: no-store and private directives.
273
- // If the application explicitly says "don't cache this," we obey.
274
- if (hasCacheControlNoStore(response.headers.get('Cache-Control'))) return false;
275
-
276
- // Vary: * means the response varies on everything — don't cache
277
- const vary = parseVaryHeader(response.headers.get('Vary'));
278
- if (vary && vary[0] === '*') return false;
279
-
280
- // Only cache responses with a body
281
- if (!response.body) return false;
282
-
283
- return true;
284
- }
285
-
286
- /** Construct a fresh Response from a cache entry (each caller gets their own). */
287
- function responseFromEntry(entry: CacheEntry): Response {
288
- // Null-body statuses (204, 304) cannot have a body per HTTP spec.
289
- // The Response constructor throws if you pass a body with these statuses.
290
- const isNullBody = entry.status === 204 || entry.status === 304;
291
- // slice(0) creates a copy so each caller owns their buffer
292
- return new Response(isNullBody ? null : entry.body.slice(0), {
293
- status: entry.status,
294
- headers: entry.headers,
295
- });
296
- }
297
-
298
- /**
299
- * Record the Vary header from a response so future requests to the same
300
- * path include varied header values in their cache key.
301
- */
302
- function recordVaryHeaders(pathKey: string, response: Response): void {
303
- const vary = parseVaryHeader(response.headers.get('Vary'));
304
- if (vary) {
305
- knownVaryHeaders.set(pathKey, vary);
306
- }
307
- }
308
-
309
- return {
310
- async getOrRender(
311
- req: Request,
312
- isRscPayload: boolean,
313
- renderFn: () => Promise<Response>
314
- ): Promise<Response> {
315
- const cacheKey = buildCacheKey(req, isRscPayload);
316
-
317
- // No cache key = skip caching and singleflight entirely.
318
- // This covers POST requests, authenticated requests (publicOnly),
319
- // and Vary: * responses.
320
- if (cacheKey === null) {
321
- return renderFn();
322
- }
323
-
324
- // Check LRU cache first
325
- const cached = lru.get(cacheKey);
326
- if (cached) {
327
- return responseFromEntry(cached);
328
- }
329
-
330
- // Singleflight: concurrent requests to the same key share one render.
331
- // We buffer the response body into an ArrayBuffer so ALL callers —
332
- // including the singleflight leader — get independent copies.
333
- // This fixes the body-loss bug where the leader consumed the body
334
- // and concurrent waiters got an empty response.
335
- const result: CacheEntry | null = await singleflight.do(cacheKey, async () => {
336
- const response = await renderFn();
337
-
338
- // Record Vary headers for future cache key construction
339
- const url = new URL(req.url);
340
- const pathKey = `${url.pathname}:${isRscPayload ? 'rsc' : 'html'}`;
341
- recordVaryHeaders(pathKey, response);
342
-
343
- if (!isCacheable(response)) {
344
- // Buffer the body even for non-cacheable responses so the
345
- // singleflight leader and all concurrent waiters each get
346
- // an independent copy. Without this, the leader consumes
347
- // the body stream and waiters get an empty response.
348
- const body = await response.arrayBuffer();
349
- const headers: [string, string][] = [];
350
- response.headers.forEach((value, key) => {
351
- headers.push([key, value]);
352
- });
353
- // Return as a CacheEntry shape but DON'T store in LRU.
354
- // Callers construct Responses from this, but it won't be
355
- // reused for future requests.
356
- return {
357
- body,
358
- status: response.status,
359
- headers,
360
- createdAt: Date.now(),
361
- vary: response.headers.get('Vary'),
362
- };
363
- }
364
-
365
- // Buffer the response body for caching.
366
- const body = await response.arrayBuffer();
367
- const headers: [string, string][] = [];
368
- response.headers.forEach((value, key) => {
369
- headers.push([key, value]);
370
- });
371
-
372
- const entry: CacheEntry = {
373
- body,
374
- status: response.status,
375
- headers,
376
- createdAt: Date.now(),
377
- vary: response.headers.get('Vary'),
378
- };
379
-
380
- // Re-check the cache key now that we know the Vary headers.
381
- // The initial key may not have included Vary'd header values
382
- // if this was the first request to this path. Rebuild the key
383
- // with the now-known Vary headers for correct LRU storage.
384
- const updatedKey = buildCacheKey(req, isRscPayload);
385
- if (updatedKey) {
386
- lru.set(updatedKey, entry);
387
- }
388
-
389
- return entry;
390
- });
391
-
392
- if (result === null) {
393
- // Shouldn't happen — singleflight always returns a result.
394
- // Defensive fallback: re-render.
395
- return renderFn();
396
- }
397
-
398
- return responseFromEntry(result);
399
- },
400
-
401
- get size() {
402
- return lru.size;
403
- },
404
-
405
- clear() {
406
- lru.clear();
407
- knownVaryHeaders.clear();
408
- },
409
- };
410
- }