dinou 9.1.0 → 9.3.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.
@@ -10,6 +10,8 @@ const cachedSlotStorage = new AsyncLocalStorage();
10
10
  const slotDiscoveryStorage = new AsyncLocalStorage();
11
11
  const runtimeStaticSuspenseStorage = new AsyncLocalStorage();
12
12
  const pageRootElements = new WeakSet();
13
+ const layoutRootElements = new WeakSet();
14
+ const layoutChildrenPlaceholders = new WeakSet();
13
15
 
14
16
  const NO_DYNAMIC_HOOK_KEY = Symbol.for("dinou.ppr.noDynamic.hook");
15
17
  export const SKIP_STATIC_SUSPENSE_SYMBOL = Symbol.for("dinou.ppr.skipStatic");
@@ -23,8 +25,38 @@ export function runInRuntimeStaticSuspense(callback) {
23
25
  );
24
26
  }
25
27
 
26
- export function runInRuntimeDynamicHole(callback) {
27
- return runtimeStaticSuspenseStorage.run({ active: false, depth: 0 }, callback);
28
+ export function runInRuntimeDynamicHole(callback, holeId = null) {
29
+ const prev = runtimeStaticSuspenseStorage.getStore();
30
+ const slotDepthAtSuspense =
31
+ prev && typeof prev.slotDepthAtSuspense === "number"
32
+ ? prev.slotDepthAtSuspense
33
+ : cachedSlotStorage.getStore()?.depth || 0;
34
+ return runtimeStaticSuspenseStorage.run(
35
+ {
36
+ active: false,
37
+ depth: 0,
38
+ holeId: holeId || prev?.holeId || null,
39
+ idCount: 0,
40
+ childHoleCount: 0,
41
+ slotDepthAtSuspense,
42
+ },
43
+ callback
44
+ );
45
+ }
46
+
47
+ export function getActiveRuntimeDynamicHoleStore() {
48
+ const store = runtimeStaticSuspenseStorage.getStore();
49
+ return store && store.holeId ? store : null;
50
+ }
51
+
52
+ export function isInsideRuntimeSuspenseOutsideSlot() {
53
+ const currentSlotDepth = cachedSlotStorage.getStore()?.depth || 0;
54
+ if (currentSlotDepth > 0) return false;
55
+ const holeStore = getActiveRuntimeDynamicHoleStore();
56
+ if (holeStore && (holeStore.slotDepthAtSuspense || 0) === 0) {
57
+ return true;
58
+ }
59
+ return isInsideOuterSuspenseOutsideSlot();
28
60
  }
29
61
 
30
62
  export function isRuntimeStaticSuspenseActive() {
@@ -67,9 +99,25 @@ export function isNoDynamicActive() {
67
99
  return Boolean(store && store.noDynamic);
68
100
  }
69
101
 
70
- export function runInCachedSlot(callback) {
102
+ export function runInCachedSlot(callback, options = {}) {
71
103
  const prev = cachedSlotStorage.getStore();
72
- return cachedSlotStorage.run({ depth: (prev?.depth || 0) + 1 }, callback);
104
+ const isCached = options?.cached !== false;
105
+ const slotId =
106
+ typeof options === "string"
107
+ ? options
108
+ : options?.slotId || prev?.slotId || null;
109
+ return cachedSlotStorage.run(
110
+ {
111
+ depth: (prev?.depth || 0) + (isCached ? 1 : 0),
112
+ slotId,
113
+ idCount: 0,
114
+ },
115
+ callback
116
+ );
117
+ }
118
+
119
+ export function getActiveCachedSlotStore() {
120
+ return cachedSlotStorage.getStore() || null;
73
121
  }
74
122
 
75
123
  export function isInsideCachedSlot() {
@@ -95,20 +143,68 @@ export function isPageRootElement(el) {
95
143
  return Boolean(el && typeof el === "object" && pageRootElements.has(el));
96
144
  }
97
145
 
98
- export function runInPageSegment(callback) {
99
- return pageSegmentStorage.run({ active: true }, callback);
146
+ export function markLayoutRootElement(el) {
147
+ if (el && typeof el === "object") {
148
+ layoutRootElements.add(el);
149
+ }
150
+ return el;
151
+ }
152
+
153
+ export function isLayoutRootElement(el) {
154
+ return Boolean(el && typeof el === "object" && layoutRootElements.has(el));
155
+ }
156
+
157
+ export function createLayoutChildrenPlaceholder() {
158
+ const placeholder = {
159
+ $$typeof: Symbol.for("react.transitional.element"),
160
+ type: Symbol.for("dinou.layout.children.placeholder"),
161
+ key: null,
162
+ ref: null,
163
+ props: {},
164
+ };
165
+ layoutChildrenPlaceholders.add(placeholder);
166
+ return placeholder;
167
+ }
168
+
169
+ export function isLayoutChildrenPlaceholder(el) {
170
+ return Boolean(
171
+ el &&
172
+ typeof el === "object" &&
173
+ (layoutChildrenPlaceholders.has(el) ||
174
+ el.type === Symbol.for("dinou.layout.children.placeholder"))
175
+ );
176
+ }
177
+
178
+ export function runInPageSegment(callback, storeOverride = null) {
179
+ const prev = pageSegmentStorage.getStore();
180
+ const store =
181
+ storeOverride || prev || { active: true, idCount: 0, holeCount: 0 };
182
+ return pageSegmentStorage.run(store, callback);
183
+ }
184
+
185
+ export function getActivePageSegmentStore() {
186
+ return pageSegmentStorage.getStore() || null;
100
187
  }
101
188
 
102
189
  export function isInsidePageSegment() {
103
190
  return Boolean(pageSegmentStorage.getStore()?.active);
104
191
  }
105
192
 
106
- export function runInLayoutSegment(callback) {
107
- return layoutSegmentStorage.run({ active: true }, callback);
193
+ export function runInLayoutSegment(callback, storeOverride = null) {
194
+ const prev = layoutSegmentStorage.getStore();
195
+ const store =
196
+ storeOverride || prev || { active: true, idCount: 0, holeCount: 0 };
197
+ return layoutSegmentStorage.run(store, callback);
198
+ }
199
+
200
+ export function getActiveLayoutSegmentStore() {
201
+ return layoutSegmentStorage.getStore() || null;
108
202
  }
109
203
 
110
204
  export function isInsideLayoutSegment() {
111
- return Boolean(layoutSegmentStorage.getStore()?.active);
205
+ return Boolean(
206
+ layoutSegmentStorage.getStore()?.active && !isInsidePageSegment()
207
+ );
112
208
  }
113
209
 
114
210
  /**
@@ -209,7 +305,29 @@ export function getPprHoleCounter() {
209
305
  export function runInSuspense(callback, explicitHoleId = null) {
210
306
  const store = getPprStore();
211
307
  if (!store) {
212
- return callback(null, null);
308
+ let holeId = explicitHoleId;
309
+ if (!holeId) {
310
+ const holeStore = getActiveRuntimeDynamicHoleStore();
311
+ const layoutStore = getActiveLayoutSegmentStore();
312
+ const pageStore = getActivePageSegmentStore();
313
+ if (holeStore && holeStore.holeId) {
314
+ holeStore.childHoleCount = (holeStore.childHoleCount || 0) + 1;
315
+ holeId = `${holeStore.holeId}-${holeStore.childHoleCount}`;
316
+ } else if (
317
+ layoutStore &&
318
+ layoutStore.active &&
319
+ !isInsidePageSegment()
320
+ ) {
321
+ layoutStore.holeCount = (layoutStore.holeCount || 0) + 1;
322
+ holeId = `layout-hole-${layoutStore.holeCount}`;
323
+ } else if (pageStore && pageStore.active) {
324
+ pageStore.holeCount = (pageStore.holeCount || 0) + 1;
325
+ holeId = `ppr-hole-${pageStore.holeCount}`;
326
+ } else {
327
+ holeId = "ppr-hole-1";
328
+ }
329
+ }
330
+ return callback(holeId, null);
213
331
  }
214
332
  let holeId = explicitHoleId;
215
333
  if (!holeId) {
@@ -359,6 +477,7 @@ export default {
359
477
  SKIP_STATIC_SUSPENSE_SYMBOL,
360
478
  runInRuntimeStaticSuspense,
361
479
  runInRuntimeDynamicHole,
480
+ getActiveRuntimeDynamicHoleStore,
362
481
  isRuntimeStaticSuspenseActive,
363
482
  resetComponentNoDynamicFlag,
364
483
  consumeComponentNoDynamicFlag,
@@ -367,8 +486,16 @@ export default {
367
486
  isInsideCachedSlot,
368
487
  runInSlotDiscovery,
369
488
  isSlotDiscoveryActive,
489
+ markPageRootElement,
490
+ isPageRootElement,
491
+ markLayoutRootElement,
492
+ isLayoutRootElement,
493
+ createLayoutChildrenPlaceholder,
494
+ isLayoutChildrenPlaceholder,
370
495
  runInPageSegment,
371
496
  isInsidePageSegment,
497
+ runInLayoutSegment,
498
+ isInsideLayoutSegment,
372
499
  runWithPprContext,
373
500
  getPprStore,
374
501
  isPprBuildActive,
@@ -9,12 +9,23 @@ import { renderRSCStream } from "./rsc-renderer.js";
9
9
  import { getClientManifest } from "./manifest-provider.js";
10
10
  import { requestStorage, setCurrentContext } from "./request-context.js";
11
11
  import getJSX from "./get-jsx.js";
12
+ import {
13
+ asyncRenderJSXToClientJSX,
14
+ invokeWithServerHooksDispatcher,
15
+ } from "./render-jsx-to-client-jsx.js";
12
16
  import {
13
17
  SKIP_STATIC_SUSPENSE_SYMBOL,
14
18
  runInRuntimeStaticSuspense,
15
19
  runInRuntimeDynamicHole,
16
20
  } from "./ppr-context.js";
17
21
 
22
+ async function PprDynamicHoleBoundary({ holeId, children }) {
23
+ return await runInRuntimeDynamicHole(
24
+ () => asyncRenderJSXToClientJSX(children),
25
+ holeId
26
+ );
27
+ }
28
+
18
29
  function escapeHtml(str) {
19
30
  return String(str)
20
31
  .replace(/&/g, "&")
@@ -114,7 +125,9 @@ async function spliceDynamicHolesIntoShell(
114
125
  }
115
126
  if (typeof current?.type === "function") {
116
127
  try {
117
- const evaluated = await current.type(current.props || {});
128
+ const evaluated = await invokeWithServerHooksDispatcher(() =>
129
+ current.type(current.props || {})
130
+ );
118
131
  await traverse(evaluated);
119
132
  } catch (err) {
120
133
  if (err && err.$$typeof === SKIP_STATIC_SUSPENSE_SYMBOL) {
@@ -134,7 +147,9 @@ async function spliceDynamicHolesIntoShell(
134
147
  return;
135
148
  }
136
149
  try {
137
- const rendered = await current.type.render(current.props || {}, current.ref);
150
+ const rendered = await invokeWithServerHooksDispatcher(() =>
151
+ current.type.render(current.props || {}, current.ref)
152
+ );
138
153
  await traverse(rendered);
139
154
  } catch (err) {
140
155
  if (err && err.$$typeof === SKIP_STATIC_SUSPENSE_SYMBOL) {
@@ -155,8 +170,21 @@ async function spliceDynamicHolesIntoShell(
155
170
  }
156
171
  try {
157
172
  const innerComp = current.type.type;
158
- const rendered =
159
- typeof innerComp === "function" ? await innerComp(current.props || {}) : null;
173
+ let rendered = null;
174
+ if (typeof innerComp === "function") {
175
+ rendered = await invokeWithServerHooksDispatcher(() =>
176
+ innerComp(current.props || {})
177
+ );
178
+ } else if (
179
+ typeof innerComp === "object" &&
180
+ innerComp !== null &&
181
+ innerComp.$$typeof === Symbol.for("react.forward_ref") &&
182
+ typeof innerComp.render === "function"
183
+ ) {
184
+ rendered = await invokeWithServerHooksDispatcher(() =>
185
+ innerComp.render(current.props || {}, current.ref)
186
+ );
187
+ }
160
188
  await traverse(rendered);
161
189
  } catch (err) {
162
190
  if (err && err.$$typeof === SKIP_STATIC_SUSPENSE_SYMBOL) {
@@ -173,7 +201,18 @@ async function spliceDynamicHolesIntoShell(
173
201
  holeIndex++;
174
202
  const currentHoleId = `ppr-hole-${holeIndex}`;
175
203
  if (knownHoleIds.has(currentHoleId)) {
176
- liveHolesMap.set(currentHoleId, current);
204
+ const wrappedHole = React.createElement(
205
+ React.Suspense,
206
+ {
207
+ ...(current.props || {}),
208
+ ...(current.key != null ? { key: current.key } : {}),
209
+ children: React.createElement(PprDynamicHoleBoundary, {
210
+ holeId: currentHoleId,
211
+ children: current.props?.children,
212
+ }),
213
+ }
214
+ );
215
+ liveHolesMap.set(currentHoleId, wrappedHole);
177
216
  return;
178
217
  }
179
218
  const runStaticScope = staticParentHoleIds.has(currentHoleId)
@@ -320,7 +359,9 @@ async function streamDynamicHoles(
320
359
  // Handle React Server Component function
321
360
  if (typeof current?.type === "function") {
322
361
  try {
323
- const evaluated = await current.type(current.props || {});
362
+ const evaluated = await invokeWithServerHooksDispatcher(() =>
363
+ current.type(current.props || {})
364
+ );
324
365
  await traverse(evaluated);
325
366
  } catch (err) {
326
367
  if (err && err.$$typeof === SKIP_STATIC_SUSPENSE_SYMBOL) {
@@ -342,7 +383,9 @@ async function streamDynamicHoles(
342
383
  return;
343
384
  }
344
385
  try {
345
- const rendered = await current.type.render(current.props || {}, current.ref);
386
+ const rendered = await invokeWithServerHooksDispatcher(() =>
387
+ current.type.render(current.props || {}, current.ref)
388
+ );
346
389
  await traverse(rendered);
347
390
  } catch (err) {
348
391
  if (err && err.$$typeof === SKIP_STATIC_SUSPENSE_SYMBOL) {
@@ -365,8 +408,21 @@ async function streamDynamicHoles(
365
408
  }
366
409
  try {
367
410
  const innerComp = current.type.type;
368
- const rendered =
369
- typeof innerComp === "function" ? await innerComp(current.props || {}) : null;
411
+ let rendered = null;
412
+ if (typeof innerComp === "function") {
413
+ rendered = await invokeWithServerHooksDispatcher(() =>
414
+ innerComp(current.props || {})
415
+ );
416
+ } else if (
417
+ typeof innerComp === "object" &&
418
+ innerComp !== null &&
419
+ innerComp.$$typeof === Symbol.for("react.forward_ref") &&
420
+ typeof innerComp.render === "function"
421
+ ) {
422
+ rendered = await invokeWithServerHooksDispatcher(() =>
423
+ innerComp.render(current.props || {}, current.ref)
424
+ );
425
+ }
370
426
  await traverse(rendered);
371
427
  } catch (err) {
372
428
  if (err && err.$$typeof === SKIP_STATIC_SUSPENSE_SYMBOL) {
@@ -397,7 +453,8 @@ async function streamDynamicHoles(
397
453
  try {
398
454
  let html = "";
399
455
  if (platformContext && platformContext.renderHtmlStream) {
400
- const rscStream = renderRSCStream(children, clientManifest, { runtime: "edge" });
456
+ const resolvedChildren = await asyncRenderJSXToClientJSX(children);
457
+ const rscStream = renderRSCStream(resolvedChildren, clientManifest, { runtime: "edge" });
401
458
  const htmlStream = await platformContext.renderHtmlStream(rscStream, {
402
459
  bootstrapModules: [],
403
460
  });
@@ -415,7 +472,7 @@ async function streamDynamicHoles(
415
472
  const errChunk = `\n<template id="ppr-content-${currentHoleId}"><div style="color:#e11d48;padding:8px 12px;background:#fff1f2;border:1px solid #fecdd3;border-radius:6px;font-size:0.875rem;font-family:system-ui,sans-serif;">[PPR Error: ${errMsg}]</div></template>\n<script>(function(){var c=document.getElementById("ppr-content-${currentHoleId}");if(!c)return;var b=document.getElementById("B:${currentBoundaryIndex}");var t=document.querySelector('[data-ppr-hole="${currentHoleId}"]')||(b?b.nextElementSibling:null);try{if(t){if(c.content){t.replaceWith(c.content);}else{t.replaceWith.apply(t,Array.from(c.childNodes));}}}finally{c.remove();}})();</script>\n`;
416
473
  await writer.write(encoder.encode(errChunk));
417
474
  }
418
- })());
475
+ })(), currentHoleId);
419
476
  holePromises.push(promise);
420
477
  return;
421
478
  }