@teyik0/furin 0.2.0-alpha.2 → 0.2.0-alpha.3
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 +33 -33
- package/dist/adapter/bun.d.ts +3 -0
- package/dist/adapter/static.d.ts +3 -0
- package/dist/build/client.d.ts +25 -0
- package/dist/build/compile-entry.d.ts +9 -0
- package/dist/build/compile-entry.ts +109 -0
- package/dist/build/entry-template.d.ts +38 -0
- package/dist/build/entry-template.ts +156 -0
- package/dist/build/hydrate.d.ts +24 -0
- package/dist/build/index.d.ts +8 -0
- package/dist/build/index.js +8106 -0
- package/dist/build/route-types.d.ts +23 -0
- package/dist/build/scan-server.d.ts +8 -0
- package/dist/build/server-routes-entry.d.ts +13 -0
- package/dist/build/server-routes-entry.ts +36 -0
- package/dist/build/shared.d.ts +12 -0
- package/dist/build/ssg-cache.d.ts +4 -0
- package/dist/build/types.d.ts +80 -0
- package/dist/cli/config.d.ts +9 -0
- package/dist/cli/index.d.ts +2 -0
- package/dist/cli/index.js +8097 -0
- package/dist/client/boundaries.d.ts +117 -0
- package/dist/client/default-screens.d.ts +17 -0
- package/dist/client/link.d.ts +22 -0
- package/dist/client/link.js +4244 -0
- package/dist/client/router/boundary-tree.d.ts +26 -0
- package/dist/client/router/context.d.ts +8 -0
- package/dist/client/router/history.d.ts +9 -0
- package/dist/client/router/index.d.ts +11 -0
- package/dist/client/router/link-utils.d.ts +53 -0
- package/dist/client/router/navigation.d.ts +10 -0
- package/dist/client/router/provider.d.ts +4 -0
- package/dist/client/router/search/index.d.ts +18 -0
- package/dist/client/router/search-store.d.ts +21 -0
- package/dist/client/router/spa-response.d.ts +58 -0
- package/dist/client/router/types.d.ts +292 -0
- package/dist/client/server-error.d.ts +12 -0
- package/dist/client/sync.d.ts +25 -0
- package/dist/client.d.ts +243 -0
- package/dist/client.js +140 -0
- package/dist/config.d.ts +57 -0
- package/dist/config.js +29 -0
- package/dist/env.d.ts +50 -0
- package/dist/furin.d.ts +77 -0
- package/dist/furin.js +8693 -0
- package/dist/plugin/index.d.ts +20 -0
- package/dist/plugin/index.js +1500 -0
- package/dist/plugin/transform-client.d.ts +10 -0
- package/dist/server/auto-invalidate/index.d.ts +4 -0
- package/dist/server/auto-invalidate/plugin.d.ts +38 -0
- package/dist/server/auto-invalidate/registry.d.ts +9 -0
- package/dist/server/auto-invalidate/runtime.d.ts +9 -0
- package/dist/server/auto-invalidate/types.d.ts +12 -0
- package/dist/server/cache/dev-loader.d.ts +28 -0
- package/dist/server/cache/index.d.ts +10 -0
- package/dist/server/cache/invalidation.d.ts +12 -0
- package/dist/server/cache/isr-ssg.d.ts +26 -0
- package/dist/server/cache/isr.d.ts +6 -0
- package/dist/server/cache/registry.d.ts +5 -0
- package/dist/server/cache/route-cache.d.ts +26 -0
- package/dist/server/cache/ssg.d.ts +5 -0
- package/dist/server/context-logger.d.ts +28 -0
- package/dist/server/dev-inspector.d.ts +89 -0
- package/dist/server/dev-page-plugin.d.ts +112 -0
- package/dist/server/internal.d.ts +35 -0
- package/dist/server/internal.js +17 -0
- package/dist/server/lang-detect.d.ts +5 -0
- package/dist/server/render/assemble.d.ts +49 -0
- package/dist/server/render/element.d.ts +24 -0
- package/dist/server/render/index.d.ts +7 -0
- package/dist/server/render/index.ts +22 -0
- package/dist/server/render/isr.d.ts +4 -0
- package/dist/server/render/loaders.d.ts +64 -0
- package/dist/server/render/not-found.d.ts +6 -0
- package/dist/server/render/shell.d.ts +39 -0
- package/dist/server/render/shell.ts +202 -0
- package/dist/server/render/ssg.d.ts +9 -0
- package/dist/server/render/ssr.d.ts +108 -0
- package/dist/server/render/template.d.ts +6 -0
- package/dist/server/router/discovery.d.ts +16 -0
- package/dist/server/router/hmr.d.ts +65 -0
- package/dist/server/router/index.d.ts +6 -0
- package/dist/server/router/index.js +27 -0
- package/dist/server/router/index.ts +28 -0
- package/dist/server/router/patterns.d.ts +40 -0
- package/dist/server/router/plugin.d.ts +21 -0
- package/dist/server/router/schemas.d.ts +59 -0
- package/dist/server/router/types.d.ts +60 -0
- package/dist/server/runtime-env.d.ts +3 -0
- package/dist/server/runtime-env.js +10 -0
- package/dist/server/sync/config.d.ts +6 -0
- package/dist/server/sync/index.d.ts +4 -0
- package/dist/server/sync/plugin.d.ts +369 -0
- package/dist/server/sync/stream.d.ts +47 -0
- package/dist/shared/await.d.ts +67 -0
- package/dist/shared/deferred-ndjson.d.ts +32 -0
- package/dist/shared/digest.d.ts +16 -0
- package/dist/shared/error.d.ts +37 -0
- package/dist/shared/not-found.d.ts +19 -0
- package/dist/shared/search-params.d.ts +17 -0
- package/dist/shared/utils/ast-walk.d.ts +18 -0
- package/dist/shared/utils/index.d.ts +13 -0
- package/package.json +5 -5
- 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 -337
- 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 -79
- package/src/build/ssg-cache.ts +32 -32
- package/src/build/types.ts +89 -89
- package/src/client/router/link-utils.ts +184 -177
- package/src/client/router/provider.tsx +804 -759
- package/src/client/router/types.ts +276 -271
- package/src/client/sync.ts +98 -0
- package/src/client.ts +369 -355
- package/src/furin.ts +465 -444
- package/src/server/auto-invalidate/runtime.ts +134 -133
- package/src/server/dev-page-plugin.ts +454 -449
- package/src/server/render/assemble.ts +160 -133
- package/src/server/render/isr.ts +323 -317
- package/src/server/render/ssr.ts +585 -583
- package/src/server/router/plugin.ts +384 -384
- package/src/server/sync/config.ts +29 -0
- package/src/server/sync/index.ts +14 -0
- package/src/server/sync/plugin.ts +108 -0
- package/src/server/sync/stream.ts +144 -0
package/src/server/render/isr.ts
CHANGED
|
@@ -1,317 +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
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
} from "./
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
const
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
ctx
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
}
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
}
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
}
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
ctx
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
const
|
|
189
|
-
|
|
190
|
-
if (
|
|
191
|
-
return
|
|
192
|
-
}
|
|
193
|
-
|
|
194
|
-
const
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
if (result
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
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
|
+
}
|