@ultimat3/render 27.6.1 → 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/README.md +19 -1
- package/package.json +6 -6
- package/src/duration.ts +14 -0
- package/src/index.ts +2 -1
- package/src/modes.ts +4 -0
- package/src/registry.ts +18 -26
- package/src/render-isr-cooldown.ts +44 -0
- package/src/render-isr-key.ts +95 -0
- package/src/render-isr-result.ts +125 -0
- package/src/render-isr-routes.ts +98 -0
- package/src/render-isr-store.ts +57 -11
- package/src/render-isr-types.ts +102 -0
- package/src/render-isr.ts +229 -289
- package/src/revalidate-shape.ts +100 -0
- package/src/route-status.ts +21 -0
- package/src/route.ts +29 -0
- package/src/server.ts +18 -10
package/src/render-isr.ts
CHANGED
|
@@ -5,117 +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
|
-
|
|
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 {
|
|
22
|
-
import { finiteStatus, isRenderStatus } from './finite-status';
|
|
22
|
+
import { finiteStatus } from './finite-status';
|
|
23
23
|
import type { RouteDescriptor } from './registry';
|
|
24
24
|
import { describePages } from './registry';
|
|
25
|
-
import
|
|
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';
|
|
26
30
|
import { memoryIsrStore } from './render-isr-store';
|
|
27
|
-
import {
|
|
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';
|
|
28
40
|
import type { RenderResult } from './route';
|
|
29
|
-
import { compilePattern } from './route-pattern';
|
|
30
|
-
|
|
31
|
-
/**
|
|
32
|
-
* The reserved query parameter the negotiated locale rides in. A parameter and not a prefix
|
|
33
|
-
* because `routePathOf` splits a key at its `?`: a `es:/blog` key would match no route, so
|
|
34
|
-
* `descriptorFor` would answer `undefined` and a declared `revalidate: { ttl }` would silently
|
|
35
|
-
* become tag-only. Reserved spelling, so an app's own `?locale=` stays its own dimension.
|
|
36
|
-
*/
|
|
37
|
-
export const ISR_LOCALE_PARAM = '__x_locale';
|
|
38
|
-
|
|
39
|
-
/**
|
|
40
|
-
* The ISR store key for one request URL: pathname, the negotiated LOCALE, and the query, **params
|
|
41
|
-
* sorted**.
|
|
42
|
-
*
|
|
43
|
-
* Exported because deriving it is the caller's job and there may only be ONE derivation — a
|
|
44
|
-
* server that keyed on `url.pathname` while the store believed it held a whole URL is the shape
|
|
45
|
-
* of #171. Sorting makes `?a=1&b=2` and `?b=2&a=1` one entry rather than two renders of one page.
|
|
46
|
-
*
|
|
47
|
-
* The locale is REQUIRED, and required as an argument rather than read from the ambient context so
|
|
48
|
-
* that every call site has to answer: a document is rendered with `<html lang>` and every `t()` in
|
|
49
|
-
* the request's own locale, so one entry per path served visitor 2 the document negotiated for
|
|
50
|
-
* visitor 1 — for the whole TTL, and with `s-maxage` telling the CDN to do the same. The time zone
|
|
51
|
-
* is deliberately NOT a dimension: it is unbounded where a locale set is declared, and an `isr`
|
|
52
|
-
* page is a shared artifact, so a date on one belongs in an explicit zone the page itself names.
|
|
53
|
-
*
|
|
54
|
-
* A query-carrying URL therefore gets its own entry, which is correct and is not free: a crawler
|
|
55
|
-
* appending `?utm_source=…` mints one entry per value. That is bounded, not unbounded —
|
|
56
|
-
* `DEFAULT_ISR_MAX_ENTRIES` evicts least-recently-generated first — and a bounded cache that
|
|
57
|
-
* thrashes is the right failure next to an unbounded one that answers the wrong document.
|
|
58
|
-
*/
|
|
59
|
-
export function isrKey(url: URL, locale: string): string {
|
|
60
|
-
const params = new URLSearchParams(url.search);
|
|
61
|
-
params.set(ISR_LOCALE_PARAM, locale);
|
|
62
|
-
params.sort();
|
|
63
|
-
return `${url.pathname}?${params.toString()}`;
|
|
64
|
-
}
|
|
65
|
-
|
|
66
|
-
/**
|
|
67
|
-
* The route pattern a key belongs to. Every lookup that asks the ROUTE TABLE a question — the
|
|
68
|
-
* descriptor, and therefore the TTL — has to strip the query first: `descriptorFor('/blog?page=2')`
|
|
69
|
-
* matches no route, so the entry would silently fall back to `ttlMs: null` and a declared
|
|
70
|
-
* `revalidate: { ttl: '5m' }` would become tag-only. Keying without this split is a half-fix that
|
|
71
|
-
* trades a leak for a wrong TTL.
|
|
72
|
-
*/
|
|
73
|
-
function routePathOf(key: string): string {
|
|
74
|
-
const query = key.indexOf('?');
|
|
75
|
-
return query === -1 ? key : key.slice(0, query);
|
|
76
|
-
}
|
|
77
|
-
|
|
78
|
-
/**
|
|
79
|
-
* A render that also answers a status — what a loader's `withStatus(404, …)` becomes once the
|
|
80
|
-
* document is built. A bare string is the 200 every render before this one was: the union is
|
|
81
|
-
* additive, and a render function that never learned the object shape keeps compiling.
|
|
82
|
-
*/
|
|
83
|
-
export interface IsrRendered {
|
|
84
|
-
readonly html: string;
|
|
85
|
-
readonly status: number;
|
|
86
|
-
}
|
|
87
|
-
|
|
88
|
-
export type IsrRenderFn = (path: string) => string | IsrRendered | Promise<string | IsrRendered>;
|
|
89
|
-
|
|
90
|
-
/** One shape for the generator, so nothing below branches on what the render handed back. */
|
|
91
|
-
function renderedOf(rendered: string | IsrRendered): IsrRendered {
|
|
92
|
-
return typeof rendered === 'string' ? { html: rendered, status: 200 } : rendered;
|
|
93
|
-
}
|
|
94
|
-
|
|
95
|
-
export interface IsrServeResult {
|
|
96
|
-
readonly state: IsrState;
|
|
97
|
-
readonly entry: IsrEntry;
|
|
98
|
-
readonly result: RenderResult;
|
|
99
|
-
/** True when this request started a background regeneration. */
|
|
100
|
-
readonly regenerating: boolean;
|
|
101
|
-
}
|
|
102
|
-
|
|
103
|
-
export interface IsrControllerOptions {
|
|
104
|
-
readonly store?: IsrStore;
|
|
105
|
-
readonly buildId?: string;
|
|
106
|
-
readonly now?: () => number;
|
|
107
|
-
/** Route table provider — defaults to the real registry. */
|
|
108
|
-
readonly routes?: () => readonly RouteDescriptor[];
|
|
109
|
-
/** ISR-route dependents for a tag set; defaults to `@ultimat3/cache`'s graph. */
|
|
110
|
-
readonly isrDependents?: (tags: readonly CacheTag[]) => readonly string[];
|
|
111
|
-
/**
|
|
112
|
-
* How long one regeneration may hold its path, in whole ms (≥ 1). Defaults to
|
|
113
|
-
* `DEFAULT_ISR_REGENERATE_DEADLINE_MS`. There is no "forever": that is the bug it bounds.
|
|
114
|
-
*/
|
|
115
|
-
readonly regenerateDeadlineMs?: number | undefined;
|
|
116
|
-
/** Injected so the deadline is provable without waiting one out. */
|
|
117
|
-
readonly schedule?: Scheduler | undefined;
|
|
118
|
-
}
|
|
119
41
|
|
|
120
42
|
/**
|
|
121
43
|
* The ceiling on one regeneration's hold on its path — `@ultimat3/http`'s default
|
|
@@ -127,21 +49,18 @@ export interface IsrControllerOptions {
|
|
|
127
49
|
*/
|
|
128
50
|
export const DEFAULT_ISR_REGENERATE_DEADLINE_MS = 30_000;
|
|
129
51
|
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
* memo, LRU, Redis and the CDN. Returns a detach function for tests and reloads.
|
|
143
|
-
*/
|
|
144
|
-
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;
|
|
145
64
|
}
|
|
146
65
|
|
|
147
66
|
/**
|
|
@@ -177,16 +96,17 @@ export function isrController(options: IsrControllerOptions = {}): IsrController
|
|
|
177
96
|
*/
|
|
178
97
|
const latest = new Map<string, object>();
|
|
179
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
|
+
);
|
|
180
108
|
|
|
181
|
-
|
|
182
|
-
const path = routePathOf(key);
|
|
183
|
-
const table = routes();
|
|
184
|
-
return table.find((r) => r.path === path) ?? matchersOf(table).find((m) => m.test(path))?.route;
|
|
185
|
-
}
|
|
186
|
-
|
|
187
|
-
/** The route's `revalidate.tags` for a store key — what its document is purged by at the edge. */
|
|
188
|
-
const tagsOf = (key: string): readonly CacheTag[] =>
|
|
189
|
-
(descriptorFor(key)?.revalidateTags ?? []).map(parseWireTag);
|
|
109
|
+
const descriptorFor = (key: string): RouteDescriptor | undefined => isrRouteFor(routes(), key);
|
|
190
110
|
|
|
191
111
|
/**
|
|
192
112
|
* A rendered page joins the invalidation graph under its route's tags, so `/blog/a` and
|
|
@@ -195,10 +115,15 @@ export function isrController(options: IsrControllerOptions = {}): IsrController
|
|
|
195
115
|
function registerPath(path: string, descriptor: RouteDescriptor | undefined): void {
|
|
196
116
|
if (descriptor === undefined || registered.has(path)) return;
|
|
197
117
|
if (descriptor.revalidateTags.length === 0) return;
|
|
198
|
-
registerDependent(descriptor.revalidateTags.map(
|
|
118
|
+
registerDependent(descriptor.revalidateTags.map(parseTag), { kind: 'isr-route', id: path });
|
|
199
119
|
registered.add(path);
|
|
200
120
|
}
|
|
201
121
|
|
|
122
|
+
function forgetPath(path: string): void {
|
|
123
|
+
if (!registered.delete(path)) return;
|
|
124
|
+
unregisterDependent({ kind: 'isr-route', id: path });
|
|
125
|
+
}
|
|
126
|
+
|
|
202
127
|
/**
|
|
203
128
|
* A registration is only true while the store still holds the page. The store evicts silently
|
|
204
129
|
* and offers no callback — and a custom `IsrStore` need not have one at all — so the store's own
|
|
@@ -208,12 +133,10 @@ export function isrController(options: IsrControllerOptions = {}): IsrController
|
|
|
208
133
|
*/
|
|
209
134
|
function forgetEvictedPaths(): void {
|
|
210
135
|
if (registered.size === 0) return;
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
registered.delete(path);
|
|
216
|
-
}
|
|
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);
|
|
217
140
|
}
|
|
218
141
|
|
|
219
142
|
function isFresh(entry: IsrEntry): boolean {
|
|
@@ -223,13 +146,25 @@ export function isrController(options: IsrControllerOptions = {}): IsrController
|
|
|
223
146
|
return now() - entry.generatedAt < ttlMs;
|
|
224
147
|
}
|
|
225
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
|
+
|
|
226
160
|
/** One regeneration of `path`; `led` is told when THIS call started it rather than joined one. */
|
|
227
|
-
function
|
|
228
|
-
return flight.run(path, async (): Promise<
|
|
161
|
+
function generate(path: string, render: IsrRenderFn, led?: () => void): Promise<Generation> {
|
|
162
|
+
return flight.run(path, async (): Promise<Generation> => {
|
|
229
163
|
led?.();
|
|
230
164
|
const run = {};
|
|
231
165
|
latest.set(path, run);
|
|
232
166
|
const descriptor = descriptorFor(path);
|
|
167
|
+
const policy = isrPolicyOf(descriptor);
|
|
233
168
|
try {
|
|
234
169
|
// BEFORE the render, not after: `revalidateByTags` reads the graph, so a bust arriving
|
|
235
170
|
// while a cold path's first render was in flight could not see the page it was
|
|
@@ -240,46 +175,144 @@ export function isrController(options: IsrControllerOptions = {}): IsrController
|
|
|
240
175
|
// and a `markStale` landing in between was then ERASED by `store.set({ stale: false })`.
|
|
241
176
|
// For a tag-only route `isFresh` is true forever, so the process went on serving pre-write
|
|
242
177
|
// HTML for the rest of its life. One mechanism, not a second one grown here.
|
|
243
|
-
const fence =
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
const
|
|
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));
|
|
248
183
|
const entry: IsrEntry = {
|
|
249
184
|
path,
|
|
250
|
-
html,
|
|
251
|
-
hash: contentHash(html),
|
|
185
|
+
html: rendered.html,
|
|
186
|
+
hash: contentHash(rendered.html),
|
|
252
187
|
generatedAt: now(),
|
|
253
|
-
ttlMs:
|
|
188
|
+
ttlMs: policy.ttlMs,
|
|
254
189
|
stale: false,
|
|
255
190
|
// Screened at generation, the one place a status enters the store: a `NaN` written here
|
|
256
191
|
// would be served for the whole TTL as a `RangeError` on every hit.
|
|
257
|
-
status: finiteStatus('IsrRenderFn', status),
|
|
192
|
+
status: finiteStatus('IsrRenderFn', rendered.status),
|
|
258
193
|
};
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
//
|
|
262
|
-
|
|
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
|
+
}
|
|
263
210
|
forgetEvictedPaths();
|
|
264
|
-
|
|
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 };
|
|
265
214
|
} finally {
|
|
266
215
|
if (latest.get(path) === run) latest.delete(path);
|
|
267
216
|
}
|
|
268
217
|
});
|
|
269
218
|
}
|
|
270
219
|
|
|
271
|
-
|
|
272
|
-
|
|
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;
|
|
273
252
|
}
|
|
274
253
|
|
|
275
254
|
function markStale(path: string): boolean {
|
|
276
255
|
// The mark is recorded whether or not the store holds the page: a regeneration already in
|
|
277
256
|
// flight for a path this store has never held is exactly the case the fence above exists for,
|
|
278
257
|
// and `invalidateTags`' own fanout only marks the TAGS.
|
|
279
|
-
|
|
258
|
+
fences.mark({ key: path });
|
|
280
259
|
return store.markStale(path);
|
|
281
260
|
}
|
|
282
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
|
+
|
|
283
316
|
return {
|
|
284
317
|
store: () => store,
|
|
285
318
|
inflight: () => flight.size,
|
|
@@ -287,56 +320,73 @@ export function isrController(options: IsrControllerOptions = {}): IsrController
|
|
|
287
320
|
markStale,
|
|
288
321
|
|
|
289
322
|
async serve(path, render) {
|
|
290
|
-
const
|
|
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
|
+
}
|
|
291
338
|
|
|
292
339
|
if (cached === undefined) {
|
|
293
|
-
const
|
|
294
|
-
const result =
|
|
295
|
-
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 };
|
|
296
343
|
}
|
|
297
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);
|
|
298
348
|
if (isFresh(cached)) {
|
|
299
|
-
return {
|
|
300
|
-
state: 'hit',
|
|
301
|
-
entry: cached,
|
|
302
|
-
result: toResult(cached, buildId, tagsOf(path)),
|
|
303
|
-
regenerating: false,
|
|
304
|
-
};
|
|
349
|
+
return { state: 'hit', entry: cached, result: answer(cached), regenerating: false };
|
|
305
350
|
}
|
|
306
351
|
|
|
307
|
-
// 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.
|
|
308
354
|
let started = false;
|
|
309
|
-
|
|
355
|
+
if (failed.get(path) !== undefined) {
|
|
356
|
+
return { state: 'stale', entry: cached, result: answer(cached, true), regenerating: false };
|
|
357
|
+
}
|
|
358
|
+
void generate(path, render, () => {
|
|
310
359
|
started = true;
|
|
311
|
-
})
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
};
|
|
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 };
|
|
323
371
|
},
|
|
324
372
|
|
|
325
373
|
revalidateByTags(tags) {
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
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);
|
|
331
380
|
return [...affected].sort();
|
|
332
381
|
},
|
|
333
382
|
|
|
334
383
|
attach() {
|
|
335
|
-
// The cache fanout owns the trigger; render owns only "what does
|
|
384
|
+
// The cache fanout owns the trigger; render owns only "what does an invalidation mean here".
|
|
336
385
|
const revalidate: Revalidator = (path) => {
|
|
337
|
-
|
|
386
|
+
invalidate(path);
|
|
338
387
|
};
|
|
339
|
-
|
|
388
|
+
const revalidateTags: TagRevalidator = (tags) => revalidateHeld(tags);
|
|
389
|
+
registerRevalidator(revalidate, revalidateTags);
|
|
340
390
|
installedRevalidator = revalidate;
|
|
341
391
|
return () => {
|
|
342
392
|
for (const path of registered) unregisterDependent({ kind: 'isr-route', id: path });
|
|
@@ -367,113 +417,3 @@ export async function invalidateAndRevalidate(
|
|
|
367
417
|
const report = await invalidateTags(tags);
|
|
368
418
|
return report.isr;
|
|
369
419
|
}
|
|
370
|
-
|
|
371
|
-
/** `post` / `post:123` → `{ entity, id? }`. Mirrors `@ultimat3/cache`'s wire form. */
|
|
372
|
-
function parseWireTag(wire: string): CacheTag {
|
|
373
|
-
const split = wire.indexOf(':');
|
|
374
|
-
if (split === -1) return { entity: wire };
|
|
375
|
-
return { entity: wire.slice(0, split), id: wire.slice(split + 1) };
|
|
376
|
-
}
|
|
377
|
-
|
|
378
|
-
interface RouteMatcher {
|
|
379
|
-
readonly route: RouteDescriptor;
|
|
380
|
-
test(storedPath: string): boolean;
|
|
381
|
-
}
|
|
382
|
-
|
|
383
|
-
/** One compiled set per route TABLE — `describePages()` hands out one array per registry change. */
|
|
384
|
-
const compiledTables = new WeakMap<readonly RouteDescriptor[], readonly RouteMatcher[]>();
|
|
385
|
-
|
|
386
|
-
/**
|
|
387
|
-
* A stored path belongs to a route when the route's pattern matches it — `route-pattern.ts`'s one
|
|
388
|
-
* pattern compiler, never a second. Ordered MOST SPECIFIC FIRST, as `@ultimat3/http`'s trie
|
|
389
|
-
* resolves a request: segment by segment, a static beats a `:param` beats a `*catch-all`. In table
|
|
390
|
-
* order `/docs/*path` sorts before `/docs/:id`, so the first match gave `/docs/7` the catch-all's
|
|
391
|
-
* TTL and tags — a page rendered by one route and expired by another's clock.
|
|
392
|
-
*/
|
|
393
|
-
function matchersOf(table: readonly RouteDescriptor[]): readonly RouteMatcher[] {
|
|
394
|
-
const cached = compiledTables.get(table);
|
|
395
|
-
if (cached !== undefined) return cached;
|
|
396
|
-
// Static routes too: `/precios-españa` is the route's path and `/precios-espa%C3%B1a` the key's,
|
|
397
|
-
// so only the pattern (raw or encoded, per character) finds it.
|
|
398
|
-
const compiled = table
|
|
399
|
-
.map((route): RouteMatcher & { readonly specificity: number } => {
|
|
400
|
-
const { regex, specificity } = compilePattern(route.path);
|
|
401
|
-
return { route, specificity, test: (stored) => regex.test(stored) };
|
|
402
|
-
})
|
|
403
|
-
.sort((a, b) => b.specificity - a.specificity);
|
|
404
|
-
compiledTables.set(table, compiled);
|
|
405
|
-
return compiled;
|
|
406
|
-
}
|
|
407
|
-
|
|
408
|
-
/** Tag-only routes have no clock of their own; a tag bust reaches the CDN through the fanout. */
|
|
409
|
-
const TAG_ONLY_S_MAX_AGE_SECONDS = 60;
|
|
410
|
-
|
|
411
|
-
/**
|
|
412
|
-
* `IsrStore` is a driver seam, so an entry can come back from an app's own store — one backed by
|
|
413
|
-
* Redis round-trips it through JSON, where a `ttlMs` that was never written reads back as
|
|
414
|
-
* `undefined` and `entry.ttlMs === null` is then false. Two failures follow from that one value
|
|
415
|
-
* and neither raises: `now - generatedAt < NaN` is false, so the page is NEVER fresh and every
|
|
416
|
-
* request regenerates it, and the CDN is handed `s-maxage=NaN` — an unparseable directive a
|
|
417
|
-
* conforming cache IGNORES, dropping the page to heuristic caching rather than to the declared
|
|
418
|
-
* age. Read on the request path, so it is TOTAL rather than a throw: a ttl that is not a positive
|
|
419
|
-
* finite number of milliseconds is the tag-only `null` `parseTtlMs` would have answered for it.
|
|
420
|
-
*/
|
|
421
|
-
function entryTtlMs(entry: IsrEntry): number | null {
|
|
422
|
-
const ttlMs = entry.ttlMs;
|
|
423
|
-
if (ttlMs === null || (Number.isFinite(ttlMs) && ttlMs > 0)) return ttlMs;
|
|
424
|
-
logger.warn('isr.entry_ttl_invalid', { path: entry.path, ttlMs: String(ttlMs) });
|
|
425
|
-
return null;
|
|
426
|
-
}
|
|
427
|
-
|
|
428
|
-
/**
|
|
429
|
-
* The declared TTL is the route's own contract with the CDN: a shared cache must not hold the
|
|
430
|
-
* page longer than the app said it stays true. A flat `s-maxage=60` made `revalidate: { ttl:
|
|
431
|
-
* '5m' }` a lie in one direction and `ttl: '30s'` a lie in the other.
|
|
432
|
-
*/
|
|
433
|
-
function cacheControl(ttlMs: number | null): string {
|
|
434
|
-
const sMaxAge = ttlMs === null ? TAG_ONLY_S_MAX_AGE_SECONDS : Math.round(ttlMs / 1_000);
|
|
435
|
-
|
|
436
|
-
return `public, max-age=0, s-maxage=${sMaxAge}, stale-while-revalidate=86400`;
|
|
437
|
-
}
|
|
438
|
-
|
|
439
|
-
/**
|
|
440
|
-
* `entryTtlMs`'s reason, one field over: a store may hand back an entry with no `status`, or one
|
|
441
|
-
* that JSON turned into something else, on the request path. Absent is 200 — the only value any
|
|
442
|
-
* entry carried before the field existed — and anything the range refuses is 200 with a warning,
|
|
443
|
-
* because a stored number must not 500 the page for its whole TTL.
|
|
444
|
-
*/
|
|
445
|
-
function entryStatus(entry: IsrEntry): number {
|
|
446
|
-
const status = entry.status;
|
|
447
|
-
if (status === undefined) return 200;
|
|
448
|
-
if (isRenderStatus(status)) return status;
|
|
449
|
-
logger.warn('isr.entry_status_invalid', { path: entry.path, status: String(status) });
|
|
450
|
-
return 200;
|
|
451
|
-
}
|
|
452
|
-
|
|
453
|
-
function toResult(
|
|
454
|
-
entry: IsrEntry,
|
|
455
|
-
buildId: string,
|
|
456
|
-
tags: readonly CacheTag[],
|
|
457
|
-
servedStale = false,
|
|
458
|
-
): RenderResult {
|
|
459
|
-
const headers: Record<string, string> = {
|
|
460
|
-
...staticHeaders(entry.hash, buildId),
|
|
461
|
-
'cache-control': cacheControl(entryTtlMs(entry)),
|
|
462
|
-
// The store keys on the locale; a shared cache in front of it has to as well, or the CDN
|
|
463
|
-
// repeats the bug this entry was split to fix. `ssrHeaders`' own line, for the same reason.
|
|
464
|
-
// The rest of the shared key — the cookie, the zone — is added by `@ultimat3/http`'s
|
|
465
|
-
// `cache-headers` stage, which sees the actor this function cannot.
|
|
466
|
-
vary: 'accept-language',
|
|
467
|
-
};
|
|
468
|
-
// The keys an edge purges this document by — `@ultimat3/cache`'s list, never a second one, so
|
|
469
|
-
// what a bust sends and what the page carries cannot drift. Absent, an `invalidates` cleared
|
|
470
|
-
// every tier but the CDN, which held the document for its whole `s-maxage`. The tags were
|
|
471
|
-
// screened when the route registered (`registry.ts`), so this cannot refuse on the request path.
|
|
472
|
-
const keys = surrogateKeys(tags, 'isr');
|
|
473
|
-
if (keys.length > 0) {
|
|
474
|
-
headers['surrogate-key'] = keys.join(' ');
|
|
475
|
-
headers['cache-tag'] = keys.join(',');
|
|
476
|
-
}
|
|
477
|
-
if (servedStale) headers['x-ultimate-isr'] = 'stale';
|
|
478
|
-
return { status: entryStatus(entry), headers, body: entry.html };
|
|
479
|
-
}
|