dinou 9.0.2 → 9.0.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/CHANGELOG.md +54 -0
- package/README.md +503 -9883
- 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/build-static-pages.js +15 -7
- package/dinou/core/cache-revalidate.js +909 -151
- package/dinou/core/cache-slot.js +728 -97
- 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 +333 -329
- package/dinou/core/handler.js +2983 -2343
- package/dinou/core/layout-functions.js +102 -1
- package/dinou/core/ppr-context.js +256 -105
- package/dinou/core/ppr-runtime.js +540 -267
- package/dinou/core/render-jsx-to-client-jsx.js +131 -71
- 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/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 +5 -2
- 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 +25 -11
- package/dinou/server.js +5 -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
|
@@ -1,9 +1,40 @@
|
|
|
1
|
+
import React from "react";
|
|
1
2
|
import {
|
|
2
3
|
isPprBuildActive,
|
|
3
|
-
|
|
4
|
-
|
|
4
|
+
runInSuspense,
|
|
5
|
+
commitPprSuspenseEffects,
|
|
5
6
|
registerHole,
|
|
7
|
+
isPageRootElement,
|
|
8
|
+
runInPageSegment,
|
|
6
9
|
} from "./ppr-context.js";
|
|
10
|
+
import { getContext } from "./request-context.js";
|
|
11
|
+
|
|
12
|
+
function cloneWithDevProps(jsx, newProps, resolvedKey, overrideType) {
|
|
13
|
+
const cloned = {
|
|
14
|
+
...jsx,
|
|
15
|
+
$$typeof: Symbol.for("react.transitional.element"),
|
|
16
|
+
type: overrideType !== undefined ? overrideType : jsx.type,
|
|
17
|
+
props: newProps,
|
|
18
|
+
key: resolvedKey,
|
|
19
|
+
};
|
|
20
|
+
if ("_owner" in jsx) cloned._owner = jsx._owner;
|
|
21
|
+
if ("_store" in jsx && jsx._store) {
|
|
22
|
+
cloned._store = {};
|
|
23
|
+
Object.defineProperty(cloned._store, "validated", {
|
|
24
|
+
configurable: false,
|
|
25
|
+
enumerable: false,
|
|
26
|
+
writable: true,
|
|
27
|
+
value: jsx._store.validated || 1,
|
|
28
|
+
});
|
|
29
|
+
}
|
|
30
|
+
for (const devProp of ["ref", "_debugInfo", "_debugStack", "_debugTask"]) {
|
|
31
|
+
const desc = Object.getOwnPropertyDescriptor(jsx, devProp);
|
|
32
|
+
if (desc) {
|
|
33
|
+
Object.defineProperty(cloned, devProp, desc);
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
return cloned;
|
|
37
|
+
}
|
|
7
38
|
|
|
8
39
|
// Function to check if a component is a client component
|
|
9
40
|
function isClientComponent(type) {
|
|
@@ -18,6 +49,20 @@ function isClientComponent(type) {
|
|
|
18
49
|
}
|
|
19
50
|
|
|
20
51
|
async function asyncRenderJSXToClientJSX(jsx, key = null) {
|
|
52
|
+
if (isPageRootElement(jsx)) {
|
|
53
|
+
return await runInPageSegment(async () => {
|
|
54
|
+
const resolved = await asyncRenderJSXToClientJSXInner(jsx, key);
|
|
55
|
+
const ctx = getContext();
|
|
56
|
+
if (ctx) {
|
|
57
|
+
ctx._resolvedPageClientJsx = resolved;
|
|
58
|
+
}
|
|
59
|
+
return resolved;
|
|
60
|
+
});
|
|
61
|
+
}
|
|
62
|
+
return await asyncRenderJSXToClientJSXInner(jsx, key);
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
async function asyncRenderJSXToClientJSXInner(jsx, key = null) {
|
|
21
66
|
if (
|
|
22
67
|
typeof jsx === "string" ||
|
|
23
68
|
typeof jsx === "number" ||
|
|
@@ -28,6 +73,13 @@ async function asyncRenderJSXToClientJSX(jsx, key = null) {
|
|
|
28
73
|
) {
|
|
29
74
|
return jsx;
|
|
30
75
|
} else if (Array.isArray(jsx)) {
|
|
76
|
+
if (isPprBuildActive()) {
|
|
77
|
+
const results = [];
|
|
78
|
+
for (const child of jsx) {
|
|
79
|
+
results.push(await asyncRenderJSXToClientJSX(child, child?.key ?? null));
|
|
80
|
+
}
|
|
81
|
+
return results;
|
|
82
|
+
}
|
|
31
83
|
return await Promise.all(
|
|
32
84
|
jsx.map((child) =>
|
|
33
85
|
asyncRenderJSXToClientJSX(
|
|
@@ -39,12 +91,7 @@ async function asyncRenderJSXToClientJSX(jsx, key = null) {
|
|
|
39
91
|
} else if (typeof jsx === "symbol") {
|
|
40
92
|
if (jsx === Symbol.for("react.fragment")) {
|
|
41
93
|
// Handle Fragment as an empty props object
|
|
42
|
-
return {
|
|
43
|
-
$$typeof: Symbol.for("react.transitional.element"),
|
|
44
|
-
type: Symbol.for("react.fragment"),
|
|
45
|
-
props: {},
|
|
46
|
-
key,
|
|
47
|
-
};
|
|
94
|
+
return React.createElement(React.Fragment, key != null ? { key } : null);
|
|
48
95
|
}
|
|
49
96
|
console.error("Unsupported symbol:", String(jsx));
|
|
50
97
|
throw new Error(`Unsupported symbol: ${String(jsx)}`);
|
|
@@ -52,68 +99,76 @@ async function asyncRenderJSXToClientJSX(jsx, key = null) {
|
|
|
52
99
|
if (jsx.$$typeof === Symbol.for("react.transitional.element")) {
|
|
53
100
|
if (jsx.type === Symbol.for("react.suspense")) {
|
|
54
101
|
if (isPprBuildActive()) {
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
exitSuspense();
|
|
59
|
-
return {
|
|
60
|
-
...jsx,
|
|
61
|
-
props: {
|
|
62
|
-
...jsx.props,
|
|
63
|
-
children: renderedChildren,
|
|
64
|
-
fallback: await asyncRenderJSXToClientJSX(jsx.props?.fallback),
|
|
65
|
-
},
|
|
66
|
-
key: key ?? jsx.key,
|
|
67
|
-
};
|
|
68
|
-
} catch (err) {
|
|
69
|
-
exitSuspense();
|
|
70
|
-
if (
|
|
71
|
-
(err && err.$$typeof === Symbol.for("dinou.ppr.postpone")) ||
|
|
72
|
-
(err && typeof err?.then === "function") ||
|
|
73
|
-
(err && err?.$$typeof === Symbol.for("react.postpone"))
|
|
74
|
-
) {
|
|
75
|
-
registerHole(holeId, { type: err.pprType || "suspense" });
|
|
102
|
+
return await runInSuspense(async (holeId, suspenseStore) => {
|
|
103
|
+
try {
|
|
104
|
+
const renderedChildren = await asyncRenderJSXToClientJSX(jsx.props?.children);
|
|
76
105
|
const renderedFallback = await asyncRenderJSXToClientJSX(jsx.props?.fallback);
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
106
|
+
await commitPprSuspenseEffects(suspenseStore);
|
|
107
|
+
return cloneWithDevProps(
|
|
108
|
+
jsx,
|
|
109
|
+
{
|
|
110
|
+
...jsx.props,
|
|
111
|
+
children: renderedChildren,
|
|
112
|
+
fallback: renderedFallback,
|
|
84
113
|
},
|
|
85
|
-
key
|
|
86
|
-
|
|
114
|
+
key ?? jsx.key
|
|
115
|
+
);
|
|
116
|
+
} catch (err) {
|
|
117
|
+
if (
|
|
118
|
+
(err && err.$$typeof === Symbol.for("dinou.ppr.postpone")) ||
|
|
119
|
+
(err && typeof err?.then === "function") ||
|
|
120
|
+
(err && err?.$$typeof === Symbol.for("react.postpone"))
|
|
121
|
+
) {
|
|
122
|
+
registerHole(holeId, { type: err.pprType || "suspense" });
|
|
123
|
+
const renderedFallback = await asyncRenderJSXToClientJSX(jsx.props?.fallback);
|
|
124
|
+
return cloneWithDevProps(
|
|
125
|
+
jsx,
|
|
126
|
+
{
|
|
127
|
+
"data-ppr-hole": holeId,
|
|
128
|
+
style: { display: "contents" },
|
|
129
|
+
children: renderedFallback,
|
|
130
|
+
},
|
|
131
|
+
key ?? jsx.key,
|
|
132
|
+
"div"
|
|
133
|
+
);
|
|
134
|
+
}
|
|
135
|
+
throw err;
|
|
87
136
|
}
|
|
88
|
-
|
|
89
|
-
}
|
|
137
|
+
});
|
|
90
138
|
}
|
|
91
|
-
return
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
key
|
|
95
|
-
|
|
139
|
+
return cloneWithDevProps(
|
|
140
|
+
jsx,
|
|
141
|
+
await asyncRenderJSXToClientJSX(jsx.props),
|
|
142
|
+
key ?? jsx.key
|
|
143
|
+
);
|
|
96
144
|
} else if (
|
|
97
145
|
jsx.type === Symbol.for("react.fragment") ||
|
|
98
146
|
jsx.type === Symbol.for("react.view_transition") ||
|
|
99
147
|
typeof jsx.type === "string"
|
|
100
148
|
) {
|
|
101
|
-
return
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
key
|
|
105
|
-
|
|
149
|
+
return cloneWithDevProps(
|
|
150
|
+
jsx,
|
|
151
|
+
await asyncRenderJSXToClientJSX(jsx.props),
|
|
152
|
+
key ?? jsx.key
|
|
153
|
+
);
|
|
106
154
|
} else if (typeof jsx.type === "function") {
|
|
107
155
|
const Component = jsx.type;
|
|
108
156
|
const props = jsx.props;
|
|
109
157
|
if (isClientComponent(Component)) {
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
158
|
+
if (jsx.__clientJsxAlreadyRendered) {
|
|
159
|
+
return cloneWithDevProps(
|
|
160
|
+
jsx,
|
|
161
|
+
props,
|
|
162
|
+
key ?? jsx.key,
|
|
163
|
+
Component
|
|
164
|
+
);
|
|
165
|
+
}
|
|
166
|
+
return cloneWithDevProps(
|
|
167
|
+
jsx,
|
|
168
|
+
await asyncRenderJSXToClientJSX(props),
|
|
169
|
+
key ?? jsx.key,
|
|
170
|
+
Component
|
|
171
|
+
);
|
|
117
172
|
} else {
|
|
118
173
|
// Server component: execute and process
|
|
119
174
|
const returnedJsx = await Component(props);
|
|
@@ -125,13 +180,12 @@ async function asyncRenderJSXToClientJSX(jsx, key = null) {
|
|
|
125
180
|
jsx.type.$$typeof === Symbol.for("react.forward_ref")
|
|
126
181
|
) {
|
|
127
182
|
if (isClientComponent(jsx.type)) {
|
|
128
|
-
return
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
};
|
|
183
|
+
return cloneWithDevProps(
|
|
184
|
+
jsx,
|
|
185
|
+
await asyncRenderJSXToClientJSX(jsx.props),
|
|
186
|
+
key ?? jsx.key,
|
|
187
|
+
jsx.type
|
|
188
|
+
);
|
|
135
189
|
} else {
|
|
136
190
|
const returnedJsx = await jsx.type.render(jsx.props, jsx.ref);
|
|
137
191
|
return await asyncRenderJSXToClientJSX(returnedJsx, key ?? jsx.key);
|
|
@@ -142,13 +196,12 @@ async function asyncRenderJSXToClientJSX(jsx, key = null) {
|
|
|
142
196
|
jsx.type.$$typeof === Symbol.for("react.memo")
|
|
143
197
|
) {
|
|
144
198
|
if (isClientComponent(jsx.type)) {
|
|
145
|
-
return
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
};
|
|
199
|
+
return cloneWithDevProps(
|
|
200
|
+
jsx,
|
|
201
|
+
await asyncRenderJSXToClientJSX(jsx.props),
|
|
202
|
+
key ?? jsx.key,
|
|
203
|
+
jsx.type
|
|
204
|
+
);
|
|
152
205
|
} else {
|
|
153
206
|
const innerComp = jsx.type.type;
|
|
154
207
|
const returnedJsx = typeof innerComp === "function" ? await innerComp(jsx.props) : null;
|
|
@@ -162,6 +215,13 @@ async function asyncRenderJSXToClientJSX(jsx, key = null) {
|
|
|
162
215
|
return jsx;
|
|
163
216
|
} else {
|
|
164
217
|
// Process object props (e.g., { className: "foo" })
|
|
218
|
+
if (isPprBuildActive()) {
|
|
219
|
+
const entries = [];
|
|
220
|
+
for (const [propName, value] of Object.entries(jsx)) {
|
|
221
|
+
entries.push([propName, await asyncRenderJSXToClientJSX(value)]);
|
|
222
|
+
}
|
|
223
|
+
return Object.fromEntries(entries);
|
|
224
|
+
}
|
|
165
225
|
return Object.fromEntries(
|
|
166
226
|
await Promise.all(
|
|
167
227
|
Object.entries(jsx).map(async ([propName, value]) => [
|
package/dinou/core/slot.js
CHANGED
|
@@ -7,6 +7,7 @@ const useContext = React.useContext;
|
|
|
7
7
|
const useMemo = React.useMemo;
|
|
8
8
|
const useState = React.useState;
|
|
9
9
|
const useEffect = React.useEffect;
|
|
10
|
+
const useLayoutEffect = React.useLayoutEffect || React.useEffect;
|
|
10
11
|
const startTransition = React.startTransition;
|
|
11
12
|
const use = React.use;
|
|
12
13
|
const createElement = React.createElement;
|
|
@@ -217,7 +218,21 @@ class SlotErrorBoundary extends Component {
|
|
|
217
218
|
}
|
|
218
219
|
}
|
|
219
220
|
|
|
220
|
-
function PageConsumer({ pagePromise }) {
|
|
221
|
+
function PageConsumer({ pagePromise, pageElement }) {
|
|
222
|
+
if (typeof useLayoutEffect === "function" && typeof window !== "undefined") {
|
|
223
|
+
useLayoutEffect(() => {
|
|
224
|
+
if (
|
|
225
|
+
typeof document !== "undefined" &&
|
|
226
|
+
document.body &&
|
|
227
|
+
document.body.getAttribute("data-hydrated") !== "true"
|
|
228
|
+
) {
|
|
229
|
+
document.body.setAttribute("data-hydrated", "true");
|
|
230
|
+
}
|
|
231
|
+
});
|
|
232
|
+
}
|
|
233
|
+
if (pageElement !== undefined) {
|
|
234
|
+
return pageElement;
|
|
235
|
+
}
|
|
221
236
|
if (pagePromise && pagePromise.status === "rejected") {
|
|
222
237
|
throw pagePromise.reason;
|
|
223
238
|
}
|
|
@@ -225,13 +240,19 @@ function PageConsumer({ pagePromise }) {
|
|
|
225
240
|
}
|
|
226
241
|
|
|
227
242
|
export function DinouPageSlot() {
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
243
|
+
const fallbackContext =
|
|
244
|
+
typeof globalThis !== "undefined" ? globalThis.__DINOU_SSR_SLOT_CONTEXT__ : null;
|
|
245
|
+
const slotContext =
|
|
246
|
+
typeof useContext === "function" && DinouPageContext
|
|
247
|
+
? useContext(DinouPageContext) || fallbackContext
|
|
248
|
+
: fallbackContext;
|
|
232
249
|
if (!slotContext) {
|
|
233
250
|
return null;
|
|
234
251
|
}
|
|
252
|
+
const pageElement =
|
|
253
|
+
slotContext && slotContext.pageElement !== undefined
|
|
254
|
+
? slotContext.pageElement
|
|
255
|
+
: undefined;
|
|
235
256
|
const pagePromise =
|
|
236
257
|
slotContext && slotContext.pagePromise !== undefined
|
|
237
258
|
? slotContext.pagePromise
|
|
@@ -244,57 +265,249 @@ export function DinouPageSlot() {
|
|
|
244
265
|
return createElement(
|
|
245
266
|
SlotErrorBoundary,
|
|
246
267
|
{ resetKey, pagePromise },
|
|
247
|
-
createElement(PageConsumer, { pagePromise })
|
|
268
|
+
createElement(PageConsumer, { pagePromise, pageElement })
|
|
248
269
|
);
|
|
249
270
|
}
|
|
250
271
|
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
272
|
+
const slotOverrideCache =
|
|
273
|
+
typeof globalThis !== "undefined"
|
|
274
|
+
? (globalThis.__DINOU_SLOT_OVERRIDE_CACHE__ =
|
|
275
|
+
globalThis.__DINOU_SLOT_OVERRIDE_CACHE__ || new Map())
|
|
276
|
+
: new Map();
|
|
277
|
+
|
|
278
|
+
const knownClientSlots =
|
|
279
|
+
typeof globalThis !== "undefined"
|
|
280
|
+
? (globalThis.__DINOU_KNOWN_CLIENT_SLOTS__ =
|
|
281
|
+
globalThis.__DINOU_KNOWN_CLIENT_SLOTS__ || new Map())
|
|
282
|
+
: new Map();
|
|
283
|
+
|
|
284
|
+
const mountedClientSlots =
|
|
285
|
+
typeof globalThis !== "undefined"
|
|
286
|
+
? (globalThis.__DINOU_MOUNTED_CLIENT_SLOTS__ =
|
|
287
|
+
globalThis.__DINOU_MOUNTED_CLIENT_SLOTS__ || new Set())
|
|
288
|
+
: new Set();
|
|
289
|
+
|
|
290
|
+
if (
|
|
291
|
+
typeof window !== "undefined" &&
|
|
292
|
+
!window.__DINOU_UNMOUNTED_SLOT_LISTENER_INSTALLED__
|
|
293
|
+
) {
|
|
294
|
+
window.__DINOU_UNMOUNTED_SLOT_LISTENER_INSTALLED__ = true;
|
|
295
|
+
window.addEventListener("dinou:refresh-slot", (e) => {
|
|
296
|
+
const targetId = e?.detail?.id;
|
|
297
|
+
const options = e?.detail?.options;
|
|
298
|
+
if (typeof window.__DINOU_FETCH_SLOT__ !== "function") return;
|
|
299
|
+
|
|
300
|
+
for (const [slotId, entry] of knownClientSlots.entries()) {
|
|
301
|
+
if (mountedClientSlots.has(slotId)) continue;
|
|
302
|
+
const matchesTag =
|
|
303
|
+
Array.isArray(entry.tags) && targetId && entry.tags.includes(targetId);
|
|
304
|
+
if (!e || !e.detail || targetId === slotId || targetId === "*" || matchesTag) {
|
|
305
|
+
const fetchOpts = {
|
|
306
|
+
...(options || {}),
|
|
307
|
+
currentPath: entry.routePath || window.location.pathname,
|
|
308
|
+
};
|
|
309
|
+
const promise = window.__DINOU_FETCH_SLOT__(slotId, fetchOpts);
|
|
310
|
+
if (promise && typeof promise.then === "function") {
|
|
311
|
+
const reqId = (entry.latestReqId || 0) + 1;
|
|
312
|
+
entry.latestReqId = reqId;
|
|
313
|
+
entry.pendingPromise = promise;
|
|
314
|
+
entry.pendingReqId = reqId;
|
|
315
|
+
const baseRef = entry.baseChildren;
|
|
316
|
+
promise.then(
|
|
317
|
+
(resolved) => {
|
|
318
|
+
if (entry.latestReqId !== reqId) {
|
|
319
|
+
return;
|
|
320
|
+
}
|
|
321
|
+
if (entry.pendingPromise === promise) {
|
|
322
|
+
entry.pendingPromise = null;
|
|
323
|
+
}
|
|
324
|
+
slotOverrideCache.set(slotId, {
|
|
325
|
+
baseChildren: baseRef,
|
|
326
|
+
content: resolved,
|
|
327
|
+
});
|
|
328
|
+
if (typeof entry.notifyMounted === "function") {
|
|
329
|
+
entry.notifyMounted(resolved, baseRef);
|
|
330
|
+
}
|
|
331
|
+
},
|
|
332
|
+
() => {
|
|
333
|
+
if (entry.pendingPromise === promise) {
|
|
334
|
+
entry.pendingPromise = null;
|
|
335
|
+
}
|
|
336
|
+
}
|
|
337
|
+
);
|
|
338
|
+
}
|
|
339
|
+
}
|
|
340
|
+
}
|
|
341
|
+
});
|
|
256
342
|
}
|
|
257
343
|
|
|
258
|
-
export function DinouCacheSlotBoundary({ id, children }) {
|
|
344
|
+
export function DinouCacheSlotBoundary({ id, tags, children }) {
|
|
259
345
|
if (typeof useState !== "function") {
|
|
260
346
|
return children;
|
|
261
347
|
}
|
|
262
|
-
const
|
|
348
|
+
const isBrowser = typeof window !== "undefined";
|
|
349
|
+
const cachedOverride = isBrowser ? slotOverrideCache.get(id) : null;
|
|
350
|
+
const initialContent =
|
|
351
|
+
cachedOverride && cachedOverride.baseChildren === children
|
|
352
|
+
? cachedOverride.content
|
|
353
|
+
: null;
|
|
354
|
+
|
|
355
|
+
const [override, setOverride] = useState({
|
|
356
|
+
baseChildren: children,
|
|
357
|
+
content: initialContent,
|
|
358
|
+
});
|
|
359
|
+
const childrenRef =
|
|
360
|
+
typeof React.useRef === "function" ? React.useRef(children) : { current: children };
|
|
361
|
+
childrenRef.current = children;
|
|
362
|
+
|
|
363
|
+
if (isBrowser) {
|
|
364
|
+
const existingKnown = knownClientSlots.get(id);
|
|
365
|
+
if (!existingKnown || existingKnown.baseChildren !== children) {
|
|
366
|
+
knownClientSlots.set(id, {
|
|
367
|
+
id,
|
|
368
|
+
tags: Array.isArray(tags) ? tags : [],
|
|
369
|
+
baseChildren: children,
|
|
370
|
+
routePath: window.location.pathname,
|
|
371
|
+
pendingPromise: existingKnown?.pendingPromise || null,
|
|
372
|
+
pendingReqId: existingKnown?.pendingReqId || 0,
|
|
373
|
+
latestReqId: existingKnown?.latestReqId || 0,
|
|
374
|
+
notifyMounted: existingKnown?.notifyMounted || null,
|
|
375
|
+
});
|
|
376
|
+
} else if (Array.isArray(tags)) {
|
|
377
|
+
existingKnown.tags = tags;
|
|
378
|
+
}
|
|
379
|
+
if (cachedOverride && cachedOverride.baseChildren !== children) {
|
|
380
|
+
slotOverrideCache.delete(id);
|
|
381
|
+
}
|
|
382
|
+
}
|
|
263
383
|
|
|
264
384
|
useEffect(() => {
|
|
385
|
+
mountedClientSlots.add(id);
|
|
386
|
+
const applyResolved = (resolved, baseRef = childrenRef.current) => {
|
|
387
|
+
slotOverrideCache.set(id, {
|
|
388
|
+
baseChildren: baseRef,
|
|
389
|
+
content: resolved,
|
|
390
|
+
});
|
|
391
|
+
const applyFn = () => {
|
|
392
|
+
setOverride({
|
|
393
|
+
baseChildren: baseRef,
|
|
394
|
+
content: resolved,
|
|
395
|
+
});
|
|
396
|
+
};
|
|
397
|
+
if (typeof startTransition === "function") {
|
|
398
|
+
startTransition(applyFn);
|
|
399
|
+
} else {
|
|
400
|
+
applyFn();
|
|
401
|
+
}
|
|
402
|
+
};
|
|
403
|
+
|
|
404
|
+
const knownEntry = knownClientSlots.get(id);
|
|
405
|
+
if (knownEntry) {
|
|
406
|
+
knownEntry.routePath = window.location.pathname;
|
|
407
|
+
knownEntry.baseChildren = childrenRef.current;
|
|
408
|
+
knownEntry.notifyMounted = applyResolved;
|
|
409
|
+
if (Array.isArray(tags)) {
|
|
410
|
+
knownEntry.tags = tags;
|
|
411
|
+
}
|
|
412
|
+
if (knownEntry.pendingPromise) {
|
|
413
|
+
const pending = knownEntry.pendingPromise;
|
|
414
|
+
const pendingReqId = knownEntry.pendingReqId;
|
|
415
|
+
pending.then(
|
|
416
|
+
(resolved) => {
|
|
417
|
+
if (knownEntry.latestReqId !== pendingReqId) {
|
|
418
|
+
return;
|
|
419
|
+
}
|
|
420
|
+
if (knownEntry.pendingPromise === pending) {
|
|
421
|
+
knownEntry.pendingPromise = null;
|
|
422
|
+
}
|
|
423
|
+
applyResolved(resolved, childrenRef.current);
|
|
424
|
+
},
|
|
425
|
+
() => {}
|
|
426
|
+
);
|
|
427
|
+
}
|
|
428
|
+
}
|
|
429
|
+
|
|
430
|
+
const latestCached = slotOverrideCache.get(id);
|
|
431
|
+
if (
|
|
432
|
+
latestCached &&
|
|
433
|
+
latestCached.baseChildren === childrenRef.current &&
|
|
434
|
+
latestCached.content !== null
|
|
435
|
+
) {
|
|
436
|
+
setOverride((prev) =>
|
|
437
|
+
prev.content === latestCached.content && prev.baseChildren === childrenRef.current
|
|
438
|
+
? prev
|
|
439
|
+
: { baseChildren: childrenRef.current, content: latestCached.content }
|
|
440
|
+
);
|
|
441
|
+
}
|
|
442
|
+
|
|
265
443
|
const handleRefresh = (e) => {
|
|
266
|
-
|
|
444
|
+
const targetId = e?.detail?.id;
|
|
445
|
+
const matchesTag = Array.isArray(tags) && targetId && tags.includes(targetId);
|
|
446
|
+
if (!e || !e.detail || targetId === id || targetId === "*" || matchesTag) {
|
|
267
447
|
if (
|
|
268
448
|
typeof window !== "undefined" &&
|
|
269
449
|
typeof window.__DINOU_FETCH_SLOT__ === "function"
|
|
270
450
|
) {
|
|
451
|
+
const curEntry = knownClientSlots.get(id);
|
|
271
452
|
const promise = window.__DINOU_FETCH_SLOT__(id, e.detail?.options);
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
453
|
+
const reqId = curEntry ? (curEntry.latestReqId || 0) + 1 : 0;
|
|
454
|
+
if (curEntry) {
|
|
455
|
+
curEntry.latestReqId = reqId;
|
|
456
|
+
}
|
|
457
|
+
if (promise && typeof promise.then === "function") {
|
|
458
|
+
if (curEntry) {
|
|
459
|
+
curEntry.pendingPromise = promise;
|
|
460
|
+
curEntry.pendingReqId = reqId;
|
|
279
461
|
}
|
|
462
|
+
promise.then(
|
|
463
|
+
(resolved) => {
|
|
464
|
+
if (curEntry && curEntry.latestReqId !== reqId) {
|
|
465
|
+
return;
|
|
466
|
+
}
|
|
467
|
+
if (curEntry && curEntry.pendingPromise === promise) {
|
|
468
|
+
curEntry.pendingPromise = null;
|
|
469
|
+
}
|
|
470
|
+
applyResolved(resolved, childrenRef.current);
|
|
471
|
+
},
|
|
472
|
+
(err) => {
|
|
473
|
+
if (curEntry && curEntry.pendingPromise === promise) {
|
|
474
|
+
curEntry.pendingPromise = null;
|
|
475
|
+
}
|
|
476
|
+
console.error(`[Dinou] Failed to refresh slot "${id}":`, err);
|
|
477
|
+
}
|
|
478
|
+
);
|
|
479
|
+
} else if (promise) {
|
|
480
|
+
if (curEntry) {
|
|
481
|
+
curEntry.pendingPromise = null;
|
|
482
|
+
}
|
|
483
|
+
applyResolved(promise, childrenRef.current);
|
|
280
484
|
}
|
|
281
485
|
}
|
|
282
486
|
}
|
|
283
487
|
};
|
|
284
488
|
window.addEventListener("dinou:refresh-slot", handleRefresh);
|
|
285
|
-
return () =>
|
|
286
|
-
|
|
489
|
+
return () => {
|
|
490
|
+
mountedClientSlots.delete(id);
|
|
491
|
+
const curKnown = knownClientSlots.get(id);
|
|
492
|
+
if (curKnown && curKnown.notifyMounted === applyResolved) {
|
|
493
|
+
curKnown.notifyMounted = null;
|
|
494
|
+
}
|
|
495
|
+
window.removeEventListener("dinou:refresh-slot", handleRefresh);
|
|
496
|
+
};
|
|
497
|
+
}, [id, tags]);
|
|
287
498
|
|
|
288
|
-
if (
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
499
|
+
if (isBrowser) {
|
|
500
|
+
const latestCached = slotOverrideCache.get(id);
|
|
501
|
+
if (
|
|
502
|
+
latestCached &&
|
|
503
|
+
latestCached.content !== null &&
|
|
504
|
+
latestCached.baseChildren === children
|
|
505
|
+
) {
|
|
506
|
+
return latestCached.content;
|
|
507
|
+
}
|
|
508
|
+
}
|
|
509
|
+
if (override.content !== null && override.baseChildren === children) {
|
|
510
|
+
return override.content;
|
|
298
511
|
}
|
|
299
512
|
return children;
|
|
300
513
|
}
|