feat(cms): provide compat ctx as 3rd arg to section loaders (#305)#347
Open
JonasJesus42 wants to merge 1 commit into
Open
feat(cms): provide compat ctx as 3rd arg to section loaders (#305)#347JonasJesus42 wants to merge 1 commit into
JonasJesus42 wants to merge 1 commit into
Conversation
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) <noreply@anthropic.com>
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.
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 readundefinedand threw — rendering sections blank on every migrated site. Rather than deletectx(which would force rewriting every loader by hand), the framework now assembles a real compatctx(device from the request UA, server-sidectx.invoke.*via self-fetch, per-app state viagetAppState, andctx.response.headers) and passes it as the 3rd arg throughwithPageContext— covering all four loader paths — withcompose/withSectionLoaderforwarding it and a parametrizedcreateAppInvokeWith. On the migration side, a newctx-compattransform optional-chainsctx.*reads so unconfigured app state degrades toundefinedinstead of crashing, the{ seo: {} } as anySEOPDP 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 onportal-davinciwhere the page renders with noctx-undefined errors.🤖 Generated with Claude Code
Summary by cubic
Provide a real compat
ctxas the 3rd argument to section loaders and add a codemod to optional‑chainctx.*reads. This addresses (#305) by matching deco.cx (Fresh) loader APIs so migrated sections stop rendering blank.New Features
buildSectionLoaderContext(req)to supplyctx.device(from UA),ctx.invoke.*(server-side self-fetch), per-app state (ctx.vtex,ctx.salesforce, …), andctx.response.headers.ctxthrough all loader paths viawithPageContext;composeandwithSectionLoadernow forward it.createAppInvokeWith({ basePath, fetcher })and routedctx.invokethrough it withRequestContext.fetch.SectionLoaderContextand builder from@decocms/blocks/cms; updatedSectionLoaderFnandFnContexttypes.Migration
ctx-compattransform: optional-chains allctx.*reads in files exporting aloaderso missing app state degrades toundefinedinstead of throwing; added to the transform pipeline.ctx(removed the{ seo: {} } as anystub) and pass it toIsEvents.ctxis real and thatctx.response.headersis supported (writes are inert on dev/SPA serverFn).Written for commit f6e9cd7. Summary will update on new commits.