@teyik0/furin 0.2.0-alpha.3 → 0.2.0-alpha.4
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/LICENSE +21 -21
- package/README.md +37 -33
- package/dist/build/compile-entry.ts +109 -109
- package/dist/build/entry-template.ts +156 -156
- package/dist/build/server-routes-entry.ts +36 -36
- package/dist/cli/index.js +0 -0
- package/dist/client/link.js +163 -16
- package/dist/client/router/abort.d.ts +1 -0
- package/dist/client/router/sync-catch-up.d.ts +29 -0
- package/dist/furin.d.ts +1 -1
- package/dist/furin.js +499 -215
- package/dist/server/sync/adapter.d.ts +62 -0
- package/dist/server/sync/fingerprint.d.ts +6 -0
- package/dist/server/sync/index.d.ts +2 -2
- package/dist/server/sync/memory-adapter.d.ts +17 -0
- package/dist/server/sync/plugin.d.ts +12 -195
- package/dist/server/sync/response.d.ts +4 -0
- package/dist/server/sync/stream.d.ts +16 -4
- package/package.json +1 -1
- package/src/adapter/README.md +13 -13
- package/src/build/client.ts +152 -152
- package/src/build/compile-entry.ts +109 -109
- package/src/build/entry-template.ts +156 -156
- package/src/build/hydrate.ts +340 -340
- package/src/build/index.ts +152 -152
- package/src/build/route-types.ts +145 -145
- package/src/build/scan-server.ts +95 -95
- package/src/build/server-routes-entry.ts +36 -36
- package/src/build/shared.ts +80 -80
- package/src/build/ssg-cache.ts +32 -32
- package/src/build/types.ts +89 -89
- package/src/client/router/abort.ts +5 -0
- package/src/client/router/link-utils.ts +184 -184
- package/src/client/router/provider.tsx +883 -804
- package/src/client/router/sync-catch-up.ts +113 -0
- package/src/client/router/types.ts +276 -276
- package/src/client/sync.ts +98 -98
- package/src/client.ts +369 -369
- package/src/furin.ts +465 -465
- package/src/server/auto-invalidate/runtime.ts +134 -134
- package/src/server/dev-page-plugin.ts +454 -454
- package/src/server/render/assemble.ts +160 -160
- package/src/server/render/isr.ts +323 -323
- package/src/server/render/ssr.ts +591 -585
- package/src/server/router/plugin.ts +384 -384
- package/src/server/sync/adapter.ts +65 -0
- package/src/server/sync/config.ts +29 -29
- package/src/server/sync/fingerprint.ts +44 -0
- package/src/server/sync/index.ts +15 -14
- package/src/server/sync/memory-adapter.ts +196 -0
- package/src/server/sync/plugin.ts +200 -108
- package/src/server/sync/response.ts +77 -0
- package/src/server/sync/stream.ts +152 -144
- package/src/shared/deferred-ndjson.ts +7 -0
package/src/server/render/isr.ts
CHANGED
|
@@ -1,323 +1,323 @@
|
|
|
1
|
-
import type { Context } from "elysia";
|
|
2
|
-
import { renderToReadableStream } from "react-dom/server";
|
|
3
|
-
import { isNotFoundError } from "../../shared/not-found.ts";
|
|
4
|
-
import type { SearchRouteMetadata } from "../../shared/search-params.ts";
|
|
5
|
-
import { autoInvalidateRegistry } from "../auto-invalidate/registry.ts";
|
|
6
|
-
import { deleteISRCache, getISRCache, setISRCache } from "../cache/isr.ts";
|
|
7
|
-
import type { ISRCacheEntry } from "../cache/isr-ssg.ts";
|
|
8
|
-
import { createLogger, useLogger } from "../context-logger.ts";
|
|
9
|
-
import type { ResolvedRoute, RootLayout } from "../router/index.ts";
|
|
10
|
-
import {
|
|
11
|
-
assembleHTML,
|
|
12
|
-
injectSyncRuntimeScript,
|
|
13
|
-
type LoaderContext,
|
|
14
|
-
resolvePath,
|
|
15
|
-
streamToString,
|
|
16
|
-
} from "./assemble.ts";
|
|
17
|
-
import {
|
|
18
|
-
type PreparedRender,
|
|
19
|
-
prepareRender,
|
|
20
|
-
renderElementWithShellFallback,
|
|
21
|
-
renderForPath,
|
|
22
|
-
} from "./ssr.ts";
|
|
23
|
-
|
|
24
|
-
/**
|
|
25
|
-
* Builds the Cache-Control header value for an ISR response.
|
|
26
|
-
*/
|
|
27
|
-
function isrCacheControl(isFresh: boolean, revalidate: number): string {
|
|
28
|
-
const sMaxAge = isFresh ? revalidate : 0;
|
|
29
|
-
return `public, max-age=0, must-revalidate, s-maxage=${sMaxAge}, stale-while-revalidate=${revalidate}`;
|
|
30
|
-
}
|
|
31
|
-
|
|
32
|
-
/**
|
|
33
|
-
* ETag for an ISR entry: `"buildId:generatedAt"`. Null when no build ID is
|
|
34
|
-
* available (dev), which disables conditional requests for that response.
|
|
35
|
-
*/
|
|
36
|
-
function isrEtag(buildId: string | undefined, generatedAt: number): string | null {
|
|
37
|
-
return buildId ? `"${buildId}:${generatedAt}"` : null;
|
|
38
|
-
}
|
|
39
|
-
|
|
40
|
-
/**
|
|
41
|
-
* Serves a response from an existing ISR cache entry.
|
|
42
|
-
* Handles stale-while-revalidate background refresh and ETag conditional requests.
|
|
43
|
-
*/
|
|
44
|
-
function serveISRCacheHit(
|
|
45
|
-
cached: ISRCacheEntry,
|
|
46
|
-
ctx: Context,
|
|
47
|
-
route: ResolvedRoute,
|
|
48
|
-
params: Record<string, string>,
|
|
49
|
-
cacheKey: string,
|
|
50
|
-
revalidate: number,
|
|
51
|
-
root: RootLayout,
|
|
52
|
-
buildId: string | undefined,
|
|
53
|
-
searchRoutes?: SearchRouteMetadata[]
|
|
54
|
-
): string | undefined {
|
|
55
|
-
const isFresh = Date.now() - cached.generatedAt < revalidate * 1000;
|
|
56
|
-
|
|
57
|
-
if (!isFresh) {
|
|
58
|
-
revalidateInBackground(route, params, cacheKey, revalidate, root, ctx, searchRoutes);
|
|
59
|
-
}
|
|
60
|
-
|
|
61
|
-
const etag = isrEtag(buildId, cached.generatedAt);
|
|
62
|
-
if (etag && ctx.request.headers.get("if-none-match") === etag) {
|
|
63
|
-
ctx.set.status = 304;
|
|
64
|
-
ctx.set.headers.etag = etag;
|
|
65
|
-
ctx.set.headers["cache-control"] = isrCacheControl(isFresh, revalidate);
|
|
66
|
-
return;
|
|
67
|
-
}
|
|
68
|
-
|
|
69
|
-
ctx.set.headers["content-type"] = "text/html; charset=utf-8";
|
|
70
|
-
ctx.set.headers["cache-control"] = isrCacheControl(isFresh, revalidate);
|
|
71
|
-
if (etag) {
|
|
72
|
-
ctx.set.headers.etag = etag;
|
|
73
|
-
}
|
|
74
|
-
|
|
75
|
-
useLogger().set({
|
|
76
|
-
furin: { render: "isr", route: route.pattern, cache: isFresh ? "hit" : "stale" },
|
|
77
|
-
});
|
|
78
|
-
return injectSyncRuntimeScript(cached.html);
|
|
79
|
-
}
|
|
80
|
-
|
|
81
|
-
/**
|
|
82
|
-
* Handles the ISR non-200 render path: shell-recovery with fallback error
|
|
83
|
-
* component, structured logging, and cache-control headers.
|
|
84
|
-
*/
|
|
85
|
-
async function renderISRNon200(
|
|
86
|
-
prepared: PreparedRender,
|
|
87
|
-
route: ResolvedRoute,
|
|
88
|
-
ctx: Context,
|
|
89
|
-
root: RootLayout,
|
|
90
|
-
errorDigest: string | undefined,
|
|
91
|
-
renderStart: number,
|
|
92
|
-
buildId: string | undefined
|
|
93
|
-
): Promise<string> {
|
|
94
|
-
const { componentProps, element, headData, headers, template, status, notFoundError } = prepared;
|
|
95
|
-
const fallbackProps: Record<string, unknown> = { ...componentProps };
|
|
96
|
-
if (status === 404) {
|
|
97
|
-
fallbackProps.__furinStatus = 404;
|
|
98
|
-
if (notFoundError) {
|
|
99
|
-
fallbackProps.__furinNotFound = notFoundError;
|
|
100
|
-
}
|
|
101
|
-
}
|
|
102
|
-
|
|
103
|
-
const { stream: reactStream, shellError } = await renderElementWithShellFallback(
|
|
104
|
-
element,
|
|
105
|
-
route.error ?? root.error,
|
|
106
|
-
prepared.ssrContext
|
|
107
|
-
);
|
|
108
|
-
let finalStatus = status;
|
|
109
|
-
let finalDigest = errorDigest;
|
|
110
|
-
if (shellError) {
|
|
111
|
-
finalStatus = 500;
|
|
112
|
-
finalDigest = shellError.digest;
|
|
113
|
-
useLogger().set({
|
|
114
|
-
furin: {
|
|
115
|
-
render: "isr",
|
|
116
|
-
route: route.pattern,
|
|
117
|
-
cache: "miss",
|
|
118
|
-
digest: finalDigest,
|
|
119
|
-
phase: "shell",
|
|
120
|
-
},
|
|
121
|
-
});
|
|
122
|
-
fallbackProps.__furinError = { digest: finalDigest, status: finalStatus };
|
|
123
|
-
fallbackProps.__furinStatus = 500;
|
|
124
|
-
}
|
|
125
|
-
if (!fallbackProps.__furinError && errorDigest) {
|
|
126
|
-
fallbackProps.__furinError = { digest: errorDigest, status };
|
|
127
|
-
}
|
|
128
|
-
|
|
129
|
-
await reactStream.allReady;
|
|
130
|
-
const reactHtml = await streamToString(reactStream);
|
|
131
|
-
const html = assembleHTML(template, headData, reactHtml, fallbackProps);
|
|
132
|
-
const generatedAt = Date.now();
|
|
133
|
-
|
|
134
|
-
const renderMs = generatedAt - renderStart;
|
|
135
|
-
useLogger().set({
|
|
136
|
-
furin: {
|
|
137
|
-
render: "isr",
|
|
138
|
-
route: route.pattern,
|
|
139
|
-
cache: "miss",
|
|
140
|
-
render_ms: renderMs,
|
|
141
|
-
...(finalDigest ? { digest: finalDigest } : {}),
|
|
142
|
-
status: finalStatus,
|
|
143
|
-
},
|
|
144
|
-
});
|
|
145
|
-
|
|
146
|
-
const etag = isrEtag(buildId, generatedAt);
|
|
147
|
-
// Apply loader-set headers first so custom headers survive, then let the
|
|
148
|
-
// ISR-critical headers win (the cache contract is framework-owned).
|
|
149
|
-
for (const [key, value] of Object.entries(headers)) {
|
|
150
|
-
ctx.set.headers[key] = value;
|
|
151
|
-
}
|
|
152
|
-
ctx.set.headers["content-type"] = "text/html; charset=utf-8";
|
|
153
|
-
ctx.set.headers["cache-control"] = "no-store";
|
|
154
|
-
if (etag) {
|
|
155
|
-
ctx.set.headers.etag = etag;
|
|
156
|
-
}
|
|
157
|
-
ctx.set.status = finalStatus;
|
|
158
|
-
return html;
|
|
159
|
-
}
|
|
160
|
-
|
|
161
|
-
export async function handleISR(
|
|
162
|
-
route: ResolvedRoute,
|
|
163
|
-
ctx: Context,
|
|
164
|
-
root: RootLayout,
|
|
165
|
-
buildId: string | undefined,
|
|
166
|
-
searchRoutes?: SearchRouteMetadata[]
|
|
167
|
-
) {
|
|
168
|
-
const revalidate = route.page._route.revalidate ?? 60;
|
|
169
|
-
const params = ctx.params ?? {};
|
|
170
|
-
const cacheKey = resolvePath(route.pattern, params);
|
|
171
|
-
|
|
172
|
-
const cached = getISRCache(cacheKey);
|
|
173
|
-
if (cached) {
|
|
174
|
-
return serveISRCacheHit(
|
|
175
|
-
cached,
|
|
176
|
-
ctx,
|
|
177
|
-
route,
|
|
178
|
-
params,
|
|
179
|
-
cacheKey,
|
|
180
|
-
revalidate,
|
|
181
|
-
root,
|
|
182
|
-
buildId,
|
|
183
|
-
searchRoutes
|
|
184
|
-
);
|
|
185
|
-
}
|
|
186
|
-
|
|
187
|
-
const renderStart = Date.now();
|
|
188
|
-
const prepared = await prepareRender(route, ctx, root, undefined, false, undefined, searchRoutes);
|
|
189
|
-
|
|
190
|
-
if (prepared instanceof Response) {
|
|
191
|
-
return prepared;
|
|
192
|
-
}
|
|
193
|
-
|
|
194
|
-
const { element, headData, headers, syncData, template, status, errorDigest } = prepared;
|
|
195
|
-
|
|
196
|
-
if (status !== 200) {
|
|
197
|
-
return renderISRNon200(prepared, route, ctx, root, errorDigest, renderStart, buildId);
|
|
198
|
-
}
|
|
199
|
-
|
|
200
|
-
const stream = await renderToReadableStream(element);
|
|
201
|
-
await stream.allReady;
|
|
202
|
-
const reactHtml = await streamToString(stream);
|
|
203
|
-
const html = assembleHTML(template, headData, reactHtml, syncData);
|
|
204
|
-
const generatedAt = Date.now();
|
|
205
|
-
|
|
206
|
-
useLogger().set({
|
|
207
|
-
furin: {
|
|
208
|
-
render: "isr",
|
|
209
|
-
route: route.pattern,
|
|
210
|
-
cache: "miss",
|
|
211
|
-
render_ms: generatedAt - renderStart,
|
|
212
|
-
},
|
|
213
|
-
});
|
|
214
|
-
|
|
215
|
-
setISRCache(cacheKey, { html, generatedAt, revalidate });
|
|
216
|
-
autoInvalidateRegistry.registerLoaderTags(cacheKey, route.tags);
|
|
217
|
-
|
|
218
|
-
const etag = isrEtag(buildId, generatedAt);
|
|
219
|
-
// Apply loader-set headers first so custom headers survive, then let the
|
|
220
|
-
// ISR-critical headers win (the cache contract is framework-owned).
|
|
221
|
-
for (const [key, value] of Object.entries(headers)) {
|
|
222
|
-
ctx.set.headers[key] = value;
|
|
223
|
-
}
|
|
224
|
-
ctx.set.headers["content-type"] = "text/html; charset=utf-8";
|
|
225
|
-
ctx.set.headers["cache-control"] = isrCacheControl(true, revalidate);
|
|
226
|
-
if (etag) {
|
|
227
|
-
ctx.set.headers.etag = etag;
|
|
228
|
-
}
|
|
229
|
-
return html;
|
|
230
|
-
}
|
|
231
|
-
|
|
232
|
-
/** Tracks in-flight ISR revalidations to prevent thundering herd. */
|
|
233
|
-
const pendingRevalidations = new Set<string>();
|
|
234
|
-
|
|
235
|
-
function revalidateInBackground(
|
|
236
|
-
route: ResolvedRoute,
|
|
237
|
-
params: Record<string, string>,
|
|
238
|
-
cacheKey: string,
|
|
239
|
-
revalidate: number,
|
|
240
|
-
root: RootLayout,
|
|
241
|
-
originalCtx: LoaderContext,
|
|
242
|
-
searchRoutes?: SearchRouteMetadata[]
|
|
243
|
-
) {
|
|
244
|
-
if (pendingRevalidations.has(cacheKey)) {
|
|
245
|
-
const logger = createLogger({});
|
|
246
|
-
logger.set({
|
|
247
|
-
furin: {
|
|
248
|
-
render: "isr",
|
|
249
|
-
route: route.pattern,
|
|
250
|
-
cache: "revalidation_skipped",
|
|
251
|
-
reason: "already_in_flight",
|
|
252
|
-
},
|
|
253
|
-
});
|
|
254
|
-
logger.emit();
|
|
255
|
-
return;
|
|
256
|
-
}
|
|
257
|
-
pendingRevalidations.add(cacheKey);
|
|
258
|
-
|
|
259
|
-
renderForPath(
|
|
260
|
-
route,
|
|
261
|
-
params,
|
|
262
|
-
root,
|
|
263
|
-
new URL(originalCtx.request.url).origin,
|
|
264
|
-
"isr",
|
|
265
|
-
undefined,
|
|
266
|
-
searchRoutes
|
|
267
|
-
)
|
|
268
|
-
.then((result) => {
|
|
269
|
-
if (result instanceof Response) {
|
|
270
|
-
return;
|
|
271
|
-
}
|
|
272
|
-
// Only replace the cached entry with a healthy 200 render. The current
|
|
273
|
-
// ISR cache stores HTML only, so caching non-200 output would serve it
|
|
274
|
-
// back as a 200 on the next hit.
|
|
275
|
-
if (result.status !== 200) {
|
|
276
|
-
const logger = createLogger({});
|
|
277
|
-
logger.set({
|
|
278
|
-
furin: {
|
|
279
|
-
render: "isr",
|
|
280
|
-
route: route.pattern,
|
|
281
|
-
cache: "revalidation_skipped",
|
|
282
|
-
reason: "non_200_render",
|
|
283
|
-
status: result.status,
|
|
284
|
-
},
|
|
285
|
-
});
|
|
286
|
-
logger.emit();
|
|
287
|
-
return;
|
|
288
|
-
}
|
|
289
|
-
setISRCache(cacheKey, {
|
|
290
|
-
html: result.html,
|
|
291
|
-
generatedAt: Date.now(),
|
|
292
|
-
revalidate,
|
|
293
|
-
});
|
|
294
|
-
})
|
|
295
|
-
.catch((err: unknown) => {
|
|
296
|
-
const logger = createLogger({});
|
|
297
|
-
if (isNotFoundError(err)) {
|
|
298
|
-
deleteISRCache(cacheKey);
|
|
299
|
-
logger.set({
|
|
300
|
-
furin: {
|
|
301
|
-
render: "isr",
|
|
302
|
-
route: route.pattern,
|
|
303
|
-
cache: "revalidation_invalidated",
|
|
304
|
-
reason: "not_found",
|
|
305
|
-
},
|
|
306
|
-
});
|
|
307
|
-
logger.emit();
|
|
308
|
-
return;
|
|
309
|
-
}
|
|
310
|
-
logger.set({
|
|
311
|
-
furin: {
|
|
312
|
-
render: "isr",
|
|
313
|
-
route: route.pattern,
|
|
314
|
-
cache: "revalidation_failed",
|
|
315
|
-
},
|
|
316
|
-
});
|
|
317
|
-
logger.error(err instanceof Error ? err : new Error(String(err)));
|
|
318
|
-
logger.emit();
|
|
319
|
-
})
|
|
320
|
-
.finally(() => {
|
|
321
|
-
pendingRevalidations.delete(cacheKey);
|
|
322
|
-
});
|
|
323
|
-
}
|
|
1
|
+
import type { Context } from "elysia";
|
|
2
|
+
import { renderToReadableStream } from "react-dom/server";
|
|
3
|
+
import { isNotFoundError } from "../../shared/not-found.ts";
|
|
4
|
+
import type { SearchRouteMetadata } from "../../shared/search-params.ts";
|
|
5
|
+
import { autoInvalidateRegistry } from "../auto-invalidate/registry.ts";
|
|
6
|
+
import { deleteISRCache, getISRCache, setISRCache } from "../cache/isr.ts";
|
|
7
|
+
import type { ISRCacheEntry } from "../cache/isr-ssg.ts";
|
|
8
|
+
import { createLogger, useLogger } from "../context-logger.ts";
|
|
9
|
+
import type { ResolvedRoute, RootLayout } from "../router/index.ts";
|
|
10
|
+
import {
|
|
11
|
+
assembleHTML,
|
|
12
|
+
injectSyncRuntimeScript,
|
|
13
|
+
type LoaderContext,
|
|
14
|
+
resolvePath,
|
|
15
|
+
streamToString,
|
|
16
|
+
} from "./assemble.ts";
|
|
17
|
+
import {
|
|
18
|
+
type PreparedRender,
|
|
19
|
+
prepareRender,
|
|
20
|
+
renderElementWithShellFallback,
|
|
21
|
+
renderForPath,
|
|
22
|
+
} from "./ssr.ts";
|
|
23
|
+
|
|
24
|
+
/**
|
|
25
|
+
* Builds the Cache-Control header value for an ISR response.
|
|
26
|
+
*/
|
|
27
|
+
function isrCacheControl(isFresh: boolean, revalidate: number): string {
|
|
28
|
+
const sMaxAge = isFresh ? revalidate : 0;
|
|
29
|
+
return `public, max-age=0, must-revalidate, s-maxage=${sMaxAge}, stale-while-revalidate=${revalidate}`;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
/**
|
|
33
|
+
* ETag for an ISR entry: `"buildId:generatedAt"`. Null when no build ID is
|
|
34
|
+
* available (dev), which disables conditional requests for that response.
|
|
35
|
+
*/
|
|
36
|
+
function isrEtag(buildId: string | undefined, generatedAt: number): string | null {
|
|
37
|
+
return buildId ? `"${buildId}:${generatedAt}"` : null;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
/**
|
|
41
|
+
* Serves a response from an existing ISR cache entry.
|
|
42
|
+
* Handles stale-while-revalidate background refresh and ETag conditional requests.
|
|
43
|
+
*/
|
|
44
|
+
function serveISRCacheHit(
|
|
45
|
+
cached: ISRCacheEntry,
|
|
46
|
+
ctx: Context,
|
|
47
|
+
route: ResolvedRoute,
|
|
48
|
+
params: Record<string, string>,
|
|
49
|
+
cacheKey: string,
|
|
50
|
+
revalidate: number,
|
|
51
|
+
root: RootLayout,
|
|
52
|
+
buildId: string | undefined,
|
|
53
|
+
searchRoutes?: SearchRouteMetadata[]
|
|
54
|
+
): string | undefined {
|
|
55
|
+
const isFresh = Date.now() - cached.generatedAt < revalidate * 1000;
|
|
56
|
+
|
|
57
|
+
if (!isFresh) {
|
|
58
|
+
revalidateInBackground(route, params, cacheKey, revalidate, root, ctx, searchRoutes);
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
const etag = isrEtag(buildId, cached.generatedAt);
|
|
62
|
+
if (etag && ctx.request.headers.get("if-none-match") === etag) {
|
|
63
|
+
ctx.set.status = 304;
|
|
64
|
+
ctx.set.headers.etag = etag;
|
|
65
|
+
ctx.set.headers["cache-control"] = isrCacheControl(isFresh, revalidate);
|
|
66
|
+
return;
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
ctx.set.headers["content-type"] = "text/html; charset=utf-8";
|
|
70
|
+
ctx.set.headers["cache-control"] = isrCacheControl(isFresh, revalidate);
|
|
71
|
+
if (etag) {
|
|
72
|
+
ctx.set.headers.etag = etag;
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
useLogger().set({
|
|
76
|
+
furin: { render: "isr", route: route.pattern, cache: isFresh ? "hit" : "stale" },
|
|
77
|
+
});
|
|
78
|
+
return injectSyncRuntimeScript(cached.html);
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
/**
|
|
82
|
+
* Handles the ISR non-200 render path: shell-recovery with fallback error
|
|
83
|
+
* component, structured logging, and cache-control headers.
|
|
84
|
+
*/
|
|
85
|
+
async function renderISRNon200(
|
|
86
|
+
prepared: PreparedRender,
|
|
87
|
+
route: ResolvedRoute,
|
|
88
|
+
ctx: Context,
|
|
89
|
+
root: RootLayout,
|
|
90
|
+
errorDigest: string | undefined,
|
|
91
|
+
renderStart: number,
|
|
92
|
+
buildId: string | undefined
|
|
93
|
+
): Promise<string> {
|
|
94
|
+
const { componentProps, element, headData, headers, template, status, notFoundError } = prepared;
|
|
95
|
+
const fallbackProps: Record<string, unknown> = { ...componentProps };
|
|
96
|
+
if (status === 404) {
|
|
97
|
+
fallbackProps.__furinStatus = 404;
|
|
98
|
+
if (notFoundError) {
|
|
99
|
+
fallbackProps.__furinNotFound = notFoundError;
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
const { stream: reactStream, shellError } = await renderElementWithShellFallback(
|
|
104
|
+
element,
|
|
105
|
+
route.error ?? root.error,
|
|
106
|
+
prepared.ssrContext
|
|
107
|
+
);
|
|
108
|
+
let finalStatus = status;
|
|
109
|
+
let finalDigest = errorDigest;
|
|
110
|
+
if (shellError) {
|
|
111
|
+
finalStatus = 500;
|
|
112
|
+
finalDigest = shellError.digest;
|
|
113
|
+
useLogger().set({
|
|
114
|
+
furin: {
|
|
115
|
+
render: "isr",
|
|
116
|
+
route: route.pattern,
|
|
117
|
+
cache: "miss",
|
|
118
|
+
digest: finalDigest,
|
|
119
|
+
phase: "shell",
|
|
120
|
+
},
|
|
121
|
+
});
|
|
122
|
+
fallbackProps.__furinError = { digest: finalDigest, status: finalStatus };
|
|
123
|
+
fallbackProps.__furinStatus = 500;
|
|
124
|
+
}
|
|
125
|
+
if (!fallbackProps.__furinError && errorDigest) {
|
|
126
|
+
fallbackProps.__furinError = { digest: errorDigest, status };
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
await reactStream.allReady;
|
|
130
|
+
const reactHtml = await streamToString(reactStream);
|
|
131
|
+
const html = assembleHTML(template, headData, reactHtml, fallbackProps);
|
|
132
|
+
const generatedAt = Date.now();
|
|
133
|
+
|
|
134
|
+
const renderMs = generatedAt - renderStart;
|
|
135
|
+
useLogger().set({
|
|
136
|
+
furin: {
|
|
137
|
+
render: "isr",
|
|
138
|
+
route: route.pattern,
|
|
139
|
+
cache: "miss",
|
|
140
|
+
render_ms: renderMs,
|
|
141
|
+
...(finalDigest ? { digest: finalDigest } : {}),
|
|
142
|
+
status: finalStatus,
|
|
143
|
+
},
|
|
144
|
+
});
|
|
145
|
+
|
|
146
|
+
const etag = isrEtag(buildId, generatedAt);
|
|
147
|
+
// Apply loader-set headers first so custom headers survive, then let the
|
|
148
|
+
// ISR-critical headers win (the cache contract is framework-owned).
|
|
149
|
+
for (const [key, value] of Object.entries(headers)) {
|
|
150
|
+
ctx.set.headers[key] = value;
|
|
151
|
+
}
|
|
152
|
+
ctx.set.headers["content-type"] = "text/html; charset=utf-8";
|
|
153
|
+
ctx.set.headers["cache-control"] = "no-store";
|
|
154
|
+
if (etag) {
|
|
155
|
+
ctx.set.headers.etag = etag;
|
|
156
|
+
}
|
|
157
|
+
ctx.set.status = finalStatus;
|
|
158
|
+
return html;
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
export async function handleISR(
|
|
162
|
+
route: ResolvedRoute,
|
|
163
|
+
ctx: Context,
|
|
164
|
+
root: RootLayout,
|
|
165
|
+
buildId: string | undefined,
|
|
166
|
+
searchRoutes?: SearchRouteMetadata[]
|
|
167
|
+
) {
|
|
168
|
+
const revalidate = route.page._route.revalidate ?? 60;
|
|
169
|
+
const params = ctx.params ?? {};
|
|
170
|
+
const cacheKey = resolvePath(route.pattern, params);
|
|
171
|
+
|
|
172
|
+
const cached = getISRCache(cacheKey);
|
|
173
|
+
if (cached) {
|
|
174
|
+
return serveISRCacheHit(
|
|
175
|
+
cached,
|
|
176
|
+
ctx,
|
|
177
|
+
route,
|
|
178
|
+
params,
|
|
179
|
+
cacheKey,
|
|
180
|
+
revalidate,
|
|
181
|
+
root,
|
|
182
|
+
buildId,
|
|
183
|
+
searchRoutes
|
|
184
|
+
);
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
const renderStart = Date.now();
|
|
188
|
+
const prepared = await prepareRender(route, ctx, root, undefined, false, undefined, searchRoutes);
|
|
189
|
+
|
|
190
|
+
if (prepared instanceof Response) {
|
|
191
|
+
return prepared;
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
const { element, headData, headers, syncData, template, status, errorDigest } = prepared;
|
|
195
|
+
|
|
196
|
+
if (status !== 200) {
|
|
197
|
+
return renderISRNon200(prepared, route, ctx, root, errorDigest, renderStart, buildId);
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
const stream = await renderToReadableStream(element);
|
|
201
|
+
await stream.allReady;
|
|
202
|
+
const reactHtml = await streamToString(stream);
|
|
203
|
+
const html = assembleHTML(template, headData, reactHtml, syncData);
|
|
204
|
+
const generatedAt = Date.now();
|
|
205
|
+
|
|
206
|
+
useLogger().set({
|
|
207
|
+
furin: {
|
|
208
|
+
render: "isr",
|
|
209
|
+
route: route.pattern,
|
|
210
|
+
cache: "miss",
|
|
211
|
+
render_ms: generatedAt - renderStart,
|
|
212
|
+
},
|
|
213
|
+
});
|
|
214
|
+
|
|
215
|
+
setISRCache(cacheKey, { html, generatedAt, revalidate });
|
|
216
|
+
autoInvalidateRegistry.registerLoaderTags(cacheKey, route.tags);
|
|
217
|
+
|
|
218
|
+
const etag = isrEtag(buildId, generatedAt);
|
|
219
|
+
// Apply loader-set headers first so custom headers survive, then let the
|
|
220
|
+
// ISR-critical headers win (the cache contract is framework-owned).
|
|
221
|
+
for (const [key, value] of Object.entries(headers)) {
|
|
222
|
+
ctx.set.headers[key] = value;
|
|
223
|
+
}
|
|
224
|
+
ctx.set.headers["content-type"] = "text/html; charset=utf-8";
|
|
225
|
+
ctx.set.headers["cache-control"] = isrCacheControl(true, revalidate);
|
|
226
|
+
if (etag) {
|
|
227
|
+
ctx.set.headers.etag = etag;
|
|
228
|
+
}
|
|
229
|
+
return html;
|
|
230
|
+
}
|
|
231
|
+
|
|
232
|
+
/** Tracks in-flight ISR revalidations to prevent thundering herd. */
|
|
233
|
+
const pendingRevalidations = new Set<string>();
|
|
234
|
+
|
|
235
|
+
function revalidateInBackground(
|
|
236
|
+
route: ResolvedRoute,
|
|
237
|
+
params: Record<string, string>,
|
|
238
|
+
cacheKey: string,
|
|
239
|
+
revalidate: number,
|
|
240
|
+
root: RootLayout,
|
|
241
|
+
originalCtx: LoaderContext,
|
|
242
|
+
searchRoutes?: SearchRouteMetadata[]
|
|
243
|
+
) {
|
|
244
|
+
if (pendingRevalidations.has(cacheKey)) {
|
|
245
|
+
const logger = createLogger({});
|
|
246
|
+
logger.set({
|
|
247
|
+
furin: {
|
|
248
|
+
render: "isr",
|
|
249
|
+
route: route.pattern,
|
|
250
|
+
cache: "revalidation_skipped",
|
|
251
|
+
reason: "already_in_flight",
|
|
252
|
+
},
|
|
253
|
+
});
|
|
254
|
+
logger.emit();
|
|
255
|
+
return;
|
|
256
|
+
}
|
|
257
|
+
pendingRevalidations.add(cacheKey);
|
|
258
|
+
|
|
259
|
+
renderForPath(
|
|
260
|
+
route,
|
|
261
|
+
params,
|
|
262
|
+
root,
|
|
263
|
+
new URL(originalCtx.request.url).origin,
|
|
264
|
+
"isr",
|
|
265
|
+
undefined,
|
|
266
|
+
searchRoutes
|
|
267
|
+
)
|
|
268
|
+
.then((result) => {
|
|
269
|
+
if (result instanceof Response) {
|
|
270
|
+
return;
|
|
271
|
+
}
|
|
272
|
+
// Only replace the cached entry with a healthy 200 render. The current
|
|
273
|
+
// ISR cache stores HTML only, so caching non-200 output would serve it
|
|
274
|
+
// back as a 200 on the next hit.
|
|
275
|
+
if (result.status !== 200) {
|
|
276
|
+
const logger = createLogger({});
|
|
277
|
+
logger.set({
|
|
278
|
+
furin: {
|
|
279
|
+
render: "isr",
|
|
280
|
+
route: route.pattern,
|
|
281
|
+
cache: "revalidation_skipped",
|
|
282
|
+
reason: "non_200_render",
|
|
283
|
+
status: result.status,
|
|
284
|
+
},
|
|
285
|
+
});
|
|
286
|
+
logger.emit();
|
|
287
|
+
return;
|
|
288
|
+
}
|
|
289
|
+
setISRCache(cacheKey, {
|
|
290
|
+
html: result.html,
|
|
291
|
+
generatedAt: Date.now(),
|
|
292
|
+
revalidate,
|
|
293
|
+
});
|
|
294
|
+
})
|
|
295
|
+
.catch((err: unknown) => {
|
|
296
|
+
const logger = createLogger({});
|
|
297
|
+
if (isNotFoundError(err)) {
|
|
298
|
+
deleteISRCache(cacheKey);
|
|
299
|
+
logger.set({
|
|
300
|
+
furin: {
|
|
301
|
+
render: "isr",
|
|
302
|
+
route: route.pattern,
|
|
303
|
+
cache: "revalidation_invalidated",
|
|
304
|
+
reason: "not_found",
|
|
305
|
+
},
|
|
306
|
+
});
|
|
307
|
+
logger.emit();
|
|
308
|
+
return;
|
|
309
|
+
}
|
|
310
|
+
logger.set({
|
|
311
|
+
furin: {
|
|
312
|
+
render: "isr",
|
|
313
|
+
route: route.pattern,
|
|
314
|
+
cache: "revalidation_failed",
|
|
315
|
+
},
|
|
316
|
+
});
|
|
317
|
+
logger.error(err instanceof Error ? err : new Error(String(err)));
|
|
318
|
+
logger.emit();
|
|
319
|
+
})
|
|
320
|
+
.finally(() => {
|
|
321
|
+
pendingRevalidations.delete(cacheKey);
|
|
322
|
+
});
|
|
323
|
+
}
|