Repository navigation
fix(cms): renderJson serializes nested sections, keeps loader-owned device, runs loaders of deeply nested sections - #648
Merged
Conversation
…evice, deep nested-loader walk
Three ?renderJson divergences from Fresh/deco-runtime, all in the same
path (section-in-section props):
- `serializeRenderJson` only projected the top level. A section nested in
another's props came out with its raw CMS props and the resolver's
`{ Component, props }` key, while Fresh's `walkValue` recurses, applies
the nested section's own `renderJson` and writes `{ component, props }`
(closes #567). Dropped nested sections become `null` in an object and
are removed from an array, as in Fresh.
- `device`/`isMobile`/`currentSearchParam` were stripped from every
section (#472) because the mixins inject them — but a section whose own
loader returns `device: ctx.device` as content loses it, and
production's `?renderJson` carries it. New opt-in: `keepInjectedKeys`
on the serializer, `renderJsonKeepKeys` (resolveType suffix → keys) on
`createDecoWorkerEntry`.
- `enrichNestedSections` only looked at direct props and direct array
items, so a section reachable through a plain object
(`variants: [{ section }]`, `layout: { aside }`) never had its loader
run. The walk now descends plain objects/arrays at any depth, still
returning the same reference when nothing changed.
Found while validating deco-sites/oficina-tanstack#479.
Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
…ader output The deep nested-section walk ran async on every node — one Promise and one Promise.all per object/array — on the output of every section loader, even when no nested section existed. A PLP payload paid ~10.5 ms per section call (0.42 MB of props) against 0.03 ms on main. Now a synchronous scan first marks the arrays/objects whose subtree holds a nested section; the async walk descends only into marked nodes. Props without one cost a single sync traversal and keep their reference with no Promise allocated: 0.67 ms on the same payload. Cyclic loader output (`a.self = a`) overflowed the stack and, since the enrich step ran outside any try/catch, rejected the whole runSectionLoaders batch — on main it rendered. The scan memoizes nodes below a depth guard, the walk keeps a cycle's original reference, and a failing enrich logs and keeps the section's props. Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
|
🎉 This PR is included in version 7.77.13 🎉 The release is available on:
Your semantic-release bot 📦🚀 |
This was referenced Oct 12, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Three
?renderJsondivergences from Fresh/deco-runtime, all on the same path (sections nested in another section's props). Closes #567.1. Nested sections came out raw and as
{ Component, props }serializeRenderJsononly applied therenderJsonprojection (and the framework-key strip) at the top level. A section nested in another's props (vitrine/section-tabs.tsx→variants[].section, the LP Prime hero'scontentSections) was emitted with its raw CMS props and the resolver's capitalComponentkey.Reference — deco-runtime's serializer recurses (
runtime/routes/serialize-section.ts,walkValue): every nested section is serialized with its ownrenderJsonand written as{ component, props }, the same shape as the top level; a dropped nested section (renderJson === false) isnullin an object and removed from an array (dense list for the consumer).Measured (from #567):
www.oficina.com/vitrine?renderJson→sections[7].props.section.propsis the projected contract (path,media,optimizedUrl); the TanStack site returnedlink,responsiveMedia,textColorMode: { desktop }and the desktop image. The site works around both the raw props and theComponentkey insrc/sdk/render-json-globals.ts(withNestedComponentKey).Fix: after projection + strip,
walkValuerecurses into the props; nested{ Component, props }→{ component, props: project(...) }with the same projector (projection → strip → recurse). Same reference when nothing changed.2.
devicestripped even when the section's loader owns it#472 strips
device/isMobile/currentSearchParamfrom every section because thewithDevice/withMobile/withSearchParammixins inject them (request-derived, ETag churn). That is right for the mixins, but a section whose own loader returnsdevice: ctx.deviceas content (Fresh:ProductDetails,listing-page, the rich-content sections, The Post'spost-content/post-cover…) loses a field production's?renderJsoncarries — measured onwww.oficina.com/<pdp>?renderJson→ProductDetails.props.device: "desktop"for the app's UA; TanStack omitted it. The site re-attaches it from an allowlist (LOADER_DEVICE_SECTIONSinrender-json-globals.ts).The serializer cannot tell an injected key from a loader-returned one, so this is opt-in per section (a kept key legitimately varies the ETag per device, since the content does):
SerializeOptions.keepInjectedKeys?: (component) => readonly string[] | undefinedcreateDecoWorkerEntry({ renderJsonKeepKeys: { "Product/ProductDetails.tsx": ["device"] } })— resolveType suffix, matched likerenderJson.sectionsToIgnore; applied to the page envelope and the__sectionlazy fetch, top-level and nested.Default behavior is unchanged (nothing kept).
3. Loaders of sections nested inside plain objects never ran
enrichNestedSectionsonly inspected direct prop values and direct array items for{ Component, props }. A section reachable through a plain object —variants: [{ name, section: Section }],layout: { aside: Section }— was never found, so its loader never ran and it rendered (HTML and JSON) with raw CMS props. The resolver resolves a section reference wherever the CMS placed it; the loader walk now does the same: descends plain objects and arrays at any depth, loaders in parallel, same reference when nothing changed (the zero-alloc leaf path is kept).Tests
packages/blocks/src/cms/renderJson.test.ts(+6): nested section inside an object in an array is projected and written as{ component, props }; dropped nested →null/ removed from array; wrapper-inside-wrapper recursion; props without nested sections keep the same reference;keepInjectedKeyskeeps a key only for opted-in sections, top-level and nested.packages/blocks/src/cms/sectionLoaders.test.ts(+1): loader runs forvariants[].sectionandlayout.aside, non-section siblings untouched.vitest runonrenderJson,sectionLoaders,resolve,cmsRoutetests: all pass (168/169 — the 1 failure islayoutCacheRace.test.ts, which fails identically on a cleanmainon Node 22 for lack ofURLPattern; the package'senginesasks for Node 24).tsc --noEmitclean forpackages/blocksandpackages/tanstack;biome checkclean on the touched files.Found while validating deco-sites/oficina-tanstack#479.
🤖 Generated with Claude Code
Review follow-up
Promise.allper object/array) on every loader output, even with nothing nested. 0.42 MB PLP props: 10.5 ms → 0.67 ms per section call (main: 0.03 ms — it only looked one level deep). A synchronous scan marks the subtrees that hold a nested section and the async walk descends only into those; props without one keep their reference with no Promise allocated. Tests: noPromise.alland same reference without nested sections; only the marked subtree is descended.a.self = aoverflowed the stack and, outside any try/catch, rejected the wholerunSectionLoadersbatch (main rendered it). The scan memoizes below a depth guard, the walk keeps a cycle's original reference, and a failing enrich logs and keeps the section's props. Tests: cyclic props resolve; a nested section reachable through a cycle runs once; a subtree shared by two props is enriched in both places.blocks+tanstack: 1159 passed; the 43 failures are the pre-existing ones (loader/layoutCacheRacewithoutURLPatternon Node 22, 3 draft-preview/CDN inworkerEntry). Typecheck and biome clean.