Skip to content

feat(cms): provide compat ctx as 3rd arg to section loaders (#305) - #347

Merged
JonasJesus42 merged 1 commit into
mainfrom
issue-305
Jul 26, 2026
Merged

JonasJesus42 merged 1 commit into
mainfrom
issue-305

Conversation

@JonasJesus42

@JonasJesus42 JonasJesus42 commented Jul 12, 2026 •

Copy link
Copy Markdown
Contributor

Fixes #305. deco.cx (Fresh) section loaders use a 3-arg (props, req, ctx) signature, but the framework invoked loaders with only (props, req), so migrated loaders read undefined and threw — rendering sections blank on every migrated site. Rather than delete ctx (which would force rewriting every loader by hand), the framework now assembles a real compat ctx (device from the request UA, server-side ctx.invoke.* via self-fetch, per-app state via getAppState, and ctx.response.headers) and passes it as the 3rd arg through withPageContext — covering all four loader paths — with compose/withSectionLoader forwarding it and a parametrized createAppInvokeWith. On the migration side, a new ctx-compat transform optional-chains ctx.* reads so unconfigured app state degrades to undefined instead of crashing, the { seo: {} } as any SEOPDP stub (a D3 violation) is replaced with the real ctx, and the migration skills docs are updated. Validated by blocks (537) + transform (8) unit tests, a clean typecheck, and an end-to-end run on portal-davinci where the page renders with no ctx-undefined errors.

🤖 Generated with Claude Code


Summary by cubic

Provide a real compat ctx as the 3rd argument to section loaders and add a codemod to optional‑chain ctx.* reads. This addresses (#305) by matching deco.cx (Fresh) loader APIs so migrated sections stop rendering blank.

  • New Features

    • Added buildSectionLoaderContext(req) to supply ctx.device (from UA), ctx.invoke.* (server-side self-fetch), per-app state (ctx.vtex, ctx.salesforce, …), and ctx.response.headers.
    • Threaded the compat ctx through all loader paths via withPageContext; compose and withSectionLoader now forward it.
    • Introduced createAppInvokeWith({ basePath, fetcher }) and routed ctx.invoke through it with RequestContext.fetch.
    • Exported SectionLoaderContext and builder from @decocms/blocks/cms; updated SectionLoaderFn and FnContext types.
    • Added unit tests for context building, invocation, and loader plumbing.
  • Migration

    • New ctx-compat transform: optional-chains all ctx.* reads in files exporting a loader so missing app state degrades to undefined instead of throwing; added to the transform pipeline.
    • Loader templates now forward the real 3rd-arg ctx (removed the { seo: {} } as any stub) and pass it to IsEvents.
    • Docs updated to clarify that the 3rd-arg ctx is real and that ctx.response.headers is supported (writes are inert on dev/SPA serverFn).

Written for commit f6e9cd7. Summary will update on new commits.

Review in cubic

deco.cx (Fresh) section loaders use a 3-arg `(props, req, ctx)` signature,
but the framework invoked loaders with only `(props, req)` — so migrated
loaders read `undefined` and threw, rendering sections blank. Instead of
deleting `ctx` (which would force rewriting every loader by hand), the
framework now assembles a real compat `ctx` and passes it as the 3rd arg:

- New `buildSectionLoaderContext(req)` builds `ctx.device` (from the req UA),
  `ctx.invoke.*` (server-side self-fetch via RequestContext.fetch),
  per-app state (`ctx.vtex`/`ctx.salesforce` via getAppState), and
  `ctx.response.headers` — real values, no fabricated stubs (D3-clean).
- `withPageContext` builds and threads the ctx, covering all four loader
  paths (regular/layout/cacheable/SWR); `compose`/`withSectionLoader` forward
  it; `SectionLoaderFn`/`FnContext` types updated.
- `createAppInvokeWith({basePath, fetcher})` parametrizes the invoke proxy.

Migration side: new `ctx-compat` transform optional-chains `ctx.*` reads so
unconfigured app state degrades to undefined instead of crashing; removed the
`{ seo: {} } as any` SEOPDP stub (D3 violation) in favor of the real ctx;
updated migration skills docs.

Validated: blocks 537 + transform 8 unit tests, full typecheck, and an
end-to-end run on portal-davinci (ctx-compat fired, page renders, no
ctx-undefined errors).

Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
@JonasJesus42
JonasJesus42 requested a review from a team July 12, 2026 07:53
@JonasJesus42

Copy link
Copy Markdown
Contributor Author

Note: I also explored a competing approach in a scratch branch (adapting the migrate-tooling transforms to strip ctx usage instead of providing a runtime compat shim) but decided to go with this PR's approach here — discarded that branch. No action needed on this PR from that side.

@JonasJesus42
JonasJesus42 merged commit e42501d into main Jul 26, 2026
1 check passed
@JonasJesus42
JonasJesus42 deleted the issue-305 branch July 26, 2026 00:39
@github-actions

Copy link
Copy Markdown

🎉 This PR is included in version 7.23.0 🎉

The release is available on:

Your semantic-release bot 📦🚀

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.

[migrate] Section loaders reading deco.cx ctx crash at runtime — migrate transform doesn't adapt to start's (props, req) signature

1 participant