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.
Files changed (41) hide show
  1. package/CHANGELOG.md +65 -0
  2. package/README.md +379 -9742
  3. package/dinou/bun/build.js +38 -1
  4. package/{cli.js → dinou/cli.js} +8 -7
  5. package/dinou/cloudflare/build.js +37 -0
  6. package/dinou/core/bailout-proxy.js +2 -2
  7. package/dinou/core/build-static-pages.js +17 -7
  8. package/dinou/core/cache-revalidate.js +977 -152
  9. package/dinou/core/cache-slot.js +848 -103
  10. package/dinou/core/client-webpack.jsx +60 -9
  11. package/dinou/core/client.jsx +60 -9
  12. package/dinou/core/edge-ssr.js +33 -5
  13. package/dinou/core/generate-static.js +109 -47
  14. package/dinou/core/get-jsx.js +35 -27
  15. package/dinou/core/handler.js +786 -54
  16. package/dinou/core/layout-functions.js +102 -1
  17. package/dinou/core/no-dynamic.js +25 -0
  18. package/dinou/core/ppr-context.js +293 -10
  19. package/dinou/core/ppr-runtime.js +373 -37
  20. package/dinou/core/render-jsx-to-client-jsx.js +205 -74
  21. package/dinou/core/slot.js +246 -33
  22. package/dinou/core/storage-adapter.js +117 -11
  23. package/dinou/deno/build.js +38 -1
  24. package/dinou/deno/seed-kv.js +43 -11
  25. package/dinou/esbuild/helpers-esbuild/swc-disk-cache.js +242 -225
  26. package/dinou/esbuild/plugins-esbuild/server-functions-plugin.js +253 -226
  27. package/dinou/esbuild/react-refresh/esm-hmr/client.js +209 -209
  28. package/dinou/index.d.ts +10 -0
  29. package/dinou/index.js +3 -0
  30. package/dinou/node/build.js +2 -2
  31. package/dinou/node/bundle-dual-engine.js +2 -0
  32. package/dinou/node/dev.js +2853 -2704
  33. package/dinou/package.json +4 -1
  34. package/dinou/rollup/react-refresh/esm-hmr/client.js +220 -220
  35. package/dinou/rollup/rollup-plugins/rollup-plugin-server-functions.js +182 -160
  36. package/dinou/server.d.ts +65 -12
  37. package/dinou/server.js +11 -0
  38. package/dinou/webpack/loaders/server-functions-loader.js +101 -94
  39. package/dinou/webpack/webpack.config.js +9 -4
  40. package/eject.js +219 -223
  41. package/package.json +2 -2
@@ -2,6 +2,7 @@ import path from "node:path";
2
2
  import { existsSync } from "./vfs.js";
3
3
  import importModule from "./import-module.js";
4
4
  import { resolveModulePpr } from "./resolve-module-ppr.js";
5
+ import { getFilePathAndDynamicParams } from "./get-file-path-and-dynamic-params.js";
5
6
 
6
7
  const LAYOUT_FUNCTIONS_EXTENSIONS = [".tsx", ".ts", ".jsx", ".js", ".mjs"];
7
8
  const LAYOUT_FUNCTIONS_NAMES = ["layout_functions", "layout.functions"];
@@ -175,14 +176,114 @@ async function resolveLayoutFunctionsConfig(layoutPath, dParams = {}) {
175
176
  }
176
177
  }
177
178
 
179
+ /**
180
+ * Returns the active layout chain for a route from innermost (leaf) to outermost (root),
181
+ * honoring `no_layout` and stopping at `reset_layout`.
182
+ */
183
+ function getActiveLayoutChain(
184
+ reqSegments,
185
+ queryObj = {},
186
+ srcFolder = path.resolve(process.cwd(), "src"),
187
+ options = {}
188
+ ) {
189
+ if (
190
+ !options.ignoreNoLayout &&
191
+ getFilePathAndDynamicParams(reqSegments, queryObj, srcFolder, "no_layout", false)[0]
192
+ ) {
193
+ return [];
194
+ }
195
+
196
+ const allLayouts = getFilePathAndDynamicParams(
197
+ reqSegments,
198
+ queryObj,
199
+ srcFolder,
200
+ "layout",
201
+ true,
202
+ false,
203
+ undefined,
204
+ 0,
205
+ {},
206
+ true
207
+ );
208
+
209
+ if (!allLayouts || !Array.isArray(allLayouts) || allLayouts.length === 0) {
210
+ return [];
211
+ }
212
+
213
+ const normSrc = srcFolder.replace(/\\/g, "/").replace(/\/+$/, "");
214
+ const activeChain = [];
215
+
216
+ for (const [layoutPath, dParams, slots] of [...allLayouts].reverse()) {
217
+ const layoutFolderPath = path.dirname(layoutPath);
218
+ const resetLayoutPath = getFilePathAndDynamicParams(
219
+ [],
220
+ {},
221
+ layoutFolderPath,
222
+ "reset_layout",
223
+ false
224
+ )[0];
225
+
226
+ const normFolder = layoutFolderPath.replace(/\\/g, "/").replace(/\/+$/, "");
227
+ let relFromSrc = "";
228
+ if (normFolder === normSrc) {
229
+ relFromSrc = "";
230
+ } else if (normFolder.startsWith(normSrc + "/")) {
231
+ relFromSrc = normFolder.slice(normSrc.length + 1);
232
+ } else {
233
+ const srcIdx = normFolder.lastIndexOf("/src/");
234
+ if (srcIdx !== -1) {
235
+ relFromSrc = normFolder.slice(srcIdx + 5);
236
+ } else if (normFolder.endsWith("/src")) {
237
+ relFromSrc = "";
238
+ }
239
+ }
240
+
241
+ const rawFolderSegs = relFromSrc.split("/").filter(Boolean);
242
+ let urlSegCount = 0;
243
+ for (const seg of rawFolderSegs) {
244
+ if (seg.startsWith("(") && seg.endsWith(")")) continue;
245
+ if (seg.startsWith("[[...") && seg.endsWith("]]")) {
246
+ const pName = seg.slice(5, -2);
247
+ const val = dParams?.[pName];
248
+ urlSegCount += Array.isArray(val) ? val.length : val ? 1 : 0;
249
+ } else if (seg.startsWith("[...") && seg.endsWith("]")) {
250
+ const pName = seg.slice(4, -1);
251
+ const val = dParams?.[pName];
252
+ urlSegCount += Array.isArray(val) ? val.length : val ? 1 : 0;
253
+ } else {
254
+ urlSegCount += 1;
255
+ }
256
+ }
257
+
258
+ const cleanLayoutKey = reqSegments.slice(0, urlSegCount).join("/");
259
+ const layoutRoutePath = cleanLayoutKey ? "/" + cleanLayoutKey : "/";
260
+
261
+ activeChain.push({
262
+ layoutPath,
263
+ dParams: dParams || {},
264
+ slots: slots || {},
265
+ cleanLayoutKey,
266
+ layoutRoutePath,
267
+ hasResetLayout: Boolean(resetLayoutPath),
268
+ });
269
+
270
+ if (resetLayoutPath) {
271
+ break;
272
+ }
273
+ }
274
+
275
+ return activeChain;
276
+ }
277
+
178
278
  export {
179
279
  getLayoutFunctionsPath,
180
280
  getLayoutProps,
181
281
  resolveLayoutFunctionsConfig,
282
+ getActiveLayoutChain,
182
283
  };
183
284
  export default {
184
285
  getLayoutFunctionsPath,
185
286
  getLayoutProps,
186
287
  resolveLayoutFunctionsConfig,
288
+ getActiveLayoutChain,
187
289
  };
188
-
@@ -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,9 +1,115 @@
1
1
  // dinou/core/ppr-context.js
2
- // PPR Pre-rendering Context and Suspense Hole Tracker for Dinou v7.2.
2
+ // PPR Pre-rendering Context and Suspense Hole Tracker for Dinou.
3
3
 
4
4
  import { AsyncLocalStorage } from "node:async_hooks";
5
5
 
6
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
+ }
7
113
 
8
114
  /**
9
115
  * Executes a function within a PPR pre-render context.
@@ -12,12 +118,21 @@ const pprStorage = new AsyncLocalStorage();
12
118
  * @returns {Promise<any>}
13
119
  */
14
120
  export function runWithPprContext(options, callback) {
121
+ const sharedCounter = { nextHoleIndex: 0 };
122
+ const sharedHoles = new Map();
15
123
  const store = {
16
124
  isPpr: Boolean(options?.isPpr),
17
125
  suspenseDepth: 0,
18
126
  currentHoleId: null,
19
- holes: new Map(),
20
- nextHoleIndex: 0,
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
+ },
21
136
  ...options,
22
137
  };
23
138
  return pprStorage.run(store, callback);
@@ -37,13 +152,146 @@ export function isInsideSuspense() {
37
152
  return Boolean(store && store.suspenseDepth > 0);
38
153
  }
39
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
+
40
280
  export function enterSuspense(holeId = null) {
41
281
  const store = getPprStore();
42
282
  if (store) {
43
283
  store.suspenseDepth++;
44
284
  if (!holeId) {
45
- store.nextHoleIndex++;
46
- holeId = `ppr-hole-${store.nextHoleIndex}`;
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);
47
295
  }
48
296
  store.currentHoleId = holeId;
49
297
  return holeId;
@@ -55,7 +303,13 @@ export function exitSuspense() {
55
303
  const store = getPprStore();
56
304
  if (store) {
57
305
  store.suspenseDepth = Math.max(0, store.suspenseDepth - 1);
58
- if (store.suspenseDepth === 0) {
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) {
59
313
  store.currentHoleId = null;
60
314
  }
61
315
  }
@@ -63,9 +317,17 @@ export function exitSuspense() {
63
317
 
64
318
  export function markDynamicAccess(type = "dynamic") {
65
319
  const store = getPprStore();
66
- if (store && store.isPpr && store.suspenseDepth > 0) {
67
- const holeId = store.currentHoleId || `ppr-hole-${store.nextHoleIndex}`;
68
- const error = new Error(`[Dinou PPR] Dynamic access to '${type}' postponed inside Suspense`);
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
+ );
69
331
  error.$$typeof = Symbol.for("dinou.ppr.postpone");
70
332
  error.pprType = type;
71
333
  error.holeId = holeId;
@@ -75,7 +337,7 @@ export function markDynamicAccess(type = "dynamic") {
75
337
 
76
338
  export function registerHole(holeId, meta = {}) {
77
339
  const store = getPprStore();
78
- if (store) {
340
+ if (store && holeId) {
79
341
  store.holes.set(holeId, {
80
342
  id: holeId,
81
343
  registeredAt: Date.now(),
@@ -93,10 +355,31 @@ export function getRegisteredHoles() {
93
355
  }
94
356
 
95
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,
96
372
  runWithPprContext,
97
373
  getPprStore,
98
374
  isPprBuildActive,
99
375
  isInsideSuspense,
376
+ isInsideOuterSuspenseOutsideSlot,
377
+ rollbackPprHolesAfter,
378
+ resetPprHoles,
379
+ getPprHoleCounter,
380
+ runInSuspense,
381
+ deferPprSuspenseEffect,
382
+ commitPprSuspenseEffects,
100
383
  enterSuspense,
101
384
  exitSuspense,
102
385
  markDynamicAccess,