Skip to content

fix: globalThis-backed CMS state for Vite split modules - #4

Merged
JonasJesus42 merged 2 commits into
mainfrom
JonasJesus42/cms-loader-resolve
Mar 11, 2026
Merged

JonasJesus42 merged 2 commits into
mainfrom
JonasJesus42/cms-loader-resolve

Conversation

@JonasJesus42

@JonasJesus42 JonasJesus42 commented Mar 11, 2026 •

Copy link
Copy Markdown
Contributor

Summary

  • Back all CMS shared state (blockData, sectionRegistry, commerceLoaders, sectionLoaderRegistry, layoutSections, etc.) with globalThis.__deco so Vite server function code-splitting does not create isolated empty registries
  • Change loadDeferredSection from GET to POST since it sends a request body

Test plan

  • Verify deferred sections load correctly (scroll-triggered lazy sections)
  • Verify CMS page resolution works in production builds (where Vite splits server functions)
  • Verify admin preview (block override via ALS) still works
  • Verify layout section caching still functions

Generated with Claude Code


Summary by cubic

Fixes isolated CMS state from Vite server function code-splitting by moving all shared CMS data to globalThis.__deco and consistently reading from it. Also switches the deferred section loader to POST to send a JSON body.

  • Bug Fixes

    • Share and read state via globalThis.__deco (blocks+revision with resync, registries/caches, commerce loaders/matchers, async config, init flags) so split modules see the same data.
    • Prevents empty or stale registries in server functions; CMS page resolution, admin preview overrides, layout section caching, and deferred sections work reliably in dev and prod.
  • Migration

    • loadDeferredSection now uses POST. If you call it directly, send a JSON body with component, rawProps, and pagePath.

Written for commit 51e5a10. Summary will update on new commits.

Vite server function code-splitting creates isolated module instances,
causing registries (blockData, sectionRegistry, commerceLoaders, etc.)
to be empty in server function contexts. Back all shared state with
globalThis.__deco so all module instances see the same data.

Also changes loadDeferredSection from GET to POST since it sends a
request body (component, rawProps, pagePath).

Co-Authored-By: Claude Opus 4.6 <[email protected]>

@cubic-dev-ai cubic-dev-ai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

5 issues found across 5 files

Prompt for AI agents (unresolved issues)

Check if these issues are valid — if so, understand the root cause of each and fix them. If appropriate, use sub-agents to investigate and fix each issue separately.


<file name="src/cms/sectionLoaders.ts">

<violation number="1" location="src/cms/sectionLoaders.ts:24">
P2: Move `cacheableSections` to `globalThis` too; split module instances will otherwise bypass the configured SWR cache.</violation>
</file>

<file name="src/cms/loader.ts">

<violation number="1" location="src/cms/loader.ts:97">
P1: This only re-syncs empty local caches, so split-module instances that already loaded blocks can keep serving stale CMS data after another instance calls `setBlocks()`.</violation>
</file>

<file name="src/cms/resolve.ts">

<violation number="1" location="src/cms/resolve.ts:56">
P1: Reading `asyncConfig` from `globalThis` only once leaves earlier split-module copies with stale rendering config.</violation>

<violation number="2" location="src/cms/resolve.ts:233">
P1: Gating initialization on the global flag prevents later split-module copies from applying module-local resolver setup.</violation>
</file>

<file name="src/cms/registry.ts">

<violation number="1" location="src/cms/registry.ts:25">
P1: The split-module fix is incomplete: `resolvedComponents` and `syncComponents` still stay module-local, so preloaded/sync section components can disappear across server chunks.</violation>
</file>

Reply with feedback, questions, or to request a fix. Tag @cubic-dev-ai to re-run a review.

Comment thread src/cms/loader.ts Resolved
Comment thread src/cms/resolve.ts Resolved
Comment thread src/cms/resolve.ts Resolved
Comment thread src/cms/registry.ts Resolved
Comment thread src/cms/sectionLoaders.ts Resolved
… state

- loader.ts: compare by reference instead of checking empty (catches stale data)
- resolve.ts: read asyncConfig from globalThis on every access, not just init
- resolve.ts: simplify ensureInitialized to use globalThis directly
- registry.ts: back resolvedComponents and syncComponents with globalThis
- sectionLoaders.ts: back cacheableSections with globalThis

Co-Authored-By: Claude Opus 4.6 <[email protected]>
@JonasJesus42
JonasJesus42 merged commit d9aa3c2 into main Mar 11, 2026
1 check passed
@github-actions

Copy link
Copy Markdown

🎉 This PR is included in version 0.16.3 🎉

The release is available on:

Your semantic-release bot 📦🚀

JonasJesus42 added a commit that referenced this pull request Jun 18, 2026
…#256)

"key" was listed as a React-internal prop to skip during schema generation.
But unlike "ref" or "$$typeof", React's JSX "key" is a special attribute
that never appears as an explicit TypeScript interface property — so filtering
it drops legitimate "key" fields from data interfaces.

Example breakage: a loader Props with `selectedFacets?: { key: string;
value: string }[]` generated a schema with only "value" — the "key" field
(e.g. "productClusterIds") was silently lost, blocking CMS editing.

Fixes DECO-5296 bug #4.

Co-authored-by: Claude Sonnet 4.6 (1M context) <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant