dinou 9.0.3 → 9.2.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/dinou/cli.js CHANGED
@@ -14,7 +14,7 @@ const projectRoot = process.cwd();
14
14
  program
15
15
  .name("dinou")
16
16
  .description("Full-stack React 19 framework with native RSC, progressive streaming, and dual-mode vendoring")
17
- .version("9.0.3")
17
+ .version("9.2.0")
18
18
  .option("-u, --unvendorized", "Run with standard node_modules dependencies instead of pre-bundled vendor packages");
19
19
 
20
20
  const isUnvendorizedRequested = () =>
@@ -1,7 +1,7 @@
1
- import { isPprBuildActive, isInsideSuspense, markDynamicAccess } from "./ppr-context.js";
1
+ import { isPprBuildActive, isInsideOuterSuspenseOutsideSlot, markDynamicAccess } from "./ppr-context.js";
2
2
 
3
3
  function triggerBailout(label, onBailout) {
4
- if (isPprBuildActive() && isInsideSuspense()) {
4
+ if (isPprBuildActive() && isInsideOuterSuspenseOutsideSlot()) {
5
5
  markDynamicAccess(label);
6
6
  } else if (typeof onBailout === "function") {
7
7
  onBailout();
@@ -912,6 +912,7 @@ async function buildStaticPages(onProgress = null) {
912
912
  const mockContext = {
913
913
  req: mockReq,
914
914
  res: mockRes,
915
+ _markDynamic: markAsDynamic,
915
916
  };
916
917
 
917
918
  let revalidate;
@@ -1274,6 +1275,7 @@ async function buildStaticPage(reqPath, isDynamic = null) {
1274
1275
  const mockContext = {
1275
1276
  req: mockReq,
1276
1277
  res: mockRes,
1278
+ _markDynamic: markAsDynamic,
1277
1279
  };
1278
1280
 
1279
1281
  let revalidate;
@@ -79,6 +79,43 @@ function enqueueRevalidation(fn) {
79
79
  return nextPromise;
80
80
  }
81
81
 
82
+ async function readEdgeStorageOrAsset(storage, key) {
83
+ if (!storage || !key) return null;
84
+ try {
85
+ const item = await storage.get(key);
86
+ if (item && item.content) return item;
87
+ } catch (e) {}
88
+ if (typeof storage.isDeleted === "function" && storage.isDeleted(key)) {
89
+ return null;
90
+ }
91
+ try {
92
+ const reqContext = getContext();
93
+ const assets =
94
+ reqContext?.env?.ASSETS ||
95
+ reqContext?.platformContext?.env?.ASSETS ||
96
+ reqContext?.req?.env?.ASSETS ||
97
+ (typeof globalThis !== "undefined" ? globalThis.__DINOU_ENV__?.ASSETS : null);
98
+ if (assets && typeof assets.fetch === "function") {
99
+ const cleanKey = String(key).replace(/^\/+/, "");
100
+ const res = await assets.fetch(new Request(`http://localhost/${cleanKey}`));
101
+ if (res && res.status === 200) {
102
+ const ct = res.headers?.get?.("content-type") || "";
103
+ const content = await res.text();
104
+ const isHtmlExpected = cleanKey.endsWith(".html");
105
+ if (
106
+ content &&
107
+ (isHtmlExpected || (!ct.includes("text/html") && !content.trimStart().startsWith("<")))
108
+ ) {
109
+ const item = { content };
110
+ await storage.set(cleanKey, content);
111
+ return item;
112
+ }
113
+ }
114
+ }
115
+ } catch (e) {}
116
+ return null;
117
+ }
118
+
82
119
  async function resolveRouteConfigForRevalidate(pagePath, reqSegments, existingMeta = {}) {
83
120
  const srcFolder = path.resolve(process.cwd(), "src");
84
121
  let isRoutePpr =
@@ -274,9 +311,12 @@ async function revalidatePage(reqPath, internalOptions = {}) {
274
311
  if (!cached) {
275
312
  cached = await storage.get(shellHtmlKey);
276
313
  }
314
+ if (!cached) {
315
+ cached = await readEdgeStorageOrAsset(storage, htmlKey);
316
+ }
277
317
  let currentMeta = (cached && cached.metadata) || {};
278
318
  try {
279
- const metaItem = await storage.get(metaKey);
319
+ const metaItem = await readEdgeStorageOrAsset(storage, metaKey);
280
320
  if (metaItem && metaItem.content) {
281
321
  currentMeta = JSON.parse(metaItem.content);
282
322
  }
@@ -284,7 +324,7 @@ async function revalidatePage(reqPath, internalOptions = {}) {
284
324
 
285
325
  let existingPageRsc = null;
286
326
  try {
287
- existingPageRsc = await storage.get(rscKey);
327
+ existingPageRsc = await readEdgeStorageOrAsset(storage, rscKey);
288
328
  } catch (e) {}
289
329
 
290
330
  const reqPathWithSlash = cleanPath.endsWith("/") ? cleanPath : cleanPath + "/";
@@ -322,7 +362,7 @@ async function revalidatePage(reqPath, internalOptions = {}) {
322
362
  const lMetaKey = entry.cleanLayoutKey
323
363
  ? `${entry.cleanLayoutKey}/layout.metadata.json`
324
364
  : "layout.metadata.json";
325
- const lItem = await storage.get(lMetaKey);
365
+ const lItem = await readEdgeStorageOrAsset(storage, lMetaKey);
326
366
  let lMetaObj = lItem?.metadata || null;
327
367
  if (!lMetaObj && lItem?.content) {
328
368
  try {
@@ -343,7 +383,7 @@ async function revalidatePage(reqPath, internalOptions = {}) {
343
383
  const refreshedMetaKey = leafLayout.cleanLayoutKey
344
384
  ? `${leafLayout.cleanLayoutKey}/layout.metadata.json`
345
385
  : "layout.metadata.json";
346
- const refreshedItem = await storage.get(refreshedMetaKey);
386
+ const refreshedItem = await readEdgeStorageOrAsset(storage, refreshedMetaKey);
347
387
  if (refreshedItem?.content) {
348
388
  try {
349
389
  const parsed = JSON.parse(refreshedItem.content);
@@ -361,7 +401,7 @@ async function revalidatePage(reqPath, internalOptions = {}) {
361
401
  const leafRscKey = leafLayout.cleanLayoutKey
362
402
  ? `${leafLayout.cleanLayoutKey}/layout.rsc`
363
403
  : "layout.rsc";
364
- const leafRscItem = await storage.get(leafRscKey);
404
+ const leafRscItem = await readEdgeStorageOrAsset(storage, leafRscKey);
365
405
  if (leafRscItem && leafRscItem.content && !leafRscItem.content.trimStart().startsWith("<")) {
366
406
  activeLayoutRscText = leafRscItem.content;
367
407
  }
@@ -389,6 +429,13 @@ async function revalidatePage(reqPath, internalOptions = {}) {
389
429
  const markRevalDynamic = () => {
390
430
  revalDynamicState.value = true;
391
431
  };
432
+ const outerCtx = getContext();
433
+ const activeEnv =
434
+ outerCtx?.env ||
435
+ outerCtx?.platformContext?.env ||
436
+ outerCtx?.req?.env ||
437
+ (typeof globalThis !== "undefined" ? globalThis.__DINOU_ENV__ : undefined) ||
438
+ {};
392
439
  const revalContext = {
393
440
  req: {
394
441
  query: createBailoutProxy({}, "req.query", markRevalDynamic),
@@ -396,6 +443,7 @@ async function revalidatePage(reqPath, internalOptions = {}) {
396
443
  headers: createBailoutProxy(rawHeaders, "req.headers", markRevalDynamic),
397
444
  path: reqPathWithSlash,
398
445
  method: "GET",
446
+ env: activeEnv,
399
447
  },
400
448
  res: {
401
449
  status() {},
@@ -412,6 +460,8 @@ async function revalidatePage(reqPath, internalOptions = {}) {
412
460
  markRevalDynamic();
413
461
  },
414
462
  },
463
+ env: activeEnv,
464
+ platformContext: outerCtx?.platformContext || { env: activeEnv },
415
465
  };
416
466
  const clientManifest =
417
467
  (typeof globalThis !== "undefined" && globalThis.__DINOU_CLIENT_MANIFEST__) || {};
@@ -423,7 +473,7 @@ async function revalidatePage(reqPath, internalOptions = {}) {
423
473
 
424
474
  if (onlyRefreshSlots) {
425
475
  try {
426
- const existingShellPage = await storage.get(shellPageKey);
476
+ const existingShellPage = await readEdgeStorageOrAsset(storage, shellPageKey);
427
477
  if (existingShellPage && existingShellPage.content) {
428
478
  const deserialized = deserializeJSX(existingShellPage.content);
429
479
  resolvedPageJsx = await runInPageSegment(() =>
@@ -811,10 +861,10 @@ async function revalidateLayout(cleanPath, internalOptions = {}) {
811
861
  const layoutMetaKey = cleanPathKey ? `${cleanPathKey}/layout.metadata.json` : "layout.metadata.json";
812
862
  const shellLayoutKey = cleanPathKey ? `${cleanPathKey}/shell.layout.json` : "shell.layout.json";
813
863
 
814
- let cached = await storage.get(layoutRscKey);
864
+ let cached = await readEdgeStorageOrAsset(storage, layoutRscKey);
815
865
  let currentMeta = (cached && cached.metadata) || {};
816
866
  try {
817
- const metaItem = await storage.get(layoutMetaKey);
867
+ const metaItem = await readEdgeStorageOrAsset(storage, layoutMetaKey);
818
868
  if (metaItem && metaItem.content) {
819
869
  currentMeta = JSON.parse(metaItem.content);
820
870
  }
@@ -853,6 +903,10 @@ async function revalidateLayout(cleanPath, internalOptions = {}) {
853
903
  currentMeta.ppr = Boolean(layoutCfg.ppr);
854
904
  }
855
905
  }
906
+ if (typeof currentMeta.ppr !== "boolean") {
907
+ const layoutRouteCfg = await resolveRouteConfigForRevalidate(null, reqSegments, currentMeta);
908
+ currentMeta.ppr = Boolean(layoutRouteCfg.isRoutePpr);
909
+ }
856
910
  } catch (e) {}
857
911
 
858
912
  const prevGenTime = (currentMeta && currentMeta.generatedAt) || 0;
@@ -875,6 +929,13 @@ async function revalidateLayout(cleanPath, internalOptions = {}) {
875
929
  const markRevalDynamic = () => {
876
930
  revalDynamicState.value = true;
877
931
  };
932
+ const outerCtx = getContext();
933
+ const activeEnv =
934
+ outerCtx?.env ||
935
+ outerCtx?.platformContext?.env ||
936
+ outerCtx?.req?.env ||
937
+ (typeof globalThis !== "undefined" ? globalThis.__DINOU_ENV__ : undefined) ||
938
+ {};
878
939
  const revalContext = {
879
940
  req: {
880
941
  query: createBailoutProxy({}, "req.query", markRevalDynamic),
@@ -882,6 +943,7 @@ async function revalidateLayout(cleanPath, internalOptions = {}) {
882
943
  headers: createBailoutProxy(rawHeaders, "req.headers", markRevalDynamic),
883
944
  path: reqPathWithSlash,
884
945
  method: "GET",
946
+ env: activeEnv,
885
947
  },
886
948
  res: {
887
949
  status() {},
@@ -898,6 +960,8 @@ async function revalidateLayout(cleanPath, internalOptions = {}) {
898
960
  markRevalDynamic();
899
961
  },
900
962
  },
963
+ env: activeEnv,
964
+ platformContext: outerCtx?.platformContext || { env: activeEnv },
901
965
  };
902
966
  const clientManifest =
903
967
  (typeof globalThis !== "undefined" && globalThis.__DINOU_CLIENT_MANIFEST__) || {};
@@ -905,7 +969,7 @@ async function revalidateLayout(cleanPath, internalOptions = {}) {
905
969
  let resolvedLayoutJsx = null;
906
970
  if (onlyRefreshSlots) {
907
971
  try {
908
- const existingShellLayout = await storage.get(shellLayoutKey);
972
+ const existingShellLayout = await readEdgeStorageOrAsset(storage, shellLayoutKey);
909
973
  if (existingShellLayout && existingShellLayout.content) {
910
974
  const deserialized = deserializeJSX(existingShellLayout.content);
911
975
  resolvedLayoutJsx = await runInLayoutSegment(() =>
@@ -922,7 +986,10 @@ async function revalidateLayout(cleanPath, internalOptions = {}) {
922
986
  }
923
987
  }
924
988
  if (!resolvedLayoutJsx) {
925
- await runWithPprContext({ isPpr: true }, async () => {
989
+ const isLayoutPpr = Boolean(
990
+ currentMeta.ppr || (Array.isArray(currentMeta.holes) && currentMeta.holes.length > 0)
991
+ );
992
+ await runWithPprContext({ isPpr: isLayoutPpr }, async () => {
926
993
  const rawJsx = await getJSX(reqPathWithSlash, {}, {}, false, false, {
927
994
  segment: "layout",
928
995
  });
@@ -13,18 +13,17 @@ import { getJSX } from "./get-jsx.js";
13
13
  import {
14
14
  isPprBuildActive,
15
15
  isInsideSuspense,
16
+ isInsideOuterSuspenseOutsideSlot,
16
17
  deferPprSuspenseEffect,
17
18
  runWithPprContext,
18
19
  isInsideLayoutSegment,
19
20
  isInsidePageSegment,
21
+ markDynamicAccess,
22
+ runInCachedSlot,
23
+ runInSlotDiscovery,
24
+ isSlotDiscoveryActive,
20
25
  } from "./ppr-context.js";
21
26
  import { createRequire } from "node:module";
22
- import { AsyncLocalStorage } from "node:async_hooks";
23
-
24
- const slotDiscoveryStorage = new AsyncLocalStorage();
25
- function isSlotDiscoveryActive() {
26
- return Boolean(slotDiscoveryStorage.getStore()?.active);
27
- }
28
27
 
29
28
  const require = createRequire(import.meta.url);
30
29
  const currentDir = path.dirname(fileURLToPath(import.meta.url));
@@ -47,6 +46,11 @@ if (!globalThis[RECENT_FRESH_KEY]) {
47
46
  }
48
47
  const recentFreshSlotRenders = globalThis[RECENT_FRESH_KEY];
49
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
+
50
54
  const BG_REVAL_KEY = Symbol.for("dinou.slot.background.revalidations");
51
55
  if (!globalThis[BG_REVAL_KEY]) {
52
56
  globalThis[BG_REVAL_KEY] = new Set();
@@ -300,10 +304,10 @@ function recordSlotTagsOnContext(
300
304
  for (const t of tags) {
301
305
  if (!t) continue;
302
306
  reqContext._slotTags.add(t);
303
- if (inLayout) {
304
- reqContext._layoutSlotTags.add(t);
305
- } else if (inPage) {
307
+ if (inPage) {
306
308
  reqContext._pageSlotTags.add(t);
309
+ } else if (inLayout) {
310
+ reqContext._layoutSlotTags.add(t);
307
311
  } else if (!reqContext._layoutSlotTags.has(t)) {
308
312
  reqContext._pageSlotTags.add(t);
309
313
  }
@@ -346,7 +350,8 @@ async function getSlotFromStorage(storage, storageKey, knownTags = []) {
346
350
  const assets =
347
351
  reqContext?.env?.ASSETS ||
348
352
  reqContext?.platformContext?.env?.ASSETS ||
349
- reqContext?.req?.env?.ASSETS;
353
+ reqContext?.req?.env?.ASSETS ||
354
+ (typeof globalThis !== "undefined" ? globalThis.__DINOU_ENV__?.ASSETS : null);
350
355
  if (assets && typeof assets.fetch === "function") {
351
356
  const cleanKey = String(storageKey || "").replace(/^\/+/, "");
352
357
  const metaKey = cleanKey.endsWith("/slot.json")
@@ -390,6 +395,37 @@ async function getSlotFromStorage(storage, storageKey, knownTags = []) {
390
395
  return cached;
391
396
  }
392
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
+
393
429
  /**
394
430
  * Traverses a deserialized JSX tree from a cached outer slot and refreshes
395
431
  * any nested <DinouCacheSlot> boundaries so inner slot invalidations are
@@ -479,10 +515,24 @@ async function refreshNestedSlotsInTree(node, currentSlotId, options = {}) {
479
515
  */
480
516
  async function DinouCacheSlot(props) {
481
517
  const { id, tag, tags, revalidate, children } = props || {};
518
+ const isNoCache = typeof revalidate === "number" && revalidate < 1;
482
519
 
483
520
  const slotId = id || tag || (Array.isArray(tags) ? tags[0] : null);
484
521
  if (!slotId) {
485
- return typeof children === "function" ? await children() : children;
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
+ });
486
536
  }
487
537
 
488
538
  const allTags = Array.isArray(tags)
@@ -494,7 +544,8 @@ async function DinouCacheSlot(props) {
494
544
 
495
545
  const storageKey = `slots/${slotId}/slot.json`;
496
546
  const storage = getStorageAdapter();
497
- const isNoCache = revalidate === 0;
547
+ const runSlotScope = (fn) =>
548
+ runInCachedSlot(fn, { cached: !isNoCache, slotId });
498
549
 
499
550
  function wrapWithBoundary(content, alreadyRendered = false) {
500
551
  const Boundary = getCacheSlotBoundary();
@@ -523,16 +574,20 @@ async function DinouCacheSlot(props) {
523
574
  // Register in slotRegistry for on-demand endpoint re-rendering and nested slot discovery
524
575
  slotRegistry.set(slotId, {
525
576
  render: async () => {
526
- const childElement = typeof children === "function" ? await children() : children;
527
- return await asyncRenderJSXToClientJSX(childElement);
577
+ return await runSlotScope(async () => {
578
+ const childElement = typeof children === "function" ? await children() : children;
579
+ return await asyncRenderJSXToClientJSX(childElement);
580
+ });
528
581
  },
529
582
  discover: async () => {
530
583
  if (discoveringSlots.has(slotId)) return;
531
584
  discoveringSlots.add(slotId);
532
585
  try {
533
- await slotDiscoveryStorage.run({ active: true }, async () => {
534
- const childElement = typeof children === "function" ? await children() : children;
535
- await asyncRenderJSXToClientJSX(childElement);
586
+ await runInSlotDiscovery(async () => {
587
+ await runSlotScope(async () => {
588
+ const childElement = typeof children === "function" ? await children() : children;
589
+ await asyncRenderJSXToClientJSX(childElement);
590
+ });
536
591
  });
537
592
  } catch (e) {
538
593
  } finally {
@@ -544,16 +599,32 @@ async function DinouCacheSlot(props) {
544
599
  });
545
600
 
546
601
  // When running inside a nested slot discovery pass, recursively discover any child slots
547
- if (isSlotDiscoveryActive() && !discoveringSlots.has(slotId)) {
548
- const ownEntry = slotRegistry.get(slotId);
549
- if (ownEntry && typeof ownEntry.discover === "function") {
550
- await ownEntry.discover();
602
+ // and short-circuit so discovery passes never overwrite storage cache with discovery renders.
603
+ if (isSlotDiscoveryActive()) {
604
+ if (!discoveringSlots.has(slotId)) {
605
+ const ownEntry = slotRegistry.get(slotId);
606
+ if (ownEntry && typeof ownEntry.discover === "function") {
607
+ await ownEntry.discover();
608
+ }
609
+ }
610
+ return null;
611
+ }
612
+
613
+ if (isNoCache && !isSlotDiscoveryActive()) {
614
+ if (isPprBuildActive() && isInsideOuterSuspenseOutsideSlot()) {
615
+ markDynamicAccess("slot-revalidate-0");
616
+ } else {
617
+ try {
618
+ getContext()?._markDynamic?.();
619
+ } catch (e) {}
551
620
  }
552
621
  }
553
622
 
554
623
  if (!storage || isNoCache) {
555
- const childElement = typeof children === "function" ? await children() : children;
556
- const directContent = await asyncRenderJSXToClientJSX(childElement);
624
+ const directContent = await runSlotScope(async () => {
625
+ const childElement = typeof children === "function" ? await children() : children;
626
+ return await asyncRenderJSXToClientJSX(childElement);
627
+ });
557
628
  commitSlotTags(slotTagsToCommit);
558
629
  return wrapWithBoundary(directContent, true);
559
630
  }
@@ -567,9 +638,9 @@ async function DinouCacheSlot(props) {
567
638
  // triggers PPR postpone.
568
639
  if (
569
640
  isPprBuildActive() &&
570
- isInsideSuspense() &&
641
+ isInsideOuterSuspenseOutsideSlot() &&
571
642
  cached &&
572
- !cached.metadata?.inStaticShell
643
+ !cached.metadata?.inStaticSuspense
573
644
  ) {
574
645
  cached = null;
575
646
  }
@@ -585,9 +656,20 @@ async function DinouCacheSlot(props) {
585
656
  ? meta.revalidate
586
657
  : undefined;
587
658
 
588
- if (revalidateSeconds === 0) {
589
- const childElement = typeof children === "function" ? await children() : children;
590
- const directContent = await asyncRenderJSXToClientJSX(childElement);
659
+ if (typeof revalidateSeconds === "number" && revalidateSeconds < 1) {
660
+ if (!isSlotDiscoveryActive()) {
661
+ if (isPprBuildActive() && isInsideOuterSuspenseOutsideSlot()) {
662
+ markDynamicAccess("slot-revalidate-0");
663
+ } else {
664
+ try {
665
+ getContext()?._markDynamic?.();
666
+ } catch (e) {}
667
+ }
668
+ }
669
+ const directContent = await runSlotScope(async () => {
670
+ const childElement = typeof children === "function" ? await children() : children;
671
+ return await asyncRenderJSXToClientJSX(childElement);
672
+ });
591
673
  commitSlotTags(slotTagsToCommit);
592
674
  return wrapWithBoundary(directContent, true);
593
675
  }
@@ -597,7 +679,41 @@ async function DinouCacheSlot(props) {
597
679
  revalidateSeconds > 0 &&
598
680
  now > generatedAt + revalidateSeconds * 1000;
599
681
 
600
- if (!isExpired) {
682
+ const serveStaleWhileRevalidate =
683
+ isExpired &&
684
+ Boolean(meta.inStaticShell) &&
685
+ !isRevalidationActive() &&
686
+ !isPprBuildActive() &&
687
+ !getContext()?.platformContext?.isSSG;
688
+
689
+ if (
690
+ serveStaleWhileRevalidate &&
691
+ !getContext()?._isPprRuntimeResume &&
692
+ !backgroundRevalidations.has(storageKey) &&
693
+ !inflightSlotRenders.has(storageKey)
694
+ ) {
695
+ backgroundRevalidations.add(storageKey);
696
+ const reqContext = getContext();
697
+ const currentPath = reqContext?.req?.path;
698
+ const bgPromise = (async () => {
699
+ try {
700
+ await getSlotJSX(slotId, { fresh: true, currentPath });
701
+ } catch (e) {
702
+ console.error(
703
+ `[DinouCacheSlot] Background revalidation failed for slot "${slotId}":`,
704
+ e
705
+ );
706
+ } finally {
707
+ backgroundRevalidations.delete(storageKey);
708
+ }
709
+ })();
710
+ const ctx = reqContext?.platformContext?.ctx;
711
+ if (ctx && typeof ctx.waitUntil === "function") {
712
+ ctx.waitUntil(bgPromise);
713
+ }
714
+ }
715
+
716
+ if (!isExpired || serveStaleWhileRevalidate) {
601
717
  try {
602
718
  const restored = deserializeJSX(cached.content);
603
719
  const withRefreshedNested = await refreshNestedSlotsInTree(restored, slotId);
@@ -612,16 +728,25 @@ async function DinouCacheSlot(props) {
612
728
  try {
613
729
  const deduped = await inflightSlotRenders.get(storageKey);
614
730
  commitSlotTags(slotTagsToCommit);
615
- return wrapWithBoundary(deduped, true);
731
+ return wrapWithBoundary(deduped, !isPprBuildActive());
616
732
  } catch (e) {}
617
733
  }
618
734
 
619
- const inPprSuspense = isPprBuildActive() && isInsideSuspense();
620
- const isPprShellBuild = isPprBuildActive();
735
+ const inPprSuspense = isPprBuildActive() && isInsideOuterSuspenseOutsideSlot();
736
+ const isPprShellBuild =
737
+ isPprBuildActive() ||
738
+ Boolean(getContext()?.platformContext?.isSSG) ||
739
+ isRevalidationActive() ||
740
+ !getContext()?._isPprRuntimeResume;
621
741
 
622
742
  const renderPromise = (async () => {
623
- const childElement = typeof children === "function" ? await children() : children;
624
- const resolved = await asyncRenderJSXToClientJSX(childElement);
743
+ const resolved = await runInCachedSlot(
744
+ async () => {
745
+ const childElement = typeof children === "function" ? await children() : children;
746
+ return await asyncRenderJSXToClientJSX(childElement);
747
+ },
748
+ { cached: true, slotId }
749
+ );
625
750
  const serialized = serializeJSX(resolved);
626
751
 
627
752
  const slotMeta = {
@@ -630,6 +755,7 @@ async function DinouCacheSlot(props) {
630
755
  generatedAt: Date.now(),
631
756
  slotId,
632
757
  inStaticShell: Boolean(isPprShellBuild || cached?.metadata?.inStaticShell),
758
+ inStaticSuspense: Boolean(inPprSuspense || cached?.metadata?.inStaticSuspense),
633
759
  };
634
760
 
635
761
  const persistSlot = async () => {
@@ -660,6 +786,7 @@ async function DinouCacheSlot(props) {
660
786
  inflightSlotRenders.delete(storageKey);
661
787
  }
662
788
  }
789
+ DinouCacheSlot.$$dinouCacheSlot = true;
663
790
 
664
791
  /**
665
792
  * Resolves a slot ID or tag to its canonical slotId using slotRegistry or storage metadata.
@@ -757,7 +884,17 @@ async function getSlotJSX(slotId, options = {}) {
757
884
  revalidateSeconds > 0 &&
758
885
  now > generatedAt + revalidateSeconds * 1000);
759
886
 
760
- if (!isExpired && !options.fresh) {
887
+ const serveStaleWhileRevalidate =
888
+ isExpired &&
889
+ !isNoCache &&
890
+ !options.fresh &&
891
+ Boolean(options.skipHostRevalidate) &&
892
+ Boolean(meta.inStaticShell) &&
893
+ !isRevalidationActive() &&
894
+ !isPprBuildActive() &&
895
+ !getContext()?.platformContext?.isSSG;
896
+
897
+ if ((!isExpired && !options.fresh) || serveStaleWhileRevalidate) {
761
898
  try {
762
899
  const restored = deserializeJSX(cached.content);
763
900
  return await refreshNestedSlotsInTree(restored, targetId, options);
@@ -800,7 +937,7 @@ async function getSlotJSX(slotId, options = {}) {
800
937
  try {
801
938
  const isNotFound = { value: false };
802
939
  await runWithPprContext({ isPpr: true }, async () => {
803
- await slotDiscoveryStorage.run({ active: true }, async () => {
940
+ await runInSlotDiscovery(async () => {
804
941
  const pageJsx = await getJSX(options.currentPath, {}, isNotFound, false, false, {
805
942
  segment: "page",
806
943
  });
@@ -881,6 +1018,7 @@ async function getSlotJSX(slotId, options = {}) {
881
1018
  generatedAt: Date.now(),
882
1019
  slotId: targetId,
883
1020
  inStaticShell: Boolean(cached?.metadata?.inStaticShell),
1021
+ inStaticSuspense: Boolean(cached?.metadata?.inStaticSuspense),
884
1022
  });
885
1023
  }
886
1024
  if (options.fresh && isLatestGen()) {
@@ -918,13 +1056,7 @@ async function getSlotJSX(slotId, options = {}) {
918
1056
  ...(Array.isArray(entry.tags) ? entry.tags : []),
919
1057
  ]);
920
1058
  const metaKey = cleanRouteKey ? `${cleanRouteKey}/metadata.json` : "metadata.json";
921
- const metaItem = await storage.get(metaKey);
922
- let pageMeta = metaItem?.metadata;
923
- if (!pageMeta && metaItem?.content) {
924
- try {
925
- pageMeta = JSON.parse(metaItem.content);
926
- } catch (e) {}
927
- }
1059
+ const pageMeta = await readMetaFromStorageOrAssets(storage, metaKey);
928
1060
  if (
929
1061
  pageMeta &&
930
1062
  Array.isArray(pageMeta.slotTags) &&
@@ -942,13 +1074,7 @@ async function getSlotJSX(slotId, options = {}) {
942
1074
  }
943
1075
  for (const lKey of candidateLayoutKeys) {
944
1076
  const lMetaKey = lKey ? `${lKey}/layout.metadata.json` : "layout.metadata.json";
945
- const lItem = await storage.get(lMetaKey);
946
- let lMeta = lItem?.metadata;
947
- if (!lMeta && lItem?.content) {
948
- try {
949
- lMeta = JSON.parse(lItem.content);
950
- } catch (e) {}
951
- }
1077
+ const lMeta = await readMetaFromStorageOrAssets(storage, lMetaKey);
952
1078
  if (
953
1079
  lMeta &&
954
1080
  Array.isArray(lMeta.slotTags) &&