dinou 9.0.2 → 9.1.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/CHANGELOG.md +65 -0
- package/README.md +379 -9742
- package/dinou/bun/build.js +38 -1
- package/{cli.js → dinou/cli.js} +8 -7
- package/dinou/cloudflare/build.js +37 -0
- package/dinou/core/bailout-proxy.js +2 -2
- package/dinou/core/build-static-pages.js +17 -7
- package/dinou/core/cache-revalidate.js +977 -152
- package/dinou/core/cache-slot.js +848 -103
- package/dinou/core/client-webpack.jsx +60 -9
- package/dinou/core/client.jsx +60 -9
- package/dinou/core/edge-ssr.js +33 -5
- package/dinou/core/generate-static.js +109 -47
- package/dinou/core/get-jsx.js +35 -27
- package/dinou/core/handler.js +786 -54
- package/dinou/core/layout-functions.js +102 -1
- package/dinou/core/no-dynamic.js +25 -0
- package/dinou/core/ppr-context.js +293 -10
- package/dinou/core/ppr-runtime.js +373 -37
- package/dinou/core/render-jsx-to-client-jsx.js +205 -74
- package/dinou/core/slot.js +246 -33
- package/dinou/core/storage-adapter.js +117 -11
- package/dinou/deno/build.js +38 -1
- package/dinou/deno/seed-kv.js +43 -11
- package/dinou/esbuild/helpers-esbuild/swc-disk-cache.js +242 -225
- package/dinou/esbuild/plugins-esbuild/server-functions-plugin.js +253 -226
- package/dinou/esbuild/react-refresh/esm-hmr/client.js +209 -209
- package/dinou/index.d.ts +10 -0
- package/dinou/index.js +3 -0
- package/dinou/node/build.js +2 -2
- package/dinou/node/bundle-dual-engine.js +2 -0
- package/dinou/node/dev.js +2853 -2704
- package/dinou/package.json +4 -1
- package/dinou/rollup/react-refresh/esm-hmr/client.js +220 -220
- package/dinou/rollup/rollup-plugins/rollup-plugin-server-functions.js +182 -160
- package/dinou/server.d.ts +65 -12
- package/dinou/server.js +11 -0
- package/dinou/webpack/loaders/server-functions-loader.js +101 -94
- package/dinou/webpack/webpack.config.js +9 -4
- package/eject.js +219 -223
- package/package.json +2 -2
package/dinou/core/cache-slot.js
CHANGED
|
@@ -10,6 +10,19 @@ import { asyncRenderJSXToClientJSX } from "./render-jsx-to-client-jsx.js";
|
|
|
10
10
|
import { DinouCacheSlotBoundary as RawDinouCacheSlotBoundary } from "./slot.js";
|
|
11
11
|
import { getContext } from "./request-context.js";
|
|
12
12
|
import { getJSX } from "./get-jsx.js";
|
|
13
|
+
import {
|
|
14
|
+
isPprBuildActive,
|
|
15
|
+
isInsideSuspense,
|
|
16
|
+
isInsideOuterSuspenseOutsideSlot,
|
|
17
|
+
deferPprSuspenseEffect,
|
|
18
|
+
runWithPprContext,
|
|
19
|
+
isInsideLayoutSegment,
|
|
20
|
+
isInsidePageSegment,
|
|
21
|
+
markDynamicAccess,
|
|
22
|
+
runInCachedSlot,
|
|
23
|
+
runInSlotDiscovery,
|
|
24
|
+
isSlotDiscoveryActive,
|
|
25
|
+
} from "./ppr-context.js";
|
|
13
26
|
import { createRequire } from "node:module";
|
|
14
27
|
|
|
15
28
|
const require = createRequire(import.meta.url);
|
|
@@ -21,6 +34,23 @@ if (!globalThis[INFLIGHT_KEY]) {
|
|
|
21
34
|
}
|
|
22
35
|
const inflightSlotRenders = globalThis[INFLIGHT_KEY];
|
|
23
36
|
|
|
37
|
+
const SLOT_GEN_KEY = Symbol.for("dinou.slot.render.generation");
|
|
38
|
+
if (!globalThis[SLOT_GEN_KEY]) {
|
|
39
|
+
globalThis[SLOT_GEN_KEY] = new Map();
|
|
40
|
+
}
|
|
41
|
+
const slotRenderGenerations = globalThis[SLOT_GEN_KEY];
|
|
42
|
+
|
|
43
|
+
const RECENT_FRESH_KEY = Symbol.for("dinou.slot.recent.fresh");
|
|
44
|
+
if (!globalThis[RECENT_FRESH_KEY]) {
|
|
45
|
+
globalThis[RECENT_FRESH_KEY] = new Map();
|
|
46
|
+
}
|
|
47
|
+
const recentFreshSlotRenders = globalThis[RECENT_FRESH_KEY];
|
|
48
|
+
|
|
49
|
+
const REVAL_QUEUE_STORAGE_KEY = Symbol.for("dinou.reval.queue.storage");
|
|
50
|
+
function isRevalidationActive() {
|
|
51
|
+
return Boolean(globalThis[REVAL_QUEUE_STORAGE_KEY]?.getStore());
|
|
52
|
+
}
|
|
53
|
+
|
|
24
54
|
const BG_REVAL_KEY = Symbol.for("dinou.slot.background.revalidations");
|
|
25
55
|
if (!globalThis[BG_REVAL_KEY]) {
|
|
26
56
|
globalThis[BG_REVAL_KEY] = new Set();
|
|
@@ -33,6 +63,46 @@ if (!globalThis[SLOT_REGISTRY_KEY]) {
|
|
|
33
63
|
}
|
|
34
64
|
const slotRegistry = globalThis[SLOT_REGISTRY_KEY];
|
|
35
65
|
|
|
66
|
+
const DISCOVERING_SLOTS_KEY = Symbol.for("dinou.slot.discovering");
|
|
67
|
+
if (!globalThis[DISCOVERING_SLOTS_KEY]) {
|
|
68
|
+
globalThis[DISCOVERING_SLOTS_KEY] = new Set();
|
|
69
|
+
}
|
|
70
|
+
const discoveringSlots = globalThis[DISCOVERING_SLOTS_KEY];
|
|
71
|
+
|
|
72
|
+
const PRE_EVAL_ROUTES_KEY = Symbol.for("dinou.slot.preeval.routes");
|
|
73
|
+
if (!globalThis[PRE_EVAL_ROUTES_KEY] || !(globalThis[PRE_EVAL_ROUTES_KEY] instanceof Map)) {
|
|
74
|
+
globalThis[PRE_EVAL_ROUTES_KEY] = new Map();
|
|
75
|
+
}
|
|
76
|
+
const preEvaluatingRoutes = globalThis[PRE_EVAL_ROUTES_KEY];
|
|
77
|
+
|
|
78
|
+
let _registerServerRefFn = null;
|
|
79
|
+
function getRegisterServerReference() {
|
|
80
|
+
if (_registerServerRefFn) return _registerServerRefFn;
|
|
81
|
+
if (
|
|
82
|
+
typeof globalThis !== "undefined" &&
|
|
83
|
+
globalThis.__DINOU_EDGE_RENDERER__ &&
|
|
84
|
+
typeof globalThis.__DINOU_EDGE_RENDERER__.registerServerReference === "function"
|
|
85
|
+
) {
|
|
86
|
+
_registerServerRefFn = globalThis.__DINOU_EDGE_RENDERER__.registerServerReference;
|
|
87
|
+
return _registerServerRefFn;
|
|
88
|
+
}
|
|
89
|
+
try {
|
|
90
|
+
const pkg = require("react-server-dom-webpack/server");
|
|
91
|
+
if (typeof pkg.registerServerReference === "function") {
|
|
92
|
+
_registerServerRefFn = pkg.registerServerReference;
|
|
93
|
+
return _registerServerRefFn;
|
|
94
|
+
}
|
|
95
|
+
} catch (e) {}
|
|
96
|
+
try {
|
|
97
|
+
const pkg = require("@roggc/react-server-dom-esm/server.node.js");
|
|
98
|
+
if (typeof pkg.registerServerReference === "function") {
|
|
99
|
+
_registerServerRefFn = pkg.registerServerReference;
|
|
100
|
+
return _registerServerRefFn;
|
|
101
|
+
}
|
|
102
|
+
} catch (e) {}
|
|
103
|
+
return null;
|
|
104
|
+
}
|
|
105
|
+
|
|
36
106
|
let _DinouCacheSlotBoundary = null;
|
|
37
107
|
function getCacheSlotBoundary() {
|
|
38
108
|
if (!_DinouCacheSlotBoundary) {
|
|
@@ -44,10 +114,19 @@ function getCacheSlotBoundary() {
|
|
|
44
114
|
const slotPath = path.resolve(currentDir, "slot.js");
|
|
45
115
|
const fileUrl = pathToFileURL(slotPath).href;
|
|
46
116
|
let regFn = null;
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
117
|
+
if (
|
|
118
|
+
typeof globalThis !== "undefined" &&
|
|
119
|
+
globalThis.__DINOU_EDGE_RENDERER__ &&
|
|
120
|
+
typeof globalThis.__DINOU_EDGE_RENDERER__.registerClientReference === "function"
|
|
121
|
+
) {
|
|
122
|
+
regFn = globalThis.__DINOU_EDGE_RENDERER__.registerClientReference;
|
|
123
|
+
}
|
|
124
|
+
if (!regFn) {
|
|
125
|
+
try {
|
|
126
|
+
const pkg = require("react-server-dom-webpack/server");
|
|
127
|
+
regFn = pkg.registerClientReference;
|
|
128
|
+
} catch (e) {}
|
|
129
|
+
}
|
|
51
130
|
if (!regFn) {
|
|
52
131
|
try {
|
|
53
132
|
const pkg = require("@roggc/react-server-dom-esm/server.node.js");
|
|
@@ -74,7 +153,8 @@ function getCacheSlotBoundary() {
|
|
|
74
153
|
|
|
75
154
|
/**
|
|
76
155
|
* Serializes a resolved JSX element tree into a JSON-safe string,
|
|
77
|
-
* preserving symbols, React 19 transitional elements,
|
|
156
|
+
* preserving symbols, React 19 transitional elements, client component references,
|
|
157
|
+
* and server function references ("use server").
|
|
78
158
|
*/
|
|
79
159
|
function serializeJSX(jsx) {
|
|
80
160
|
return JSON.stringify(jsx, (key, value) => {
|
|
@@ -93,6 +173,17 @@ function serializeJSX(jsx) {
|
|
|
93
173
|
async: value.$$async,
|
|
94
174
|
};
|
|
95
175
|
}
|
|
176
|
+
if (
|
|
177
|
+
(typeof value === "function" || typeof value === "object") &&
|
|
178
|
+
value !== null &&
|
|
179
|
+
value.$$typeof === Symbol.for("react.server.reference")
|
|
180
|
+
) {
|
|
181
|
+
return {
|
|
182
|
+
__dinou_server_ref__: true,
|
|
183
|
+
id: value.$$id || value.name,
|
|
184
|
+
bound: value.$$bound || null,
|
|
185
|
+
};
|
|
186
|
+
}
|
|
96
187
|
if (
|
|
97
188
|
value &&
|
|
98
189
|
typeof value === "object" &&
|
|
@@ -128,6 +219,26 @@ function deserializeJSX(json) {
|
|
|
128
219
|
});
|
|
129
220
|
return fakeRef;
|
|
130
221
|
}
|
|
222
|
+
if (value.__dinou_server_ref__) {
|
|
223
|
+
let serverRef = function () {};
|
|
224
|
+
const regServerRef = getRegisterServerReference();
|
|
225
|
+
if (regServerRef) {
|
|
226
|
+
try {
|
|
227
|
+
serverRef = regServerRef(serverRef, value.id, null);
|
|
228
|
+
if (Array.isArray(value.bound) && typeof serverRef.bind === "function") {
|
|
229
|
+
serverRef = serverRef.bind(null, ...value.bound);
|
|
230
|
+
}
|
|
231
|
+
} catch (e) {}
|
|
232
|
+
}
|
|
233
|
+
if (serverRef.$$typeof !== Symbol.for("react.server.reference")) {
|
|
234
|
+
Object.defineProperties(serverRef, {
|
|
235
|
+
$$typeof: { value: Symbol.for("react.server.reference") },
|
|
236
|
+
$$id: { value: value.id },
|
|
237
|
+
$$bound: { value: value.bound || null },
|
|
238
|
+
});
|
|
239
|
+
}
|
|
240
|
+
return serverRef;
|
|
241
|
+
}
|
|
131
242
|
}
|
|
132
243
|
return value;
|
|
133
244
|
});
|
|
@@ -149,11 +260,22 @@ function deserializeJSX(json) {
|
|
|
149
260
|
props.key = node.key;
|
|
150
261
|
}
|
|
151
262
|
const children = props.children;
|
|
263
|
+
let elem;
|
|
152
264
|
if (Array.isArray(children)) {
|
|
265
|
+
for (const ch of children) {
|
|
266
|
+
if (ch && typeof ch === "object" && ch._store) {
|
|
267
|
+
ch._store.validated = 1;
|
|
268
|
+
}
|
|
269
|
+
}
|
|
153
270
|
delete props.children;
|
|
154
|
-
|
|
271
|
+
elem = React.createElement(type, props, ...children);
|
|
272
|
+
} else {
|
|
273
|
+
elem = React.createElement(type, props);
|
|
155
274
|
}
|
|
156
|
-
|
|
275
|
+
if (elem && typeof elem === "object" && elem._store) {
|
|
276
|
+
elem._store.validated = 1;
|
|
277
|
+
}
|
|
278
|
+
return elem;
|
|
157
279
|
}
|
|
158
280
|
return node;
|
|
159
281
|
}
|
|
@@ -161,6 +283,226 @@ function deserializeJSX(json) {
|
|
|
161
283
|
return toReactElement(parsed);
|
|
162
284
|
}
|
|
163
285
|
|
|
286
|
+
function recordSlotTagsOnContext(
|
|
287
|
+
tags,
|
|
288
|
+
inLayout = isInsideLayoutSegment(),
|
|
289
|
+
inPage = isInsidePageSegment()
|
|
290
|
+
) {
|
|
291
|
+
if (!Array.isArray(tags) || tags.length === 0) return;
|
|
292
|
+
try {
|
|
293
|
+
const reqContext = getContext();
|
|
294
|
+
if (reqContext) {
|
|
295
|
+
if (!reqContext._slotTags) {
|
|
296
|
+
reqContext._slotTags = new Set();
|
|
297
|
+
}
|
|
298
|
+
if (!reqContext._layoutSlotTags) {
|
|
299
|
+
reqContext._layoutSlotTags = new Set();
|
|
300
|
+
}
|
|
301
|
+
if (!reqContext._pageSlotTags) {
|
|
302
|
+
reqContext._pageSlotTags = new Set();
|
|
303
|
+
}
|
|
304
|
+
for (const t of tags) {
|
|
305
|
+
if (!t) continue;
|
|
306
|
+
reqContext._slotTags.add(t);
|
|
307
|
+
if (inLayout) {
|
|
308
|
+
reqContext._layoutSlotTags.add(t);
|
|
309
|
+
} else if (inPage) {
|
|
310
|
+
reqContext._pageSlotTags.add(t);
|
|
311
|
+
} else if (!reqContext._layoutSlotTags.has(t)) {
|
|
312
|
+
reqContext._pageSlotTags.add(t);
|
|
313
|
+
}
|
|
314
|
+
}
|
|
315
|
+
}
|
|
316
|
+
} catch (e) {}
|
|
317
|
+
}
|
|
318
|
+
|
|
319
|
+
function commitSlotTags(tags) {
|
|
320
|
+
if (!Array.isArray(tags) || tags.length === 0) return;
|
|
321
|
+
const inLayout = isInsideLayoutSegment();
|
|
322
|
+
const inPage = isInsidePageSegment();
|
|
323
|
+
if (!deferPprSuspenseEffect(() => recordSlotTagsOnContext(tags, inLayout, inPage))) {
|
|
324
|
+
recordSlotTagsOnContext(tags, inLayout, inPage);
|
|
325
|
+
}
|
|
326
|
+
}
|
|
327
|
+
|
|
328
|
+
/**
|
|
329
|
+
* Reads a slot entry from storage, falling back to pre-rendered Edge static assets (env.ASSETS)
|
|
330
|
+
* when the runtime KV/Memory storage has not yet hydrated the build-time slot artifact.
|
|
331
|
+
*/
|
|
332
|
+
async function getSlotFromStorage(storage, storageKey, knownTags = []) {
|
|
333
|
+
if (!storage) return null;
|
|
334
|
+
let cached = null;
|
|
335
|
+
try {
|
|
336
|
+
cached = await storage.get(storageKey);
|
|
337
|
+
} catch (err) {}
|
|
338
|
+
if (cached && cached.content) {
|
|
339
|
+
return cached;
|
|
340
|
+
}
|
|
341
|
+
|
|
342
|
+
const isDeleted =
|
|
343
|
+
typeof storage.isDeleted === "function" && storage.isDeleted(storageKey);
|
|
344
|
+
if (isDeleted) {
|
|
345
|
+
return null;
|
|
346
|
+
}
|
|
347
|
+
|
|
348
|
+
try {
|
|
349
|
+
const reqContext = getContext();
|
|
350
|
+
const assets =
|
|
351
|
+
reqContext?.env?.ASSETS ||
|
|
352
|
+
reqContext?.platformContext?.env?.ASSETS ||
|
|
353
|
+
reqContext?.req?.env?.ASSETS ||
|
|
354
|
+
(typeof globalThis !== "undefined" ? globalThis.__DINOU_ENV__?.ASSETS : null);
|
|
355
|
+
if (assets && typeof assets.fetch === "function") {
|
|
356
|
+
const cleanKey = String(storageKey || "").replace(/^\/+/, "");
|
|
357
|
+
const metaKey = cleanKey.endsWith("/slot.json")
|
|
358
|
+
? cleanKey.slice(0, -"/slot.json".length) + "/slot.metadata.json"
|
|
359
|
+
: `${cleanKey}.metadata.json`;
|
|
360
|
+
const [slotRes, metaRes] = await Promise.all([
|
|
361
|
+
assets.fetch(new Request(`http://localhost/${cleanKey}`)),
|
|
362
|
+
assets.fetch(new Request(`http://localhost/${metaKey}`)),
|
|
363
|
+
]);
|
|
364
|
+
if (slotRes && slotRes.status === 200) {
|
|
365
|
+
const ct = slotRes.headers?.get?.("content-type") || "";
|
|
366
|
+
const content = await slotRes.text();
|
|
367
|
+
if (!ct.includes("text/html") && !content.trimStart().startsWith("<")) {
|
|
368
|
+
let metadata = null;
|
|
369
|
+
if (metaRes && metaRes.status === 200) {
|
|
370
|
+
try {
|
|
371
|
+
metadata = await metaRes.json();
|
|
372
|
+
} catch (e) {}
|
|
373
|
+
}
|
|
374
|
+
const generatedAt = metadata?.generatedAt || 0;
|
|
375
|
+
const combinedTags = new Set([
|
|
376
|
+
...(Array.isArray(metadata?.tags) ? metadata.tags : []),
|
|
377
|
+
...(Array.isArray(knownTags) ? knownTags : []),
|
|
378
|
+
]);
|
|
379
|
+
if (typeof storage.getTagInvalidatedAt === "function") {
|
|
380
|
+
for (const t of combinedTags) {
|
|
381
|
+
const invAt = storage.getTagInvalidatedAt(t);
|
|
382
|
+
if (invAt > 0 && invAt >= generatedAt) {
|
|
383
|
+
return null;
|
|
384
|
+
}
|
|
385
|
+
}
|
|
386
|
+
}
|
|
387
|
+
cached = { content, metadata };
|
|
388
|
+
await storage.set(cleanKey, content, metadata);
|
|
389
|
+
return cached;
|
|
390
|
+
}
|
|
391
|
+
}
|
|
392
|
+
}
|
|
393
|
+
} catch (e) {}
|
|
394
|
+
|
|
395
|
+
return cached;
|
|
396
|
+
}
|
|
397
|
+
|
|
398
|
+
async function readMetaFromStorageOrAssets(storage, key) {
|
|
399
|
+
if (!storage || !key) return null;
|
|
400
|
+
try {
|
|
401
|
+
const item = await storage.get(key);
|
|
402
|
+
if (item?.metadata && typeof item.metadata === "object") return item.metadata;
|
|
403
|
+
if (item?.content) return JSON.parse(item.content);
|
|
404
|
+
} catch (e) {}
|
|
405
|
+
if (typeof storage.isDeleted === "function" && storage.isDeleted(key)) return null;
|
|
406
|
+
try {
|
|
407
|
+
const reqContext = getContext();
|
|
408
|
+
const assets =
|
|
409
|
+
reqContext?.env?.ASSETS ||
|
|
410
|
+
reqContext?.platformContext?.env?.ASSETS ||
|
|
411
|
+
reqContext?.req?.env?.ASSETS ||
|
|
412
|
+
(typeof globalThis !== "undefined" ? globalThis.__DINOU_ENV__?.ASSETS : null);
|
|
413
|
+
if (assets && typeof assets.fetch === "function") {
|
|
414
|
+
const cleanKey = String(key).replace(/^\/+/, "");
|
|
415
|
+
const res = await assets.fetch(new Request(`http://localhost/${cleanKey}`));
|
|
416
|
+
if (res && res.status === 200) {
|
|
417
|
+
const text = await res.text();
|
|
418
|
+
if (text && text.trimStart().startsWith("{")) {
|
|
419
|
+
const parsed = JSON.parse(text);
|
|
420
|
+
await storage.set(cleanKey, text);
|
|
421
|
+
return parsed;
|
|
422
|
+
}
|
|
423
|
+
}
|
|
424
|
+
}
|
|
425
|
+
} catch (e) {}
|
|
426
|
+
return null;
|
|
427
|
+
}
|
|
428
|
+
|
|
429
|
+
/**
|
|
430
|
+
* Traverses a deserialized JSX tree from a cached outer slot and refreshes
|
|
431
|
+
* any nested <DinouCacheSlot> boundaries so inner slot invalidations are
|
|
432
|
+
* respected even when the outer slot hits its cache.
|
|
433
|
+
*/
|
|
434
|
+
async function refreshNestedSlotsInTree(node, currentSlotId, options = {}) {
|
|
435
|
+
if (node == null || typeof node !== "object") return node;
|
|
436
|
+
if (isSlotDiscoveryActive()) return node;
|
|
437
|
+
|
|
438
|
+
if (Array.isArray(node)) {
|
|
439
|
+
return Promise.all(
|
|
440
|
+
node.map((item) => refreshNestedSlotsInTree(item, currentSlotId, options))
|
|
441
|
+
);
|
|
442
|
+
}
|
|
443
|
+
|
|
444
|
+
if (node.$$typeof === Symbol.for("react.transitional.element")) {
|
|
445
|
+
const nestedSlotId = node.props && node.props.__dinou_slot__;
|
|
446
|
+
if (nestedSlotId && nestedSlotId !== currentSlotId) {
|
|
447
|
+
const nestedTags = Array.isArray(node.props.tags) ? node.props.tags : [];
|
|
448
|
+
commitSlotTags(Array.from(new Set([nestedSlotId, ...nestedTags].filter(Boolean))));
|
|
449
|
+
if (!slotRegistry.has(nestedSlotId) && slotRegistry.has(currentSlotId)) {
|
|
450
|
+
const parentEntry = slotRegistry.get(currentSlotId);
|
|
451
|
+
if (parentEntry && typeof parentEntry.discover === "function") {
|
|
452
|
+
await parentEntry.discover();
|
|
453
|
+
}
|
|
454
|
+
}
|
|
455
|
+
let currentPath = options.currentPath;
|
|
456
|
+
if (!currentPath) {
|
|
457
|
+
try {
|
|
458
|
+
currentPath = getContext()?.req?.path;
|
|
459
|
+
} catch (e) {}
|
|
460
|
+
}
|
|
461
|
+
const freshInner = await getSlotJSX(nestedSlotId, {
|
|
462
|
+
...options,
|
|
463
|
+
currentPath,
|
|
464
|
+
skipHostRevalidate: true,
|
|
465
|
+
});
|
|
466
|
+
if (freshInner !== null) {
|
|
467
|
+
const refreshedInner = await refreshNestedSlotsInTree(
|
|
468
|
+
freshInner,
|
|
469
|
+
nestedSlotId,
|
|
470
|
+
options
|
|
471
|
+
);
|
|
472
|
+
const newProps = { ...node.props };
|
|
473
|
+
if (node.key != null) newProps.key = node.key;
|
|
474
|
+
delete newProps.children;
|
|
475
|
+
return React.createElement(node.type, newProps, refreshedInner);
|
|
476
|
+
}
|
|
477
|
+
}
|
|
478
|
+
|
|
479
|
+
if (node.props && typeof node.props === "object") {
|
|
480
|
+
let changed = false;
|
|
481
|
+
const newProps = { ...node.props };
|
|
482
|
+
for (const [k, v] of Object.entries(node.props)) {
|
|
483
|
+
if (v && typeof v === "object") {
|
|
484
|
+
const updated = await refreshNestedSlotsInTree(v, currentSlotId, options);
|
|
485
|
+
if (updated !== v) {
|
|
486
|
+
newProps[k] = updated;
|
|
487
|
+
changed = true;
|
|
488
|
+
}
|
|
489
|
+
}
|
|
490
|
+
}
|
|
491
|
+
if (changed) {
|
|
492
|
+
if (node.key != null) newProps.key = node.key;
|
|
493
|
+
const children = newProps.children;
|
|
494
|
+
if (Array.isArray(children)) {
|
|
495
|
+
delete newProps.children;
|
|
496
|
+
return React.createElement(node.type, newProps, ...children);
|
|
497
|
+
}
|
|
498
|
+
return React.createElement(node.type, newProps);
|
|
499
|
+
}
|
|
500
|
+
}
|
|
501
|
+
}
|
|
502
|
+
|
|
503
|
+
return node;
|
|
504
|
+
}
|
|
505
|
+
|
|
164
506
|
/**
|
|
165
507
|
* DinouCacheSlot component.
|
|
166
508
|
*
|
|
@@ -173,10 +515,24 @@ function deserializeJSX(json) {
|
|
|
173
515
|
*/
|
|
174
516
|
async function DinouCacheSlot(props) {
|
|
175
517
|
const { id, tag, tags, revalidate, children } = props || {};
|
|
518
|
+
const isNoCache = typeof revalidate === "number" && revalidate < 1;
|
|
176
519
|
|
|
177
520
|
const slotId = id || tag || (Array.isArray(tags) ? tags[0] : null);
|
|
178
521
|
if (!slotId) {
|
|
179
|
-
|
|
522
|
+
if (isNoCache && !isSlotDiscoveryActive()) {
|
|
523
|
+
if (isPprBuildActive() && isInsideOuterSuspenseOutsideSlot()) {
|
|
524
|
+
markDynamicAccess("slot-revalidate-0");
|
|
525
|
+
} else {
|
|
526
|
+
try {
|
|
527
|
+
getContext()?._markDynamic?.();
|
|
528
|
+
} catch (e) {}
|
|
529
|
+
}
|
|
530
|
+
}
|
|
531
|
+
const runScope = isNoCache ? (fn) => fn() : runInCachedSlot;
|
|
532
|
+
return await runScope(async () => {
|
|
533
|
+
const childElement = typeof children === "function" ? await children() : children;
|
|
534
|
+
return await asyncRenderJSXToClientJSX(childElement);
|
|
535
|
+
});
|
|
180
536
|
}
|
|
181
537
|
|
|
182
538
|
const allTags = Array.isArray(tags)
|
|
@@ -184,36 +540,103 @@ async function DinouCacheSlot(props) {
|
|
|
184
540
|
: tag
|
|
185
541
|
? [tag]
|
|
186
542
|
: [];
|
|
543
|
+
const slotTagsToCommit = Array.from(new Set([slotId, ...allTags].filter(Boolean)));
|
|
187
544
|
|
|
188
545
|
const storageKey = `slots/${slotId}/slot.json`;
|
|
189
546
|
const storage = getStorageAdapter();
|
|
547
|
+
const runSlotScope = isNoCache ? (fn) => fn() : runInCachedSlot;
|
|
190
548
|
|
|
191
|
-
function wrapWithBoundary(content) {
|
|
549
|
+
function wrapWithBoundary(content, alreadyRendered = false) {
|
|
192
550
|
const Boundary = getCacheSlotBoundary();
|
|
193
551
|
if (!Boundary) return content;
|
|
194
|
-
|
|
552
|
+
const elem = React.createElement(
|
|
553
|
+
Boundary,
|
|
554
|
+
{ id: slotId, tags: allTags, __dinou_slot__: slotId },
|
|
555
|
+
content
|
|
556
|
+
);
|
|
557
|
+
if (alreadyRendered) {
|
|
558
|
+
return Object.defineProperties(
|
|
559
|
+
Object.create(Object.getPrototypeOf(elem)),
|
|
560
|
+
{
|
|
561
|
+
...Object.getOwnPropertyDescriptors(elem),
|
|
562
|
+
__clientJsxAlreadyRendered: {
|
|
563
|
+
value: true,
|
|
564
|
+
enumerable: false,
|
|
565
|
+
configurable: true,
|
|
566
|
+
},
|
|
567
|
+
}
|
|
568
|
+
);
|
|
569
|
+
}
|
|
570
|
+
return elem;
|
|
195
571
|
}
|
|
196
572
|
|
|
197
|
-
// Register in slotRegistry for on-demand endpoint re-rendering
|
|
573
|
+
// Register in slotRegistry for on-demand endpoint re-rendering and nested slot discovery
|
|
198
574
|
slotRegistry.set(slotId, {
|
|
199
575
|
render: async () => {
|
|
200
|
-
|
|
201
|
-
|
|
576
|
+
return await runSlotScope(async () => {
|
|
577
|
+
const childElement = typeof children === "function" ? await children() : children;
|
|
578
|
+
return await asyncRenderJSXToClientJSX(childElement);
|
|
579
|
+
});
|
|
580
|
+
},
|
|
581
|
+
discover: async () => {
|
|
582
|
+
if (discoveringSlots.has(slotId)) return;
|
|
583
|
+
discoveringSlots.add(slotId);
|
|
584
|
+
try {
|
|
585
|
+
await runInSlotDiscovery(async () => {
|
|
586
|
+
const childElement = typeof children === "function" ? await children() : children;
|
|
587
|
+
await asyncRenderJSXToClientJSX(childElement);
|
|
588
|
+
});
|
|
589
|
+
} catch (e) {
|
|
590
|
+
} finally {
|
|
591
|
+
discoveringSlots.delete(slotId);
|
|
592
|
+
}
|
|
202
593
|
},
|
|
203
594
|
tags: allTags,
|
|
204
595
|
revalidate: typeof revalidate === "number" ? revalidate : undefined,
|
|
205
596
|
});
|
|
206
597
|
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
598
|
+
// When running inside a nested slot discovery pass, recursively discover any child slots
|
|
599
|
+
if (isSlotDiscoveryActive() && !discoveringSlots.has(slotId)) {
|
|
600
|
+
const ownEntry = slotRegistry.get(slotId);
|
|
601
|
+
if (ownEntry && typeof ownEntry.discover === "function") {
|
|
602
|
+
await ownEntry.discover();
|
|
603
|
+
}
|
|
604
|
+
}
|
|
605
|
+
|
|
606
|
+
if (isNoCache && !isSlotDiscoveryActive()) {
|
|
607
|
+
if (isPprBuildActive() && isInsideOuterSuspenseOutsideSlot()) {
|
|
608
|
+
markDynamicAccess("slot-revalidate-0");
|
|
609
|
+
} else {
|
|
610
|
+
try {
|
|
611
|
+
getContext()?._markDynamic?.();
|
|
612
|
+
} catch (e) {}
|
|
613
|
+
}
|
|
614
|
+
}
|
|
615
|
+
|
|
616
|
+
if (!storage || isNoCache) {
|
|
617
|
+
const directContent = await runSlotScope(async () => {
|
|
618
|
+
const childElement = typeof children === "function" ? await children() : children;
|
|
619
|
+
return await asyncRenderJSXToClientJSX(childElement);
|
|
620
|
+
});
|
|
621
|
+
commitSlotTags(slotTagsToCommit);
|
|
622
|
+
return wrapWithBoundary(directContent, true);
|
|
210
623
|
}
|
|
211
624
|
|
|
212
625
|
// 1. Check if cached in storage
|
|
213
|
-
let cached =
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
626
|
+
let cached = await getSlotFromStorage(storage, storageKey, allTags);
|
|
627
|
+
|
|
628
|
+
// During a PPR static shell build, if a slot is inside a <Suspense> boundary
|
|
629
|
+
// and was only cached at runtime (not part of the static shell), do not short-circuit
|
|
630
|
+
// from disk so any dynamic request access (cookies/headers/query) inside the slot still
|
|
631
|
+
// triggers PPR postpone.
|
|
632
|
+
if (
|
|
633
|
+
isPprBuildActive() &&
|
|
634
|
+
isInsideOuterSuspenseOutsideSlot() &&
|
|
635
|
+
cached &&
|
|
636
|
+
!cached.metadata?.inStaticSuspense
|
|
637
|
+
) {
|
|
638
|
+
cached = null;
|
|
639
|
+
}
|
|
217
640
|
|
|
218
641
|
const now = Date.now();
|
|
219
642
|
if (cached && cached.content) {
|
|
@@ -226,77 +649,117 @@ async function DinouCacheSlot(props) {
|
|
|
226
649
|
? meta.revalidate
|
|
227
650
|
: undefined;
|
|
228
651
|
|
|
652
|
+
if (typeof revalidateSeconds === "number" && revalidateSeconds < 1) {
|
|
653
|
+
if (!isSlotDiscoveryActive()) {
|
|
654
|
+
if (isPprBuildActive() && isInsideOuterSuspenseOutsideSlot()) {
|
|
655
|
+
markDynamicAccess("slot-revalidate-0");
|
|
656
|
+
} else {
|
|
657
|
+
try {
|
|
658
|
+
getContext()?._markDynamic?.();
|
|
659
|
+
} catch (e) {}
|
|
660
|
+
}
|
|
661
|
+
}
|
|
662
|
+
const childElement = typeof children === "function" ? await children() : children;
|
|
663
|
+
const directContent = await asyncRenderJSXToClientJSX(childElement);
|
|
664
|
+
commitSlotTags(slotTagsToCommit);
|
|
665
|
+
return wrapWithBoundary(directContent, true);
|
|
666
|
+
}
|
|
667
|
+
|
|
229
668
|
const isExpired =
|
|
230
669
|
typeof revalidateSeconds === "number" &&
|
|
231
670
|
revalidateSeconds > 0 &&
|
|
232
671
|
now > generatedAt + revalidateSeconds * 1000;
|
|
233
672
|
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
// Stale-While-Revalidate
|
|
241
|
-
try {
|
|
242
|
-
const staleJSX = deserializeJSX(cached.content);
|
|
243
|
-
|
|
244
|
-
if (!backgroundRevalidations.has(storageKey)) {
|
|
245
|
-
backgroundRevalidations.add(storageKey);
|
|
246
|
-
const bgPromise = (async () => {
|
|
247
|
-
try {
|
|
248
|
-
const childElement = typeof children === "function" ? await children() : children;
|
|
249
|
-
const resolved = await asyncRenderJSXToClientJSX(childElement);
|
|
250
|
-
const serialized = serializeJSX(resolved);
|
|
251
|
-
await storage.set(storageKey, serialized, {
|
|
252
|
-
tags: allTags,
|
|
253
|
-
revalidate: revalidateSeconds,
|
|
254
|
-
generatedAt: Date.now(),
|
|
255
|
-
slotId,
|
|
256
|
-
});
|
|
257
|
-
} catch (e) {
|
|
258
|
-
console.error(`[DinouCacheSlot] Background revalidation failed for slot "${slotId}":`, e);
|
|
259
|
-
} finally {
|
|
260
|
-
backgroundRevalidations.delete(storageKey);
|
|
261
|
-
}
|
|
262
|
-
})();
|
|
673
|
+
const serveStaleWhileRevalidate =
|
|
674
|
+
isExpired &&
|
|
675
|
+
Boolean(meta.inStaticShell) &&
|
|
676
|
+
!isRevalidationActive() &&
|
|
677
|
+
!isPprBuildActive() &&
|
|
678
|
+
!getContext()?.platformContext?.isSSG;
|
|
263
679
|
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
680
|
+
if (
|
|
681
|
+
serveStaleWhileRevalidate &&
|
|
682
|
+
!getContext()?._isPprRuntimeResume &&
|
|
683
|
+
!backgroundRevalidations.has(storageKey) &&
|
|
684
|
+
!inflightSlotRenders.has(storageKey)
|
|
685
|
+
) {
|
|
686
|
+
backgroundRevalidations.add(storageKey);
|
|
687
|
+
const reqContext = getContext();
|
|
688
|
+
const currentPath = reqContext?.req?.path;
|
|
689
|
+
const bgPromise = (async () => {
|
|
690
|
+
try {
|
|
691
|
+
await getSlotJSX(slotId, { fresh: true, currentPath });
|
|
692
|
+
} catch (e) {
|
|
693
|
+
console.error(
|
|
694
|
+
`[DinouCacheSlot] Background revalidation failed for slot "${slotId}":`,
|
|
695
|
+
e
|
|
696
|
+
);
|
|
697
|
+
} finally {
|
|
698
|
+
backgroundRevalidations.delete(storageKey);
|
|
271
699
|
}
|
|
700
|
+
})();
|
|
701
|
+
const ctx = reqContext?.platformContext?.ctx;
|
|
702
|
+
if (ctx && typeof ctx.waitUntil === "function") {
|
|
703
|
+
ctx.waitUntil(bgPromise);
|
|
704
|
+
}
|
|
705
|
+
}
|
|
272
706
|
|
|
273
|
-
|
|
707
|
+
if (!isExpired || serveStaleWhileRevalidate) {
|
|
708
|
+
try {
|
|
709
|
+
const restored = deserializeJSX(cached.content);
|
|
710
|
+
const withRefreshedNested = await refreshNestedSlotsInTree(restored, slotId);
|
|
711
|
+
commitSlotTags(slotTagsToCommit);
|
|
712
|
+
return wrapWithBoundary(withRefreshedNested, false);
|
|
274
713
|
} catch (err) {}
|
|
275
714
|
}
|
|
276
715
|
}
|
|
277
716
|
|
|
278
|
-
// 2. Cache MISS or expired
|
|
717
|
+
// 2. Cache MISS or expired TTL: Deduplicate synchronous render
|
|
279
718
|
if (inflightSlotRenders.has(storageKey)) {
|
|
280
719
|
try {
|
|
281
720
|
const deduped = await inflightSlotRenders.get(storageKey);
|
|
282
|
-
|
|
721
|
+
commitSlotTags(slotTagsToCommit);
|
|
722
|
+
return wrapWithBoundary(deduped, !isPprBuildActive());
|
|
283
723
|
} catch (e) {}
|
|
284
724
|
}
|
|
285
725
|
|
|
726
|
+
const inPprSuspense = isPprBuildActive() && isInsideOuterSuspenseOutsideSlot();
|
|
727
|
+
const isPprShellBuild =
|
|
728
|
+
isPprBuildActive() ||
|
|
729
|
+
Boolean(getContext()?.platformContext?.isSSG) ||
|
|
730
|
+
isRevalidationActive() ||
|
|
731
|
+
!getContext()?._isPprRuntimeResume;
|
|
732
|
+
|
|
286
733
|
const renderPromise = (async () => {
|
|
287
|
-
const
|
|
288
|
-
|
|
734
|
+
const resolved = await runInCachedSlot(async () => {
|
|
735
|
+
const childElement = typeof children === "function" ? await children() : children;
|
|
736
|
+
return await asyncRenderJSXToClientJSX(childElement);
|
|
737
|
+
});
|
|
289
738
|
const serialized = serializeJSX(resolved);
|
|
290
739
|
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
}
|
|
299
|
-
|
|
740
|
+
const slotMeta = {
|
|
741
|
+
tags: allTags,
|
|
742
|
+
revalidate: typeof revalidate === "number" ? revalidate : undefined,
|
|
743
|
+
generatedAt: Date.now(),
|
|
744
|
+
slotId,
|
|
745
|
+
inStaticShell: Boolean(isPprShellBuild || cached?.metadata?.inStaticShell),
|
|
746
|
+
inStaticSuspense: Boolean(inPprSuspense || cached?.metadata?.inStaticSuspense),
|
|
747
|
+
};
|
|
748
|
+
|
|
749
|
+
const persistSlot = async () => {
|
|
750
|
+
try {
|
|
751
|
+
await storage.set(storageKey, serialized, slotMeta);
|
|
752
|
+
} catch (err) {
|
|
753
|
+
console.error(`[DinouCacheSlot] Failed to write cache for slot "${slotId}":`, err);
|
|
754
|
+
}
|
|
755
|
+
};
|
|
756
|
+
|
|
757
|
+
if (inPprSuspense) {
|
|
758
|
+
if (!deferPprSuspenseEffect(persistSlot)) {
|
|
759
|
+
await persistSlot();
|
|
760
|
+
}
|
|
761
|
+
} else {
|
|
762
|
+
await persistSlot();
|
|
300
763
|
}
|
|
301
764
|
|
|
302
765
|
return deserializeJSX(serialized);
|
|
@@ -305,94 +768,376 @@ async function DinouCacheSlot(props) {
|
|
|
305
768
|
inflightSlotRenders.set(storageKey, renderPromise);
|
|
306
769
|
try {
|
|
307
770
|
const fresh = await renderPromise;
|
|
308
|
-
|
|
771
|
+
commitSlotTags(slotTagsToCommit);
|
|
772
|
+
return wrapWithBoundary(fresh, true);
|
|
309
773
|
} finally {
|
|
310
774
|
inflightSlotRenders.delete(storageKey);
|
|
311
775
|
}
|
|
312
776
|
}
|
|
777
|
+
DinouCacheSlot.$$dinouCacheSlot = true;
|
|
778
|
+
|
|
779
|
+
/**
|
|
780
|
+
* Resolves a slot ID or tag to its canonical slotId using slotRegistry or storage metadata.
|
|
781
|
+
*/
|
|
782
|
+
async function resolveCanonicalSlotId(slotIdOrTag, storage) {
|
|
783
|
+
if (slotRegistry.has(slotIdOrTag)) {
|
|
784
|
+
return slotIdOrTag;
|
|
785
|
+
}
|
|
786
|
+
for (const [regId, entry] of slotRegistry.entries()) {
|
|
787
|
+
if (Array.isArray(entry.tags) && entry.tags.includes(slotIdOrTag)) {
|
|
788
|
+
return regId;
|
|
789
|
+
}
|
|
790
|
+
}
|
|
791
|
+
if (storage) {
|
|
792
|
+
try {
|
|
793
|
+
const direct = await getSlotFromStorage(storage, `slots/${slotIdOrTag}/slot.json`);
|
|
794
|
+
if (direct && direct.content) {
|
|
795
|
+
return slotIdOrTag;
|
|
796
|
+
}
|
|
797
|
+
if (typeof storage.keys === "function") {
|
|
798
|
+
const keys = await storage.keys("slots/");
|
|
799
|
+
for (const rawKey of keys) {
|
|
800
|
+
const key = String(rawKey || "").replace(/\\/g, "/");
|
|
801
|
+
if (key.endsWith("/slot.json") || key.endsWith("/slot.metadata.json")) {
|
|
802
|
+
const item = await storage.get(key);
|
|
803
|
+
let meta = item?.metadata;
|
|
804
|
+
if (!meta && key.endsWith("/slot.metadata.json") && item?.content) {
|
|
805
|
+
try {
|
|
806
|
+
meta = JSON.parse(item.content);
|
|
807
|
+
} catch (e) {}
|
|
808
|
+
}
|
|
809
|
+
if (meta && Array.isArray(meta.tags) && meta.tags.includes(slotIdOrTag)) {
|
|
810
|
+
if (meta.slotId) return meta.slotId;
|
|
811
|
+
const match = key.match(/^slots\/([^/]+)\//);
|
|
812
|
+
if (match) return match[1];
|
|
813
|
+
}
|
|
814
|
+
}
|
|
815
|
+
}
|
|
816
|
+
}
|
|
817
|
+
} catch (e) {}
|
|
818
|
+
}
|
|
819
|
+
return slotIdOrTag;
|
|
820
|
+
}
|
|
313
821
|
|
|
314
822
|
/**
|
|
315
823
|
* Resolves a slot's JSX tree for the granular /____rsc_slot____/:id endpoint.
|
|
316
824
|
*/
|
|
317
825
|
async function getSlotJSX(slotId, options = {}) {
|
|
318
826
|
const storage = getStorageAdapter();
|
|
319
|
-
|
|
827
|
+
let targetId = await resolveCanonicalSlotId(slotId, storage);
|
|
828
|
+
let storageKey = `slots/${targetId}/slot.json`;
|
|
320
829
|
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
830
|
+
const reqUrlEarly = String(options.req?.url || "");
|
|
831
|
+
const qIdxEarly = reqUrlEarly.indexOf("?");
|
|
832
|
+
const queryKeyEarly = qIdxEarly !== -1 ? reqUrlEarly.slice(qIdxEarly) : "";
|
|
833
|
+
const earlyStampedeKey = `${storageKey}::${queryKeyEarly}`;
|
|
834
|
+
const earlyInflightKey = options.fresh ? earlyStampedeKey : storageKey;
|
|
835
|
+
|
|
836
|
+
if (inflightSlotRenders.has(earlyInflightKey)) {
|
|
324
837
|
try {
|
|
325
|
-
|
|
838
|
+
const deduped = await inflightSlotRenders.get(earlyInflightKey);
|
|
839
|
+
return await refreshNestedSlotsInTree(deduped, targetId, options);
|
|
326
840
|
} catch (e) {}
|
|
327
841
|
}
|
|
328
842
|
|
|
843
|
+
if (options.fresh && recentFreshSlotRenders.has(earlyStampedeKey)) {
|
|
844
|
+
const recent = recentFreshSlotRenders.get(earlyStampedeKey);
|
|
845
|
+
if (recent && Date.now() - recent.timestamp < 500 && recent.serialized) {
|
|
846
|
+
recent.timestamp = Date.now();
|
|
847
|
+
const restored = deserializeJSX(recent.serialized);
|
|
848
|
+
return await refreshNestedSlotsInTree(restored, targetId, options);
|
|
849
|
+
}
|
|
850
|
+
recentFreshSlotRenders.delete(earlyStampedeKey);
|
|
851
|
+
}
|
|
852
|
+
|
|
853
|
+
// 1. Check storage cache
|
|
854
|
+
let cached = await getSlotFromStorage(storage, storageKey);
|
|
855
|
+
|
|
856
|
+
const regEntryInitial = slotRegistry.get(targetId);
|
|
329
857
|
const now = Date.now();
|
|
330
858
|
if (cached && cached.content) {
|
|
331
859
|
const meta = cached.metadata || {};
|
|
332
860
|
const generatedAt = meta.generatedAt || 0;
|
|
333
861
|
const revalidateSeconds =
|
|
334
|
-
typeof
|
|
862
|
+
typeof regEntryInitial?.revalidate === "number"
|
|
863
|
+
? regEntryInitial.revalidate
|
|
864
|
+
: typeof meta.revalidate === "number"
|
|
865
|
+
? meta.revalidate
|
|
866
|
+
: undefined;
|
|
335
867
|
|
|
868
|
+
const isNoCache = revalidateSeconds === 0;
|
|
336
869
|
const isExpired =
|
|
337
|
-
|
|
338
|
-
revalidateSeconds
|
|
339
|
-
|
|
870
|
+
isNoCache ||
|
|
871
|
+
(typeof revalidateSeconds === "number" &&
|
|
872
|
+
revalidateSeconds > 0 &&
|
|
873
|
+
now > generatedAt + revalidateSeconds * 1000);
|
|
340
874
|
|
|
341
|
-
|
|
875
|
+
const serveStaleWhileRevalidate =
|
|
876
|
+
isExpired &&
|
|
877
|
+
!isNoCache &&
|
|
878
|
+
!options.fresh &&
|
|
879
|
+
Boolean(options.skipHostRevalidate) &&
|
|
880
|
+
Boolean(meta.inStaticShell) &&
|
|
881
|
+
!isRevalidationActive() &&
|
|
882
|
+
!isPprBuildActive() &&
|
|
883
|
+
!getContext()?.platformContext?.isSSG;
|
|
884
|
+
|
|
885
|
+
if ((!isExpired && !options.fresh) || serveStaleWhileRevalidate) {
|
|
342
886
|
try {
|
|
343
|
-
|
|
887
|
+
const restored = deserializeJSX(cached.content);
|
|
888
|
+
return await refreshNestedSlotsInTree(restored, targetId, options);
|
|
344
889
|
} catch (e) {}
|
|
345
890
|
}
|
|
346
891
|
}
|
|
347
892
|
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
893
|
+
async function discoverFromRegisteredSlots() {
|
|
894
|
+
const attempted = new Set();
|
|
895
|
+
let progressed = true;
|
|
896
|
+
while (!slotRegistry.has(targetId) && progressed) {
|
|
897
|
+
progressed = false;
|
|
898
|
+
for (const [regKey, entry] of Array.from(slotRegistry.entries())) {
|
|
899
|
+
if (attempted.has(regKey)) continue;
|
|
900
|
+
attempted.add(regKey);
|
|
901
|
+
progressed = true;
|
|
902
|
+
if (typeof entry.discover === "function") {
|
|
903
|
+
await entry.discover();
|
|
904
|
+
targetId = await resolveCanonicalSlotId(slotId, storage);
|
|
905
|
+
if (slotRegistry.has(targetId)) return;
|
|
906
|
+
}
|
|
907
|
+
}
|
|
355
908
|
}
|
|
356
909
|
}
|
|
357
910
|
|
|
358
|
-
|
|
359
|
-
|
|
911
|
+
// 2. First try discovering nested slots from already-registered outer slots
|
|
912
|
+
if (!slotRegistry.has(targetId)) {
|
|
913
|
+
await discoverFromRegisteredSlots();
|
|
914
|
+
}
|
|
915
|
+
|
|
916
|
+
// 3. If still not in slotRegistry (e.g., cold server start), pre-evaluate the route once
|
|
917
|
+
if (!slotRegistry.has(targetId) && options.currentPath) {
|
|
918
|
+
if (preEvaluatingRoutes.has(options.currentPath)) {
|
|
919
|
+
try {
|
|
920
|
+
await preEvaluatingRoutes.get(options.currentPath);
|
|
921
|
+
} catch (e) {}
|
|
922
|
+
targetId = await resolveCanonicalSlotId(slotId, storage);
|
|
923
|
+
} else {
|
|
924
|
+
const preEvalPromise = (async () => {
|
|
925
|
+
try {
|
|
926
|
+
const isNotFound = { value: false };
|
|
927
|
+
await runWithPprContext({ isPpr: true }, async () => {
|
|
928
|
+
await runInSlotDiscovery(async () => {
|
|
929
|
+
const pageJsx = await getJSX(options.currentPath, {}, isNotFound, false, false, {
|
|
930
|
+
segment: "page",
|
|
931
|
+
});
|
|
932
|
+
if (pageJsx) {
|
|
933
|
+
await asyncRenderJSXToClientJSX(pageJsx);
|
|
934
|
+
}
|
|
935
|
+
const resolvedId = await resolveCanonicalSlotId(slotId, storage);
|
|
936
|
+
|
|
937
|
+
if (!slotRegistry.has(resolvedId)) {
|
|
938
|
+
const fullJsx = await getJSX(options.currentPath, {}, isNotFound, false, false);
|
|
939
|
+
if (fullJsx) {
|
|
940
|
+
await asyncRenderJSXToClientJSX(fullJsx);
|
|
941
|
+
}
|
|
942
|
+
}
|
|
943
|
+
});
|
|
944
|
+
});
|
|
945
|
+
} catch (e) {
|
|
946
|
+
console.warn(
|
|
947
|
+
`[getSlotJSX] Could not pre-evaluate route "${options.currentPath}" to register slot "${slotId}":`,
|
|
948
|
+
e
|
|
949
|
+
);
|
|
950
|
+
} finally {
|
|
951
|
+
preEvaluatingRoutes.delete(options.currentPath);
|
|
952
|
+
}
|
|
953
|
+
})();
|
|
954
|
+
preEvaluatingRoutes.set(options.currentPath, preEvalPromise);
|
|
955
|
+
await preEvalPromise;
|
|
956
|
+
targetId = await resolveCanonicalSlotId(slotId, storage);
|
|
957
|
+
}
|
|
958
|
+
|
|
959
|
+
if (!slotRegistry.has(targetId)) {
|
|
960
|
+
await discoverFromRegisteredSlots();
|
|
961
|
+
}
|
|
962
|
+
}
|
|
963
|
+
|
|
964
|
+
storageKey = `slots/${targetId}/slot.json`;
|
|
965
|
+
|
|
966
|
+
if (slotRegistry.has(targetId)) {
|
|
967
|
+
const entry = slotRegistry.get(targetId);
|
|
968
|
+
const reqUrl = String(options.req?.url || "");
|
|
969
|
+
const qIdx = reqUrl.indexOf("?");
|
|
970
|
+
const queryKey = qIdx !== -1 ? reqUrl.slice(qIdx) : "";
|
|
971
|
+
const stampedeKey = `${storageKey}::${queryKey}`;
|
|
972
|
+
const inflightKey = options.fresh ? stampedeKey : storageKey;
|
|
360
973
|
try {
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
await storage.set(storageKey, serialized, {
|
|
365
|
-
tags: entry.tags,
|
|
366
|
-
revalidate: entry.revalidate,
|
|
367
|
-
generatedAt: Date.now(),
|
|
368
|
-
slotId,
|
|
369
|
-
});
|
|
974
|
+
if (inflightSlotRenders.has(inflightKey)) {
|
|
975
|
+
const deduped = await inflightSlotRenders.get(inflightKey);
|
|
976
|
+
return await refreshNestedSlotsInTree(deduped, targetId, options);
|
|
370
977
|
}
|
|
371
|
-
|
|
978
|
+
|
|
979
|
+
if (options.fresh && recentFreshSlotRenders.has(stampedeKey)) {
|
|
980
|
+
const recent = recentFreshSlotRenders.get(stampedeKey);
|
|
981
|
+
if (recent && Date.now() - recent.timestamp < 500 && recent.serialized) {
|
|
982
|
+
recent.timestamp = Date.now();
|
|
983
|
+
const restored = deserializeJSX(recent.serialized);
|
|
984
|
+
return await refreshNestedSlotsInTree(restored, targetId, options);
|
|
985
|
+
}
|
|
986
|
+
recentFreshSlotRenders.delete(stampedeKey);
|
|
987
|
+
}
|
|
988
|
+
|
|
989
|
+
const renderGen = (slotRenderGenerations.get(storageKey) || 0) + 1;
|
|
990
|
+
slotRenderGenerations.set(storageKey, renderGen);
|
|
991
|
+
const isLatestGen = () => slotRenderGenerations.get(storageKey) === renderGen;
|
|
992
|
+
|
|
993
|
+
const renderPromise = (async () => {
|
|
994
|
+
const resolved = await entry.render();
|
|
995
|
+
const serialized = serializeJSX(resolved);
|
|
996
|
+
if (options.fresh && isLatestGen()) {
|
|
997
|
+
recentFreshSlotRenders.set(stampedeKey, {
|
|
998
|
+
timestamp: Date.now(),
|
|
999
|
+
serialized,
|
|
1000
|
+
});
|
|
1001
|
+
}
|
|
1002
|
+
if (storage && entry.revalidate !== 0 && isLatestGen()) {
|
|
1003
|
+
await storage.set(storageKey, serialized, {
|
|
1004
|
+
tags: entry.tags,
|
|
1005
|
+
revalidate: entry.revalidate,
|
|
1006
|
+
generatedAt: Date.now(),
|
|
1007
|
+
slotId: targetId,
|
|
1008
|
+
inStaticShell: Boolean(cached?.metadata?.inStaticShell),
|
|
1009
|
+
inStaticSuspense: Boolean(cached?.metadata?.inStaticSuspense),
|
|
1010
|
+
});
|
|
1011
|
+
}
|
|
1012
|
+
if (options.fresh && isLatestGen()) {
|
|
1013
|
+
recentFreshSlotRenders.set(stampedeKey, {
|
|
1014
|
+
timestamp: Date.now(),
|
|
1015
|
+
serialized,
|
|
1016
|
+
});
|
|
1017
|
+
}
|
|
1018
|
+
return deserializeJSX(serialized);
|
|
1019
|
+
})();
|
|
1020
|
+
|
|
1021
|
+
let restored;
|
|
1022
|
+
inflightSlotRenders.set(inflightKey, renderPromise);
|
|
1023
|
+
try {
|
|
1024
|
+
restored = await renderPromise;
|
|
1025
|
+
} finally {
|
|
1026
|
+
inflightSlotRenders.delete(inflightKey);
|
|
1027
|
+
}
|
|
1028
|
+
|
|
1029
|
+
if (
|
|
1030
|
+
storage &&
|
|
1031
|
+
entry.revalidate !== 0 &&
|
|
1032
|
+
!options.skipHostRevalidate &&
|
|
1033
|
+
options.currentPath &&
|
|
1034
|
+
isLatestGen()
|
|
1035
|
+
) {
|
|
1036
|
+
try {
|
|
1037
|
+
const cleanRouteKey = String(options.currentPath)
|
|
1038
|
+
.split("?")[0]
|
|
1039
|
+
.replace(/^\/+/, "")
|
|
1040
|
+
.replace(/\/+$/, "");
|
|
1041
|
+
const slotTagsToMatch = new Set([
|
|
1042
|
+
targetId,
|
|
1043
|
+
slotId,
|
|
1044
|
+
...(Array.isArray(entry.tags) ? entry.tags : []),
|
|
1045
|
+
]);
|
|
1046
|
+
const metaKey = cleanRouteKey ? `${cleanRouteKey}/metadata.json` : "metadata.json";
|
|
1047
|
+
const pageMeta = await readMetaFromStorageOrAssets(storage, metaKey);
|
|
1048
|
+
if (
|
|
1049
|
+
pageMeta &&
|
|
1050
|
+
Array.isArray(pageMeta.slotTags) &&
|
|
1051
|
+
pageMeta.slotTags.some((t) => slotTagsToMatch.has(t))
|
|
1052
|
+
) {
|
|
1053
|
+
const { revalidatePage } = await import("./cache-revalidate.js");
|
|
1054
|
+
await revalidatePage("/" + cleanRouteKey, { onlyRefreshSlots: true });
|
|
1055
|
+
}
|
|
1056
|
+
const segments = cleanRouteKey.split("/").filter(Boolean);
|
|
1057
|
+
const candidateLayoutKeys = [""];
|
|
1058
|
+
let acc = "";
|
|
1059
|
+
for (const seg of segments) {
|
|
1060
|
+
acc = acc ? `${acc}/${seg}` : seg;
|
|
1061
|
+
candidateLayoutKeys.push(acc);
|
|
1062
|
+
}
|
|
1063
|
+
for (const lKey of candidateLayoutKeys) {
|
|
1064
|
+
const lMetaKey = lKey ? `${lKey}/layout.metadata.json` : "layout.metadata.json";
|
|
1065
|
+
const lMeta = await readMetaFromStorageOrAssets(storage, lMetaKey);
|
|
1066
|
+
if (
|
|
1067
|
+
lMeta &&
|
|
1068
|
+
Array.isArray(lMeta.slotTags) &&
|
|
1069
|
+
lMeta.slotTags.some((t) => slotTagsToMatch.has(t))
|
|
1070
|
+
) {
|
|
1071
|
+
const { revalidateLayout } = await import("./cache-revalidate.js");
|
|
1072
|
+
await revalidateLayout(lKey ? "/" + lKey : "/", { onlyRefreshSlots: true });
|
|
1073
|
+
}
|
|
1074
|
+
}
|
|
1075
|
+
} catch (e) {}
|
|
1076
|
+
}
|
|
1077
|
+
|
|
1078
|
+
return await refreshNestedSlotsInTree(restored, targetId, options);
|
|
372
1079
|
} catch (e) {
|
|
373
|
-
console.error(`[getSlotJSX] Failed to regenerate slot "${
|
|
1080
|
+
console.error(`[getSlotJSX] Failed to regenerate slot "${targetId}":`, e);
|
|
374
1081
|
}
|
|
375
1082
|
}
|
|
376
1083
|
|
|
377
1084
|
// Fallback to cached content if regeneration failed
|
|
378
1085
|
if (cached && cached.content) {
|
|
379
1086
|
try {
|
|
380
|
-
|
|
1087
|
+
const restored = deserializeJSX(cached.content);
|
|
1088
|
+
return await refreshNestedSlotsInTree(restored, targetId, options);
|
|
381
1089
|
} catch (e) {}
|
|
382
1090
|
}
|
|
383
1091
|
|
|
384
1092
|
return null;
|
|
385
1093
|
}
|
|
386
1094
|
|
|
1095
|
+
async function hasExpiredSlots(slotTags) {
|
|
1096
|
+
if (!Array.isArray(slotTags) || slotTags.length === 0) return false;
|
|
1097
|
+
const storage = getStorageAdapter();
|
|
1098
|
+
if (!storage) return false;
|
|
1099
|
+
const now = Date.now();
|
|
1100
|
+
const checkedIds = new Set();
|
|
1101
|
+
for (const rawTagOrId of slotTags) {
|
|
1102
|
+
if (!rawTagOrId) continue;
|
|
1103
|
+
const targetId = await resolveCanonicalSlotId(rawTagOrId, storage);
|
|
1104
|
+
if (checkedIds.has(targetId)) continue;
|
|
1105
|
+
checkedIds.add(targetId);
|
|
1106
|
+
const storageKey = `slots/${targetId}/slot.json`;
|
|
1107
|
+
const cached = await getSlotFromStorage(storage, storageKey);
|
|
1108
|
+
if (!cached || !cached.content) continue;
|
|
1109
|
+
const meta = cached.metadata || {};
|
|
1110
|
+
const regEntry = slotRegistry.get(targetId);
|
|
1111
|
+
const revalidateSeconds =
|
|
1112
|
+
typeof regEntry?.revalidate === "number"
|
|
1113
|
+
? regEntry.revalidate
|
|
1114
|
+
: typeof meta.revalidate === "number"
|
|
1115
|
+
? meta.revalidate
|
|
1116
|
+
: undefined;
|
|
1117
|
+
if (
|
|
1118
|
+
typeof revalidateSeconds === "number" &&
|
|
1119
|
+
revalidateSeconds > 0 &&
|
|
1120
|
+
now > (meta.generatedAt || 0) + revalidateSeconds * 1000
|
|
1121
|
+
) {
|
|
1122
|
+
return true;
|
|
1123
|
+
}
|
|
1124
|
+
}
|
|
1125
|
+
return false;
|
|
1126
|
+
}
|
|
1127
|
+
|
|
387
1128
|
export {
|
|
388
1129
|
DinouCacheSlot,
|
|
389
1130
|
getSlotJSX,
|
|
390
1131
|
serializeJSX,
|
|
391
1132
|
deserializeJSX,
|
|
1133
|
+
refreshNestedSlotsInTree,
|
|
1134
|
+
hasExpiredSlots,
|
|
392
1135
|
};
|
|
393
1136
|
export default {
|
|
394
1137
|
DinouCacheSlot,
|
|
395
1138
|
getSlotJSX,
|
|
396
1139
|
serializeJSX,
|
|
397
1140
|
deserializeJSX,
|
|
1141
|
+
refreshNestedSlotsInTree,
|
|
1142
|
+
hasExpiredSlots,
|
|
398
1143
|
};
|