@ultimat3/render 27.7.0 → 27.8.0

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.
package/src/render-isr.ts CHANGED
@@ -5,125 +5,39 @@
5
5
  * and tag-driven staleness (an action's `invalidates` marks exactly the dependent routes).
6
6
  */
7
7
 
8
- import type { CacheTag, Revalidator } from '@ultimat3/cache';
8
+ import type { CacheTag, Revalidator, TagRevalidator } from '@ultimat3/cache';
9
9
  import {
10
10
  dependentsOfKind,
11
+ EVERY_TAG,
11
12
  invalidateTags,
12
13
  markInvalidated,
14
+ parseTag,
13
15
  registerDependent,
14
16
  registerRevalidator,
15
17
  sampleFence,
16
- surrogateKeys,
18
+ tagsIntersect,
17
19
  unregisterDependent,
18
20
  } from '@ultimat3/cache';
19
- import type { Scheduler } from '@ultimat3/core';
20
21
  import { finiteCount, logger, renderThrowable, singleFlight } from '@ultimat3/core';
21
- import { unlocalizedPath } from '@ultimat3/i18n';
22
- import { parseTtlMs } from './duration';
23
- import { finiteStatus, isRenderStatus } from './finite-status';
22
+ import { finiteStatus } from './finite-status';
24
23
  import type { RouteDescriptor } from './registry';
25
24
  import { describePages } from './registry';
26
- import type { IsrEntry, IsrState, IsrStore } from './render-isr-store';
25
+ import { DEFAULT_ISR_FAILURE_COOLDOWN_MS, failureCooldown } from './render-isr-cooldown';
26
+ import { entryTtlMs, isrResult } from './render-isr-result';
27
+ import type { IsrPolicy } from './render-isr-routes';
28
+ import { isrPolicyOf, isrRouteFor } from './render-isr-routes';
29
+ import type { IsrEntry } from './render-isr-store';
27
30
  import { memoryIsrStore } from './render-isr-store';
28
- import { contentHash, staticHeaders } from './render-static';
31
+ import type {
32
+ Generation,
33
+ IsrController,
34
+ IsrControllerOptions,
35
+ IsrFence,
36
+ IsrRendered,
37
+ IsrRenderFn,
38
+ } from './render-isr-types';
39
+ import { contentHash } from './render-static';
29
40
  import type { RenderResult } from './route';
30
- import { compilePattern } from './route-pattern';
31
-
32
- /**
33
- * The reserved query parameter the negotiated locale rides in. A parameter and not a prefix
34
- * because `routePathOf` splits a key at its `?`: a `es:/blog` key would match no route, so
35
- * `descriptorFor` would answer `undefined` and a declared `revalidate: { ttl }` would silently
36
- * become tag-only. Reserved spelling, so an app's own `?locale=` stays its own dimension.
37
- */
38
- export const ISR_LOCALE_PARAM = '__x_locale';
39
-
40
- /**
41
- * The ISR store key for one request URL: pathname, the negotiated LOCALE, and the query, **params
42
- * sorted**.
43
- *
44
- * Exported because deriving it is the caller's job and there may only be ONE derivation — a
45
- * server that keyed on `url.pathname` while the store believed it held a whole URL is the shape
46
- * of #171. Sorting makes `?a=1&b=2` and `?b=2&a=1` one entry rather than two renders of one page.
47
- *
48
- * The locale is REQUIRED, and required as an argument rather than read from the ambient context so
49
- * that every call site has to answer: a document is rendered with `<html lang>` and every `t()` in
50
- * the request's own locale, so one entry per path served visitor 2 the document negotiated for
51
- * visitor 1 — for the whole TTL, and with `s-maxage` telling the CDN to do the same. The time zone
52
- * is deliberately NOT a dimension: it is unbounded where a locale set is declared, and an `isr`
53
- * page is a shared artifact, so a date on one belongs in an explicit zone the page itself names.
54
- *
55
- * A query-carrying URL therefore gets its own entry, which is correct and is not free: a crawler
56
- * appending `?utm_source=…` mints one entry per value. That is bounded, not unbounded —
57
- * `DEFAULT_ISR_MAX_ENTRIES` evicts least-recently-generated first — and a bounded cache that
58
- * thrashes is the right failure next to an unbounded one that answers the wrong document.
59
- */
60
- export function isrKey(url: URL, locale: string): string {
61
- const params = new URLSearchParams(url.search);
62
- params.set(ISR_LOCALE_PARAM, locale);
63
- params.sort();
64
- return `${url.pathname}?${params.toString()}`;
65
- }
66
-
67
- /**
68
- * The path the ROUTE TABLE knows a key by. Every lookup that asks the table a question — the
69
- * descriptor, and therefore the TTL and the tags — has to undo what the key carries beyond the
70
- * route, in the order the router does before it matches (`@ultimat3/http`'s `routing` stage):
71
- *
72
- * - the query: `descriptorFor('/blog?page=2')` matches no route;
73
- * - the routed locale prefix: `/en/blog/a` is the `/blog/:slug` route in English. The key KEEPS the
74
- * prefix — it is the URL the document was rendered for, `canonical` and all — and the table has
75
- * never heard of it. Left on, a prefixed page matched no route: `ttlMs: null` and no tag edge, so
76
- * it was fresh forever while the default locale's copy of the same page went stale on schedule.
77
- *
78
- * `unlocalizedPath` is i18n's, the one the router's own split is built on: a segment that names no
79
- * routed locale is left alone, so `/fr/blog` is still no route.
80
- */
81
- function routePathOf(key: string): string {
82
- const query = key.indexOf('?');
83
- return unlocalizedPath(query === -1 ? key : key.slice(0, query));
84
- }
85
-
86
- /**
87
- * A render that also answers a status — what a loader's `withStatus(404, …)` becomes once the
88
- * document is built. A bare string is the 200 every render before this one was: the union is
89
- * additive, and a render function that never learned the object shape keeps compiling.
90
- */
91
- export interface IsrRendered {
92
- readonly html: string;
93
- readonly status: number;
94
- }
95
-
96
- export type IsrRenderFn = (path: string) => string | IsrRendered | Promise<string | IsrRendered>;
97
-
98
- /** One shape for the generator, so nothing below branches on what the render handed back. */
99
- function renderedOf(rendered: string | IsrRendered): IsrRendered {
100
- return typeof rendered === 'string' ? { html: rendered, status: 200 } : rendered;
101
- }
102
-
103
- export interface IsrServeResult {
104
- readonly state: IsrState;
105
- readonly entry: IsrEntry;
106
- readonly result: RenderResult;
107
- /** True when this request started a background regeneration. */
108
- readonly regenerating: boolean;
109
- }
110
-
111
- export interface IsrControllerOptions {
112
- readonly store?: IsrStore;
113
- readonly buildId?: string;
114
- readonly now?: () => number;
115
- /** Route table provider — defaults to the real registry. */
116
- readonly routes?: () => readonly RouteDescriptor[];
117
- /** ISR-route dependents for a tag set; defaults to `@ultimat3/cache`'s graph. */
118
- readonly isrDependents?: (tags: readonly CacheTag[]) => readonly string[];
119
- /**
120
- * How long one regeneration may hold its path, in whole ms (≥ 1). Defaults to
121
- * `DEFAULT_ISR_REGENERATE_DEADLINE_MS`. There is no "forever": that is the bug it bounds.
122
- */
123
- readonly regenerateDeadlineMs?: number | undefined;
124
- /** Injected so the deadline is provable without waiting one out. */
125
- readonly schedule?: Scheduler | undefined;
126
- }
127
41
 
128
42
  /**
129
43
  * The ceiling on one regeneration's hold on its path — `@ultimat3/http`'s default
@@ -135,21 +49,18 @@ export interface IsrControllerOptions {
135
49
  */
136
50
  export const DEFAULT_ISR_REGENERATE_DEADLINE_MS = 30_000;
137
51
 
138
- export interface IsrController {
139
- serve(path: string, render: IsrRenderFn): Promise<IsrServeResult>;
140
- /** Single-flight: concurrent callers for the same path share one render. */
141
- regenerate(path: string, render: IsrRenderFn): Promise<IsrEntry>;
142
- markStale(path: string): boolean;
143
- /** Mark every ISR page the cache graph says depends on these tags. */
144
- revalidateByTags(tags: readonly CacheTag[]): readonly string[];
145
- inflight(): number;
146
- store(): IsrStore;
147
- /**
148
- * Register this controller as the framework's revalidator, so
149
- * `action({ cache: { invalidates: [tag.post] } })` reaches ISR in the same hop as
150
- * memo, LRU, Redis and the CDN. Returns a detach function for tests and reloads.
151
- */
152
- attach(): () => void;
52
+ /** A 5xx is the app's "the read failed, ask again" — an answer about a moment, never a document. */
53
+ const isServerError = (status: number): boolean => status >= 500;
54
+
55
+ const PROCESS_FENCE: IsrFence = { sample: sampleFence, mark: markInvalidated };
56
+
57
+ /** The store's fence is keyed by tag ENTITY: `post` for `post` and `post:1` alike. */
58
+ const entitiesOf = (tags: readonly CacheTag[]): readonly string[] =>
59
+ [...new Set(tags.map((one) => one.entity))].sort();
60
+
61
+ /** One shape for the generator, so nothing below branches on what the render handed back. */
62
+ function renderedOf(rendered: string | IsrRendered): IsrRendered {
63
+ return typeof rendered === 'string' ? { html: rendered, status: 200 } : rendered;
153
64
  }
154
65
 
155
66
  /**
@@ -185,16 +96,17 @@ export function isrController(options: IsrControllerOptions = {}): IsrController
185
96
  */
186
97
  const latest = new Map<string, object>();
187
98
  const registered = new Set<string>();
99
+ const fences = options.fence ?? PROCESS_FENCE;
100
+ const failed = failureCooldown<Generation>(
101
+ finiteCount(
102
+ 'isrController',
103
+ 'failureCooldownMs',
104
+ options.failureCooldownMs ?? DEFAULT_ISR_FAILURE_COOLDOWN_MS,
105
+ ),
106
+ now,
107
+ );
188
108
 
189
- function descriptorFor(key: string): RouteDescriptor | undefined {
190
- const path = routePathOf(key);
191
- const table = routes();
192
- return table.find((r) => r.path === path) ?? matchersOf(table).find((m) => m.test(path))?.route;
193
- }
194
-
195
- /** The route's `revalidate.tags` for a store key — what its document is purged by at the edge. */
196
- const tagsOf = (key: string): readonly CacheTag[] =>
197
- (descriptorFor(key)?.revalidateTags ?? []).map(parseWireTag);
109
+ const descriptorFor = (key: string): RouteDescriptor | undefined => isrRouteFor(routes(), key);
198
110
 
199
111
  /**
200
112
  * A rendered page joins the invalidation graph under its route's tags, so `/blog/a` and
@@ -203,10 +115,15 @@ export function isrController(options: IsrControllerOptions = {}): IsrController
203
115
  function registerPath(path: string, descriptor: RouteDescriptor | undefined): void {
204
116
  if (descriptor === undefined || registered.has(path)) return;
205
117
  if (descriptor.revalidateTags.length === 0) return;
206
- registerDependent(descriptor.revalidateTags.map(parseWireTag), { kind: 'isr-route', id: path });
118
+ registerDependent(descriptor.revalidateTags.map(parseTag), { kind: 'isr-route', id: path });
207
119
  registered.add(path);
208
120
  }
209
121
 
122
+ function forgetPath(path: string): void {
123
+ if (!registered.delete(path)) return;
124
+ unregisterDependent({ kind: 'isr-route', id: path });
125
+ }
126
+
210
127
  /**
211
128
  * A registration is only true while the store still holds the page. The store evicts silently
212
129
  * and offers no callback — and a custom `IsrStore` need not have one at all — so the store's own
@@ -216,12 +133,10 @@ export function isrController(options: IsrControllerOptions = {}): IsrController
216
133
  */
217
134
  function forgetEvictedPaths(): void {
218
135
  if (registered.size === 0) return;
219
- const live = new Set(store.paths());
220
- for (const path of registered) {
221
- if (live.has(path)) continue;
222
- unregisterDependent({ kind: 'isr-route', id: path });
223
- registered.delete(path);
224
- }
136
+ // A page whose FIRST render is still running is in no store yet and is not evicted: forgotten
137
+ // here, a bust arriving before it settled found it in neither the graph nor the store.
138
+ const live = new Set([...store.paths(), ...flight.keys()]);
139
+ for (const path of registered) if (!live.has(path)) forgetPath(path);
225
140
  }
226
141
 
227
142
  function isFresh(entry: IsrEntry): boolean {
@@ -231,13 +146,25 @@ export function isrController(options: IsrControllerOptions = {}): IsrController
231
146
  return now() - entry.generatedAt < ttlMs;
232
147
  }
233
148
 
149
+ /**
150
+ * `revalidate.maxStale`: past its TTL by more than the route allows, a copy is not an answer any
151
+ * more. Without a bound, stale-while-revalidate serves the stored page ONCE MORE however old it
152
+ * is — a page nobody asked for in a week answers its next visitor with last week's document.
153
+ */
154
+ function pastMaxStale(entry: IsrEntry, policy: IsrPolicy): boolean {
155
+ const ttlMs = entryTtlMs(entry);
156
+ if (policy.maxStaleMs === null || ttlMs === null) return false;
157
+ return now() - entry.generatedAt >= ttlMs + policy.maxStaleMs;
158
+ }
159
+
234
160
  /** One regeneration of `path`; `led` is told when THIS call started it rather than joined one. */
235
- function regenerateOnce(path: string, render: IsrRenderFn, led?: () => void): Promise<IsrEntry> {
236
- return flight.run(path, async (): Promise<IsrEntry> => {
161
+ function generate(path: string, render: IsrRenderFn, led?: () => void): Promise<Generation> {
162
+ return flight.run(path, async (): Promise<Generation> => {
237
163
  led?.();
238
164
  const run = {};
239
165
  latest.set(path, run);
240
166
  const descriptor = descriptorFor(path);
167
+ const policy = isrPolicyOf(descriptor);
241
168
  try {
242
169
  // BEFORE the render, not after: `revalidateByTags` reads the graph, so a bust arriving
243
170
  // while a cold path's first render was in flight could not see the page it was
@@ -248,46 +175,144 @@ export function isrController(options: IsrControllerOptions = {}): IsrController
248
175
  // and a `markStale` landing in between was then ERASED by `store.set({ stale: false })`.
249
176
  // For a tag-only route `isFresh` is true forever, so the process went on serving pre-write
250
177
  // HTML for the rest of its life. One mechanism, not a second one grown here.
251
- const fence = sampleFence({
252
- key: path,
253
- tags: (descriptor?.revalidateTags ?? []).map(parseWireTag),
254
- });
255
- const { html, status } = renderedOf(await render(path));
178
+ const fence = fences.sample({ key: path, tags: policy.tags });
179
+ // And the STORE's own fence, for a store another process writes: read before the rows are.
180
+ const entities = entitiesOf(policy.tags);
181
+ const reading = store.tagFence?.sample(entities);
182
+ const rendered = renderedOf(await render(path));
256
183
  const entry: IsrEntry = {
257
184
  path,
258
- html,
259
- hash: contentHash(html),
185
+ html: rendered.html,
186
+ hash: contentHash(rendered.html),
260
187
  generatedAt: now(),
261
- ttlMs: parseTtlMs(descriptor?.revalidateTtl),
188
+ ttlMs: policy.ttlMs,
262
189
  stale: false,
263
190
  // Screened at generation, the one place a status enters the store: a `NaN` written here
264
191
  // would be served for the whole TTL as a `RangeError` on every hit.
265
- status: finiteStatus('IsrRenderFn', status),
192
+ status: finiteStatus('IsrRenderFn', rendered.status),
266
193
  };
267
- // Refused, never published stale-flagged: the next request re-renders from rows that now
268
- // include the write, where a stored-but-stale entry would serve this pre-write body once
269
- // more before doing the same thing. Refused too once a newer run started (`latest`).
270
- if (fence.isValid() && latest.get(path) === run) store.set(entry);
194
+ const current = latest.get(path) === run;
195
+ const unstorable = isServerError(rendered.status) || rendered.noStore === true;
196
+ // Kept only when nothing invalidated the page while it rendered — in this process (the
197
+ // fence) or in any other (the store's) — and no newer run started. Refused, never
198
+ // published stale-flagged: the next request re-renders from rows that include the write.
199
+ const kept = !unstorable && fence.isValid() && current && write(entry, entities, reading);
200
+ if (isServerError(rendered.status)) {
201
+ // Never stored, and never over a good copy: one upstream blip stored for the TTL plus a
202
+ // stale serve was minutes of 503 for a read that recovered in a second. The copy the
203
+ // store holds stays (still stale, so the next request tries again); a miss has none, and
204
+ // answers this 5xx to its own request only.
205
+ logger.warn('isr.render.unstored', { path, status: String(rendered.status) });
206
+ } else if (rendered.noStore === true) {
207
+ // The loader's answer is not a failure, so the copy held is no longer the page.
208
+ if (current) store.delete(path);
209
+ }
271
210
  forgetEvictedPaths();
272
- return entry;
211
+ // What the store did not keep, the edge may not keep either: a page rendered from rows
212
+ // read BEFORE a purge would otherwise reach the CDN after the purge had already run there.
213
+ return { entry, cacheable: kept, fenced: !unstorable && !kept };
273
214
  } finally {
274
215
  if (latest.get(path) === run) latest.delete(path);
275
216
  }
276
217
  });
277
218
  }
278
219
 
279
- function regenerate(path: string, render: IsrRenderFn): Promise<IsrEntry> {
280
- return regenerateOnce(path, render);
220
+ /** The one write: through the store's own fence when it has one, so a stale reading is refused. */
221
+ function write(
222
+ entry: IsrEntry,
223
+ entities: readonly string[],
224
+ reading: string | undefined,
225
+ ): boolean {
226
+ if (store.tagFence === undefined || reading === undefined) {
227
+ store.set(entry);
228
+ return true;
229
+ }
230
+ return store.tagFence.setIfCurrent(entry, entities, reading);
231
+ }
232
+
233
+ /**
234
+ * A generation FOR one request. A request that joined a render someone else started may have
235
+ * arrived after the bust that fenced it, so it does not take that page: it renders once more,
236
+ * and by then the fenced flight has settled, so this one began after the bust. A 5xx is
237
+ * remembered for the cooldown, so the requests right behind it are not each a failing render.
238
+ */
239
+ async function generateFor(path: string, render: IsrRenderFn): Promise<Generation> {
240
+ let led = false;
241
+ const lead = (): void => {
242
+ led = true;
243
+ };
244
+ const joined = await generate(path, render, lead);
245
+ const made = !led && joined.fenced ? await generate(path, render) : joined;
246
+ if (isServerError(made.entry.status ?? 200)) failed.remember(path, made);
247
+ return made;
248
+ }
249
+
250
+ async function regenerate(path: string, render: IsrRenderFn): Promise<IsrEntry> {
251
+ return (await generate(path, render)).entry;
281
252
  }
282
253
 
283
254
  function markStale(path: string): boolean {
284
255
  // The mark is recorded whether or not the store holds the page: a regeneration already in
285
256
  // flight for a path this store has never held is exactly the case the fence above exists for,
286
257
  // and `invalidateTags`' own fanout only marks the TAGS.
287
- markInvalidated({ key: path });
258
+ fences.mark({ key: path });
288
259
  return store.markStale(path);
289
260
  }
290
261
 
262
+ /**
263
+ * What a tag bust does to one page, as its route declared (`revalidate.onInvalidate`). `'purge'`
264
+ * DELETES: the next request is a miss that renders and blocks, where `'stale'` answers the copy
265
+ * once more, however old — the right trade for a price list, and the wrong one for an article
266
+ * that was withdrawn. The fence is voided either way, so a render in flight cannot put it back —
267
+ * and its FLIGHT is evicted, so a request arriving after this call leads a render of its own
268
+ * instead of joining one that read its rows before the write (up to the 30 s deadline of them).
269
+ */
270
+ function invalidate(path: string): void {
271
+ failed.clear(path);
272
+ flight.evict(path);
273
+ if (isrPolicyOf(descriptorFor(path)).onInvalidate !== 'purge') {
274
+ markStale(path);
275
+ return;
276
+ }
277
+ fences.mark({ key: path });
278
+ store.delete(path);
279
+ forgetPath(path);
280
+ }
281
+
282
+ /**
283
+ * Everything HELD under these tags, asked of the store, the flights and the route table rather
284
+ * than of the graph. The graph is this process's memory of what it rendered: an entry another
285
+ * controller wrote into a shared store, one that outlived a restart, or a cold page whose first
286
+ * render is still running, has no edge there — and under `'purge'` that is the withdrawn page,
287
+ * still served. No route carrying the tags is the common case, and costs no store read.
288
+ *
289
+ * By ROUTE tags: a row bust (`post:1`) reaches every stored page of a route tagged `post`. An
290
+ * entry records no tags of its own, so this errs toward one render too many, never one too few.
291
+ */
292
+ function revalidateHeld(tags: readonly CacheTag[] | typeof EVERY_TAG): readonly string[] {
293
+ const table = routes();
294
+ // A flush (`EVERY_TAG`) is a bust of every tag at once: every route that carries one owns.
295
+ const owns = (route: RouteDescriptor): boolean =>
296
+ tags === EVERY_TAG
297
+ ? route.revalidateTags.length > 0
298
+ : tagsIntersect(tags, route.revalidateTags.map(parseTag));
299
+ const owners = new Set(table.filter(owns));
300
+ if (owners.size === 0) return [];
301
+ const owned = (key: string): boolean => {
302
+ const route = isrRouteFor(table, key);
303
+ return route !== undefined && owners.has(route);
304
+ };
305
+ // The store's fence FIRST: a write racing this call from another process is refused from here.
306
+ const busted =
307
+ tags === EVERY_TAG ? [...owners].flatMap((r) => r.revalidateTags.map(parseTag)) : tags;
308
+ store.tagFence?.bump(entitiesOf(busted));
309
+ for (const key of flight.keys()) if (owned(key)) invalidate(key);
310
+ const held = store.paths().filter(owned);
311
+ // A page the graph already named was marked a moment ago; it is reported, not marked twice.
312
+ for (const path of held) if (store.get(path)?.stale !== true) invalidate(path);
313
+ return held;
314
+ }
315
+
291
316
  return {
292
317
  store: () => store,
293
318
  inflight: () => flight.size,
@@ -295,56 +320,73 @@ export function isrController(options: IsrControllerOptions = {}): IsrController
295
320
  markStale,
296
321
 
297
322
  async serve(path, render) {
298
- const cached = store.get(path);
323
+ const descriptor = descriptorFor(path);
324
+ const policy = isrPolicyOf(descriptor);
325
+ const answer = (entry: IsrEntry, servedStale = false, cacheable = true): RenderResult =>
326
+ isrResult(entry, {
327
+ buildId,
328
+ policy,
329
+ servedStale,
330
+ cacheable,
331
+ ageMs: Math.max(0, now() - entry.generatedAt),
332
+ });
333
+ let cached = store.get(path);
334
+ if (cached !== undefined && pastMaxStale(cached, policy)) {
335
+ store.delete(path);
336
+ cached = undefined;
337
+ }
299
338
 
300
339
  if (cached === undefined) {
301
- const entry = await regenerate(path, render);
302
- const result = toResult(entry, buildId, tagsOf(path));
303
- return { state: 'miss', entry, result, regenerating: false };
340
+ const made = failed.get(path) ?? (await generateFor(path, render));
341
+ const result = answer(made.entry, false, made.cacheable);
342
+ return { state: 'miss', entry: made.entry, result, regenerating: false };
304
343
  }
305
344
 
345
+ // A hit on a page ANOTHER controller rendered into a shared store joins the graph here, so
346
+ // the report of the next bust names it; the bust itself reaches it through `heldUnder`.
347
+ registerPath(path, descriptor);
306
348
  if (isFresh(cached)) {
307
- return {
308
- state: 'hit',
309
- entry: cached,
310
- result: toResult(cached, buildId, tagsOf(path)),
311
- regenerating: false,
312
- };
349
+ return { state: 'hit', entry: cached, result: answer(cached), regenerating: false };
313
350
  }
314
351
 
315
- // stale-while-revalidate: answer from the stale copy now, refresh behind the request.
352
+ // stale-while-revalidate: answer from the stale copy now, refresh behind the request —
353
+ // unless the last refresh just failed, which the next one a moment later would repeat.
316
354
  let started = false;
317
- void regenerateOnce(path, render, () => {
355
+ if (failed.get(path) !== undefined) {
356
+ return { state: 'stale', entry: cached, result: answer(cached, true), regenerating: false };
357
+ }
358
+ void generate(path, render, () => {
318
359
  started = true;
319
- }).catch((error: unknown) => {
320
- // `renderThrowable`, never `.message`/`String()`: this `.catch` is the last frame under a
321
- // route's own render function, and `String()` raises on a null-prototype object — the
322
- // handler that exists to REPORT the failure became a second, unhandled rejection.
323
- logger.warn('isr.regenerate.failed', { path, error: renderThrowable(error) });
324
- });
325
- return {
326
- state: 'stale',
327
- entry: cached,
328
- result: toResult(cached, buildId, tagsOf(path), true),
329
- regenerating: started,
330
- };
360
+ })
361
+ .then((made) => {
362
+ if (isServerError(made.entry.status ?? 200)) failed.remember(path, made);
363
+ })
364
+ .catch((error: unknown) => {
365
+ // `renderThrowable`, never `.message`/`String()`: this `.catch` is the last frame under a
366
+ // route's own render function, and `String()` raises on a null-prototype object — the
367
+ // handler that exists to REPORT the failure became a second, unhandled rejection.
368
+ logger.warn('isr.regenerate.failed', { path, error: renderThrowable(error) });
369
+ });
370
+ return { state: 'stale', entry: cached, result: answer(cached, true), regenerating: started };
331
371
  },
332
372
 
333
373
  revalidateByTags(tags) {
334
- const affected = new Set<string>();
335
- for (const path of isrDependents(tags)) {
336
- markStale(path);
337
- affected.add(path);
338
- }
374
+ // By TAG first, as `invalidateTags` does before it calls in here: a cold page whose first
375
+ // render is running may be in neither the graph nor the store, and only its fence finds it.
376
+ fences.mark({ tags });
377
+ const affected = new Set<string>(isrDependents(tags));
378
+ for (const path of affected) invalidate(path);
379
+ for (const path of revalidateHeld(tags)) affected.add(path);
339
380
  return [...affected].sort();
340
381
  },
341
382
 
342
383
  attach() {
343
- // The cache fanout owns the trigger; render owns only "what does stale mean here".
384
+ // The cache fanout owns the trigger; render owns only "what does an invalidation mean here".
344
385
  const revalidate: Revalidator = (path) => {
345
- markStale(path);
386
+ invalidate(path);
346
387
  };
347
- registerRevalidator(revalidate);
388
+ const revalidateTags: TagRevalidator = (tags) => revalidateHeld(tags);
389
+ registerRevalidator(revalidate, revalidateTags);
348
390
  installedRevalidator = revalidate;
349
391
  return () => {
350
392
  for (const path of registered) unregisterDependent({ kind: 'isr-route', id: path });
@@ -375,113 +417,3 @@ export async function invalidateAndRevalidate(
375
417
  const report = await invalidateTags(tags);
376
418
  return report.isr;
377
419
  }
378
-
379
- /** `post` / `post:123` → `{ entity, id? }`. Mirrors `@ultimat3/cache`'s wire form. */
380
- function parseWireTag(wire: string): CacheTag {
381
- const split = wire.indexOf(':');
382
- if (split === -1) return { entity: wire };
383
- return { entity: wire.slice(0, split), id: wire.slice(split + 1) };
384
- }
385
-
386
- interface RouteMatcher {
387
- readonly route: RouteDescriptor;
388
- test(storedPath: string): boolean;
389
- }
390
-
391
- /** One compiled set per route TABLE — `describePages()` hands out one array per registry change. */
392
- const compiledTables = new WeakMap<readonly RouteDescriptor[], readonly RouteMatcher[]>();
393
-
394
- /**
395
- * A stored path belongs to a route when the route's pattern matches it — `route-pattern.ts`'s one
396
- * pattern compiler, never a second. Ordered MOST SPECIFIC FIRST, as `@ultimat3/http`'s trie
397
- * resolves a request: segment by segment, a static beats a `:param` beats a `*catch-all`. In table
398
- * order `/docs/*path` sorts before `/docs/:id`, so the first match gave `/docs/7` the catch-all's
399
- * TTL and tags — a page rendered by one route and expired by another's clock.
400
- */
401
- function matchersOf(table: readonly RouteDescriptor[]): readonly RouteMatcher[] {
402
- const cached = compiledTables.get(table);
403
- if (cached !== undefined) return cached;
404
- // Static routes too: `/precios-españa` is the route's path and `/precios-espa%C3%B1a` the key's,
405
- // so only the pattern (raw or encoded, per character) finds it.
406
- const compiled = table
407
- .map((route): RouteMatcher & { readonly specificity: number } => {
408
- const { regex, specificity } = compilePattern(route.path);
409
- return { route, specificity, test: (stored) => regex.test(stored) };
410
- })
411
- .sort((a, b) => b.specificity - a.specificity);
412
- compiledTables.set(table, compiled);
413
- return compiled;
414
- }
415
-
416
- /** Tag-only routes have no clock of their own; a tag bust reaches the CDN through the fanout. */
417
- const TAG_ONLY_S_MAX_AGE_SECONDS = 60;
418
-
419
- /**
420
- * `IsrStore` is a driver seam, so an entry can come back from an app's own store — one backed by
421
- * Redis round-trips it through JSON, where a `ttlMs` that was never written reads back as
422
- * `undefined` and `entry.ttlMs === null` is then false. Two failures follow from that one value
423
- * and neither raises: `now - generatedAt < NaN` is false, so the page is NEVER fresh and every
424
- * request regenerates it, and the CDN is handed `s-maxage=NaN` — an unparseable directive a
425
- * conforming cache IGNORES, dropping the page to heuristic caching rather than to the declared
426
- * age. Read on the request path, so it is TOTAL rather than a throw: a ttl that is not a positive
427
- * finite number of milliseconds is the tag-only `null` `parseTtlMs` would have answered for it.
428
- */
429
- function entryTtlMs(entry: IsrEntry): number | null {
430
- const ttlMs = entry.ttlMs;
431
- if (ttlMs === null || (Number.isFinite(ttlMs) && ttlMs > 0)) return ttlMs;
432
- logger.warn('isr.entry_ttl_invalid', { path: entry.path, ttlMs: String(ttlMs) });
433
- return null;
434
- }
435
-
436
- /**
437
- * The declared TTL is the route's own contract with the CDN: a shared cache must not hold the
438
- * page longer than the app said it stays true. A flat `s-maxage=60` made `revalidate: { ttl:
439
- * '5m' }` a lie in one direction and `ttl: '30s'` a lie in the other.
440
- */
441
- function cacheControl(ttlMs: number | null): string {
442
- const sMaxAge = ttlMs === null ? TAG_ONLY_S_MAX_AGE_SECONDS : Math.round(ttlMs / 1_000);
443
-
444
- return `public, max-age=0, s-maxage=${sMaxAge}, stale-while-revalidate=86400`;
445
- }
446
-
447
- /**
448
- * `entryTtlMs`'s reason, one field over: a store may hand back an entry with no `status`, or one
449
- * that JSON turned into something else, on the request path. Absent is 200 — the only value any
450
- * entry carried before the field existed — and anything the range refuses is 200 with a warning,
451
- * because a stored number must not 500 the page for its whole TTL.
452
- */
453
- function entryStatus(entry: IsrEntry): number {
454
- const status = entry.status;
455
- if (status === undefined) return 200;
456
- if (isRenderStatus(status)) return status;
457
- logger.warn('isr.entry_status_invalid', { path: entry.path, status: String(status) });
458
- return 200;
459
- }
460
-
461
- function toResult(
462
- entry: IsrEntry,
463
- buildId: string,
464
- tags: readonly CacheTag[],
465
- servedStale = false,
466
- ): RenderResult {
467
- const headers: Record<string, string> = {
468
- ...staticHeaders(entry.hash, buildId),
469
- 'cache-control': cacheControl(entryTtlMs(entry)),
470
- // The store keys on the locale; a shared cache in front of it has to as well, or the CDN
471
- // repeats the bug this entry was split to fix. `ssrHeaders`' own line, for the same reason.
472
- // The rest of the shared key — the cookie, the zone — is added by `@ultimat3/http`'s
473
- // `cache-headers` stage, which sees the actor this function cannot.
474
- vary: 'accept-language',
475
- };
476
- // The keys an edge purges this document by — `@ultimat3/cache`'s list, never a second one, so
477
- // what a bust sends and what the page carries cannot drift. Absent, an `invalidates` cleared
478
- // every tier but the CDN, which held the document for its whole `s-maxage`. The tags were
479
- // screened when the route registered (`registry.ts`), so this cannot refuse on the request path.
480
- const keys = surrogateKeys(tags, 'isr');
481
- if (keys.length > 0) {
482
- headers['surrogate-key'] = keys.join(' ');
483
- headers['cache-tag'] = keys.join(',');
484
- }
485
- if (servedStale) headers['x-ultimate-isr'] = 'stale';
486
- return { status: entryStatus(entry), headers, body: entry.html };
487
- }