dinou 9.0.3 → 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.
@@ -0,0 +1,25 @@
1
+ // dinou/core/no-dynamic.js
2
+ // Opt-out primitive for Static <Suspense> boundaries in Partial Prerendering (PPR).
3
+
4
+ const NO_DYNAMIC_HOOK_KEY = Symbol.for("dinou.ppr.noDynamic.hook");
5
+
6
+ /**
7
+ * Opts the enclosing <Suspense> boundary out of becoming a Dynamic PPR Hole
8
+ * when wrapping an async Server Component on a PPR route (`ppr = true`).
9
+ *
10
+ * Call `noDynamic()` synchronously at the top of your async Server Component
11
+ * (before the first `await`) so Dinou resolves its promise at build time and
12
+ * bakes the rendered output directly into the Static Shell (`shell.html`).
13
+ */
14
+ export function noDynamic() {
15
+ if (typeof window !== "undefined") {
16
+ return;
17
+ }
18
+ const hook = globalThis[NO_DYNAMIC_HOOK_KEY];
19
+ if (typeof hook === "function") {
20
+ hook();
21
+ }
22
+ }
23
+
24
+ export default noDynamic;
25
+
@@ -1,256 +1,388 @@
1
- // dinou/core/ppr-context.js
2
- // PPR Pre-rendering Context and Suspense Hole Tracker for Dinou.
3
-
4
- import { AsyncLocalStorage } from "node:async_hooks";
5
-
6
- const pprStorage = new AsyncLocalStorage();
7
- const pageSegmentStorage = new AsyncLocalStorage();
8
- const layoutSegmentStorage = new AsyncLocalStorage();
9
- const pageRootElements = new WeakSet();
10
-
11
- export function markPageRootElement(el) {
12
- if (el && typeof el === "object") {
13
- pageRootElements.add(el);
14
- }
15
- return el;
16
- }
17
-
18
- export function isPageRootElement(el) {
19
- return Boolean(el && typeof el === "object" && pageRootElements.has(el));
20
- }
21
-
22
- export function runInPageSegment(callback) {
23
- return pageSegmentStorage.run({ active: true }, callback);
24
- }
25
-
26
- export function isInsidePageSegment() {
27
- return Boolean(pageSegmentStorage.getStore()?.active);
28
- }
29
-
30
- export function runInLayoutSegment(callback) {
31
- return layoutSegmentStorage.run({ active: true }, callback);
32
- }
33
-
34
- export function isInsideLayoutSegment() {
35
- return Boolean(layoutSegmentStorage.getStore()?.active);
36
- }
37
-
38
- /**
39
- * Executes a function within a PPR pre-render context.
40
- * @param {object} options
41
- * @param {Function} callback
42
- * @returns {Promise<any>}
43
- */
44
- export function runWithPprContext(options, callback) {
45
- const sharedCounter = { nextHoleIndex: 0 };
46
- const sharedHoles = new Map();
47
- const store = {
48
- isPpr: Boolean(options?.isPpr),
49
- suspenseDepth: 0,
50
- currentHoleId: null,
51
- holeStack: [],
52
- holes: sharedHoles,
53
- counter: sharedCounter,
54
- get nextHoleIndex() {
55
- return sharedCounter.nextHoleIndex;
56
- },
57
- set nextHoleIndex(val) {
58
- sharedCounter.nextHoleIndex = val;
59
- },
60
- ...options,
61
- };
62
- return pprStorage.run(store, callback);
63
- }
64
-
65
- export function getPprStore() {
66
- return pprStorage.getStore() || null;
67
- }
68
-
69
- export function isPprBuildActive() {
70
- const store = getPprStore();
71
- return Boolean(store && store.isPpr);
72
- }
73
-
74
- export function isInsideSuspense() {
75
- const store = getPprStore();
76
- return Boolean(store && store.suspenseDepth > 0);
77
- }
78
-
79
- export function resetPprHoles(startIndex = 0) {
80
- const store = getPprStore();
81
- if (store) {
82
- const startVal = Number(startIndex) || 0;
83
- if (store.counter) {
84
- store.counter.nextHoleIndex = startVal;
85
- } else {
86
- store.nextHoleIndex = startVal;
87
- }
88
- if (store.holes && typeof store.holes.clear === "function") {
89
- store.holes.clear();
90
- }
91
- store.suspenseDepth = 0;
92
- store.currentHoleId = null;
93
- if (Array.isArray(store.holeStack)) {
94
- store.holeStack.length = 0;
95
- }
96
- }
97
- }
98
-
99
- export function getPprHoleCounter() {
100
- const store = getPprStore();
101
- if (store) {
102
- return store.counter ? store.counter.nextHoleIndex : store.nextHoleIndex || 0;
103
- }
104
- return 0;
105
- }
106
-
107
- export function runInSuspense(callback, explicitHoleId = null) {
108
- const store = getPprStore();
109
- if (!store) {
110
- return callback(null, null);
111
- }
112
- let holeId = explicitHoleId;
113
- if (!holeId) {
114
- if (store.counter) {
115
- store.counter.nextHoleIndex++;
116
- holeId = `ppr-hole-${store.counter.nextHoleIndex}`;
117
- } else {
118
- store.nextHoleIndex++;
119
- holeId = `ppr-hole-${store.nextHoleIndex}`;
120
- }
121
- }
122
- const nextStack = Array.isArray(store.holeStack)
123
- ? [...store.holeStack, holeId]
124
- : [holeId];
125
- const pendingEffects = [];
126
- const childStore = {
127
- ...store,
128
- suspenseDepth: store.suspenseDepth + 1,
129
- currentHoleId: holeId,
130
- holeStack: nextStack,
131
- pendingEffects,
132
- parentPendingEffects: Array.isArray(store.pendingEffects)
133
- ? store.pendingEffects
134
- : null,
135
- };
136
- return pprStorage.run(childStore, () => callback(holeId, childStore));
137
- }
138
-
139
- export function deferPprSuspenseEffect(effectFn) {
140
- const store = getPprStore();
141
- if (
142
- store &&
143
- store.isPpr &&
144
- store.suspenseDepth > 0 &&
145
- Array.isArray(store.pendingEffects) &&
146
- typeof effectFn === "function"
147
- ) {
148
- store.pendingEffects.push(effectFn);
149
- return true;
150
- }
151
- return false;
152
- }
153
-
154
- export async function commitPprSuspenseEffects(childStore) {
155
- if (!childStore || !Array.isArray(childStore.pendingEffects)) return;
156
- const effects = childStore.pendingEffects.splice(0);
157
- if (effects.length === 0) return;
158
- if (Array.isArray(childStore.parentPendingEffects)) {
159
- childStore.parentPendingEffects.push(...effects);
160
- return;
161
- }
162
- for (const fn of effects) {
163
- await fn();
164
- }
165
- }
166
-
167
- export function enterSuspense(holeId = null) {
168
- const store = getPprStore();
169
- if (store) {
170
- store.suspenseDepth++;
171
- if (!holeId) {
172
- if (store.counter) {
173
- store.counter.nextHoleIndex++;
174
- holeId = `ppr-hole-${store.counter.nextHoleIndex}`;
175
- } else {
176
- store.nextHoleIndex++;
177
- holeId = `ppr-hole-${store.nextHoleIndex}`;
178
- }
179
- }
180
- if (Array.isArray(store.holeStack)) {
181
- store.holeStack.push(holeId);
182
- }
183
- store.currentHoleId = holeId;
184
- return holeId;
185
- }
186
- return null;
187
- }
188
-
189
- export function exitSuspense() {
190
- const store = getPprStore();
191
- if (store) {
192
- store.suspenseDepth = Math.max(0, store.suspenseDepth - 1);
193
- if (Array.isArray(store.holeStack) && store.holeStack.length > 0) {
194
- store.holeStack.pop();
195
- store.currentHoleId =
196
- store.holeStack.length > 0
197
- ? store.holeStack[store.holeStack.length - 1]
198
- : null;
199
- } else if (store.suspenseDepth === 0) {
200
- store.currentHoleId = null;
201
- }
202
- }
203
- }
204
-
205
- export function markDynamicAccess(type = "dynamic") {
206
- const store = getPprStore();
207
- if (store && store.isPpr && store.suspenseDepth > 0) {
208
- const idx = store.counter ? store.counter.nextHoleIndex : store.nextHoleIndex;
209
- const holeId = store.currentHoleId || `ppr-hole-${idx}`;
210
- const error = new Error(
211
- `[Dinou PPR] Dynamic access to '${type}' postponed inside Suspense`
212
- );
213
- error.$$typeof = Symbol.for("dinou.ppr.postpone");
214
- error.pprType = type;
215
- error.holeId = holeId;
216
- throw error;
217
- }
218
- }
219
-
220
- export function registerHole(holeId, meta = {}) {
221
- const store = getPprStore();
222
- if (store && holeId) {
223
- store.holes.set(holeId, {
224
- id: holeId,
225
- registeredAt: Date.now(),
226
- ...meta,
227
- });
228
- }
229
- }
230
-
231
- export function getRegisteredHoles() {
232
- const store = getPprStore();
233
- if (store) {
234
- return Array.from(store.holes.keys());
235
- }
236
- return [];
237
- }
238
-
239
- export default {
240
- runInPageSegment,
241
- isInsidePageSegment,
242
- runWithPprContext,
243
- getPprStore,
244
- isPprBuildActive,
245
- isInsideSuspense,
246
- resetPprHoles,
247
- getPprHoleCounter,
248
- runInSuspense,
249
- deferPprSuspenseEffect,
250
- commitPprSuspenseEffects,
251
- enterSuspense,
252
- exitSuspense,
253
- markDynamicAccess,
254
- registerHole,
255
- getRegisteredHoles,
256
- };
1
+ // dinou/core/ppr-context.js
2
+ // PPR Pre-rendering Context and Suspense Hole Tracker for Dinou.
3
+
4
+ import { AsyncLocalStorage } from "node:async_hooks";
5
+
6
+ const pprStorage = new AsyncLocalStorage();
7
+ const pageSegmentStorage = new AsyncLocalStorage();
8
+ const layoutSegmentStorage = new AsyncLocalStorage();
9
+ const cachedSlotStorage = new AsyncLocalStorage();
10
+ const slotDiscoveryStorage = new AsyncLocalStorage();
11
+ const runtimeStaticSuspenseStorage = new AsyncLocalStorage();
12
+ const pageRootElements = new WeakSet();
13
+
14
+ const NO_DYNAMIC_HOOK_KEY = Symbol.for("dinou.ppr.noDynamic.hook");
15
+ export const SKIP_STATIC_SUSPENSE_SYMBOL = Symbol.for("dinou.ppr.skipStatic");
16
+ let currentComponentNoDynamic = false;
17
+
18
+ export function runInRuntimeStaticSuspense(callback) {
19
+ const prev = runtimeStaticSuspenseStorage.getStore();
20
+ return runtimeStaticSuspenseStorage.run(
21
+ { active: true, depth: (prev?.depth || 0) + 1 },
22
+ callback
23
+ );
24
+ }
25
+
26
+ export function runInRuntimeDynamicHole(callback) {
27
+ return runtimeStaticSuspenseStorage.run({ active: false, depth: 0 }, callback);
28
+ }
29
+
30
+ export function isRuntimeStaticSuspenseActive() {
31
+ const store = runtimeStaticSuspenseStorage.getStore();
32
+ return Boolean(store && store.active && store.depth > 0);
33
+ }
34
+
35
+ export function noDynamic() {
36
+ if (isRuntimeStaticSuspenseActive() && !isPprBuildActive()) {
37
+ const err = new Error(
38
+ "[Dinou PPR] Skipping static noDynamic() subtree during runtime hole traversal"
39
+ );
40
+ err.$$typeof = SKIP_STATIC_SUSPENSE_SYMBOL;
41
+ throw err;
42
+ }
43
+ currentComponentNoDynamic = true;
44
+ const store = pprStorage.getStore();
45
+ if (store && store.suspenseDepth > 0) {
46
+ store.noDynamic = true;
47
+ }
48
+ }
49
+
50
+ if (typeof globalThis !== "undefined") {
51
+ globalThis[NO_DYNAMIC_HOOK_KEY] = noDynamic;
52
+ }
53
+
54
+ export function resetComponentNoDynamicFlag() {
55
+ currentComponentNoDynamic = false;
56
+ }
57
+
58
+ export function consumeComponentNoDynamicFlag() {
59
+ const val = currentComponentNoDynamic;
60
+ currentComponentNoDynamic = false;
61
+ return val;
62
+ }
63
+
64
+ export function isNoDynamicActive() {
65
+ if (currentComponentNoDynamic) return true;
66
+ const store = pprStorage.getStore();
67
+ return Boolean(store && store.noDynamic);
68
+ }
69
+
70
+ export function runInCachedSlot(callback) {
71
+ const prev = cachedSlotStorage.getStore();
72
+ return cachedSlotStorage.run({ depth: (prev?.depth || 0) + 1 }, callback);
73
+ }
74
+
75
+ export function isInsideCachedSlot() {
76
+ return Boolean((cachedSlotStorage.getStore()?.depth || 0) > 0);
77
+ }
78
+
79
+ export function runInSlotDiscovery(callback) {
80
+ return slotDiscoveryStorage.run({ active: true }, callback);
81
+ }
82
+
83
+ export function isSlotDiscoveryActive() {
84
+ return Boolean(slotDiscoveryStorage.getStore()?.active);
85
+ }
86
+
87
+ export function markPageRootElement(el) {
88
+ if (el && typeof el === "object") {
89
+ pageRootElements.add(el);
90
+ }
91
+ return el;
92
+ }
93
+
94
+ export function isPageRootElement(el) {
95
+ return Boolean(el && typeof el === "object" && pageRootElements.has(el));
96
+ }
97
+
98
+ export function runInPageSegment(callback) {
99
+ return pageSegmentStorage.run({ active: true }, callback);
100
+ }
101
+
102
+ export function isInsidePageSegment() {
103
+ return Boolean(pageSegmentStorage.getStore()?.active);
104
+ }
105
+
106
+ export function runInLayoutSegment(callback) {
107
+ return layoutSegmentStorage.run({ active: true }, callback);
108
+ }
109
+
110
+ export function isInsideLayoutSegment() {
111
+ return Boolean(layoutSegmentStorage.getStore()?.active);
112
+ }
113
+
114
+ /**
115
+ * Executes a function within a PPR pre-render context.
116
+ * @param {object} options
117
+ * @param {Function} callback
118
+ * @returns {Promise<any>}
119
+ */
120
+ export function runWithPprContext(options, callback) {
121
+ const sharedCounter = { nextHoleIndex: 0 };
122
+ const sharedHoles = new Map();
123
+ const store = {
124
+ isPpr: Boolean(options?.isPpr),
125
+ suspenseDepth: 0,
126
+ currentHoleId: null,
127
+ holeStack: [],
128
+ holes: sharedHoles,
129
+ counter: sharedCounter,
130
+ get nextHoleIndex() {
131
+ return sharedCounter.nextHoleIndex;
132
+ },
133
+ set nextHoleIndex(val) {
134
+ sharedCounter.nextHoleIndex = val;
135
+ },
136
+ ...options,
137
+ };
138
+ return pprStorage.run(store, callback);
139
+ }
140
+
141
+ export function getPprStore() {
142
+ return pprStorage.getStore() || null;
143
+ }
144
+
145
+ export function isPprBuildActive() {
146
+ const store = getPprStore();
147
+ return Boolean(store && store.isPpr);
148
+ }
149
+
150
+ export function isInsideSuspense() {
151
+ const store = getPprStore();
152
+ return Boolean(store && store.suspenseDepth > 0);
153
+ }
154
+
155
+ export function isInsideOuterSuspenseOutsideSlot() {
156
+ const store = getPprStore();
157
+ return Boolean(
158
+ store && store.suspenseDepth > 0 && store.hasOuterSuspenseOutsideSlot
159
+ );
160
+ }
161
+
162
+ export function rollbackPprHolesAfter(countBefore) {
163
+ const store = getPprStore();
164
+ if (!store) return;
165
+ const target = Number(countBefore) || 0;
166
+ const current = store.counter
167
+ ? store.counter.nextHoleIndex
168
+ : store.nextHoleIndex || 0;
169
+ if (current > target) {
170
+ for (let i = target + 1; i <= current; i++) {
171
+ store.holes.delete(`ppr-hole-${i}`);
172
+ }
173
+ if (store.counter) {
174
+ store.counter.nextHoleIndex = target;
175
+ } else {
176
+ store.nextHoleIndex = target;
177
+ }
178
+ }
179
+ }
180
+
181
+ export function resetPprHoles(startIndex = 0) {
182
+ const store = getPprStore();
183
+ if (store) {
184
+ const startVal = Number(startIndex) || 0;
185
+ if (store.counter) {
186
+ store.counter.nextHoleIndex = startVal;
187
+ } else {
188
+ store.nextHoleIndex = startVal;
189
+ }
190
+ if (store.holes && typeof store.holes.clear === "function") {
191
+ store.holes.clear();
192
+ }
193
+ store.suspenseDepth = 0;
194
+ store.currentHoleId = null;
195
+ if (Array.isArray(store.holeStack)) {
196
+ store.holeStack.length = 0;
197
+ }
198
+ }
199
+ }
200
+
201
+ export function getPprHoleCounter() {
202
+ const store = getPprStore();
203
+ if (store) {
204
+ return store.counter ? store.counter.nextHoleIndex : store.nextHoleIndex || 0;
205
+ }
206
+ return 0;
207
+ }
208
+
209
+ export function runInSuspense(callback, explicitHoleId = null) {
210
+ const store = getPprStore();
211
+ if (!store) {
212
+ return callback(null, null);
213
+ }
214
+ let holeId = explicitHoleId;
215
+ if (!holeId) {
216
+ if (store.counter) {
217
+ store.counter.nextHoleIndex++;
218
+ holeId = `ppr-hole-${store.counter.nextHoleIndex}`;
219
+ } else {
220
+ store.nextHoleIndex++;
221
+ holeId = `ppr-hole-${store.nextHoleIndex}`;
222
+ }
223
+ }
224
+ const nextStack = Array.isArray(store.holeStack)
225
+ ? [...store.holeStack, holeId]
226
+ : [holeId];
227
+ const pendingEffects = [];
228
+ const slotDepthAtSuspense = cachedSlotStorage.getStore()?.depth || 0;
229
+ const hasOuterSuspenseOutsideSlot = Boolean(
230
+ store.hasOuterSuspenseOutsideSlot || slotDepthAtSuspense === 0
231
+ );
232
+ const childStore = {
233
+ ...store,
234
+ suspenseDepth: store.suspenseDepth + 1,
235
+ currentHoleId: holeId,
236
+ holeStack: nextStack,
237
+ noDynamic: false,
238
+ settled: false,
239
+ slotDepthAtSuspense,
240
+ hasOuterSuspenseOutsideSlot,
241
+ pendingEffects,
242
+ parentPendingEffects: Array.isArray(store.pendingEffects)
243
+ ? store.pendingEffects
244
+ : null,
245
+ };
246
+ return pprStorage.run(childStore, () => callback(holeId, childStore));
247
+ }
248
+
249
+ export function deferPprSuspenseEffect(effectFn) {
250
+ const store = getPprStore();
251
+ if (
252
+ store &&
253
+ store.isPpr &&
254
+ store.suspenseDepth > 0 &&
255
+ Array.isArray(store.pendingEffects) &&
256
+ typeof effectFn === "function"
257
+ ) {
258
+ if (store.settled) {
259
+ return true;
260
+ }
261
+ store.pendingEffects.push(effectFn);
262
+ return true;
263
+ }
264
+ return false;
265
+ }
266
+
267
+ export async function commitPprSuspenseEffects(childStore) {
268
+ if (!childStore || !Array.isArray(childStore.pendingEffects)) return;
269
+ const effects = childStore.pendingEffects.splice(0);
270
+ if (effects.length === 0) return;
271
+ if (Array.isArray(childStore.parentPendingEffects)) {
272
+ childStore.parentPendingEffects.push(...effects);
273
+ return;
274
+ }
275
+ for (const fn of effects) {
276
+ await fn();
277
+ }
278
+ }
279
+
280
+ export function enterSuspense(holeId = null) {
281
+ const store = getPprStore();
282
+ if (store) {
283
+ store.suspenseDepth++;
284
+ if (!holeId) {
285
+ if (store.counter) {
286
+ store.counter.nextHoleIndex++;
287
+ holeId = `ppr-hole-${store.counter.nextHoleIndex}`;
288
+ } else {
289
+ store.nextHoleIndex++;
290
+ holeId = `ppr-hole-${store.nextHoleIndex}`;
291
+ }
292
+ }
293
+ if (Array.isArray(store.holeStack)) {
294
+ store.holeStack.push(holeId);
295
+ }
296
+ store.currentHoleId = holeId;
297
+ return holeId;
298
+ }
299
+ return null;
300
+ }
301
+
302
+ export function exitSuspense() {
303
+ const store = getPprStore();
304
+ if (store) {
305
+ store.suspenseDepth = Math.max(0, store.suspenseDepth - 1);
306
+ if (Array.isArray(store.holeStack) && store.holeStack.length > 0) {
307
+ store.holeStack.pop();
308
+ store.currentHoleId =
309
+ store.holeStack.length > 0
310
+ ? store.holeStack[store.holeStack.length - 1]
311
+ : null;
312
+ } else if (store.suspenseDepth === 0) {
313
+ store.currentHoleId = null;
314
+ }
315
+ }
316
+ }
317
+
318
+ export function markDynamicAccess(type = "dynamic") {
319
+ const store = getPprStore();
320
+ if (
321
+ store &&
322
+ store.isPpr &&
323
+ store.suspenseDepth > 0 &&
324
+ store.hasOuterSuspenseOutsideSlot
325
+ ) {
326
+ const idx = store.counter ? store.counter.nextHoleIndex : store.nextHoleIndex;
327
+ const holeId = store.currentHoleId || `ppr-hole-${idx}`;
328
+ const error = new Error(
329
+ `[Dinou PPR] Dynamic access to '${type}' postponed inside Suspense`
330
+ );
331
+ error.$$typeof = Symbol.for("dinou.ppr.postpone");
332
+ error.pprType = type;
333
+ error.holeId = holeId;
334
+ throw error;
335
+ }
336
+ }
337
+
338
+ export function registerHole(holeId, meta = {}) {
339
+ const store = getPprStore();
340
+ if (store && holeId) {
341
+ store.holes.set(holeId, {
342
+ id: holeId,
343
+ registeredAt: Date.now(),
344
+ ...meta,
345
+ });
346
+ }
347
+ }
348
+
349
+ export function getRegisteredHoles() {
350
+ const store = getPprStore();
351
+ if (store) {
352
+ return Array.from(store.holes.keys());
353
+ }
354
+ return [];
355
+ }
356
+
357
+ export default {
358
+ noDynamic,
359
+ SKIP_STATIC_SUSPENSE_SYMBOL,
360
+ runInRuntimeStaticSuspense,
361
+ runInRuntimeDynamicHole,
362
+ isRuntimeStaticSuspenseActive,
363
+ resetComponentNoDynamicFlag,
364
+ consumeComponentNoDynamicFlag,
365
+ isNoDynamicActive,
366
+ runInCachedSlot,
367
+ isInsideCachedSlot,
368
+ runInSlotDiscovery,
369
+ isSlotDiscoveryActive,
370
+ runInPageSegment,
371
+ isInsidePageSegment,
372
+ runWithPprContext,
373
+ getPprStore,
374
+ isPprBuildActive,
375
+ isInsideSuspense,
376
+ isInsideOuterSuspenseOutsideSlot,
377
+ rollbackPprHolesAfter,
378
+ resetPprHoles,
379
+ getPprHoleCounter,
380
+ runInSuspense,
381
+ deferPprSuspenseEffect,
382
+ commitPprSuspenseEffects,
383
+ enterSuspense,
384
+ exitSuspense,
385
+ markDynamicAccess,
386
+ registerHole,
387
+ getRegisteredHoles,
388
+ };