Skip to content

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

Closed

Description

@JonasJesus42

Summary

The migrate script (scripts/migrate.ts) does not adapt deco.cx section loaders to @decocms/start's loader-invocation signature, so every migrated section loader that reads ctx throws at runtime and the page renders empty. This affects essentially every deco.cx storefront (all of them use section loaders with ctx), so fixing it in the migrate transform makes every future migration land correct instead of each site having to hand-fix dozens of loaders.

Root cause

deco.cx (Fresh) section loaders use a 3-arg signature:

export const loader = (props: Props, req: Request, ctx: AppContext) => {
  const isMobile = ctx.device !== "desktop";
  const page = await ctx.invoke.magento.loaders.product.detailsPageGQL({ ... });
  const ext = ctx.salesforce?.cartExtension?.[0];
  ...
};

But @decocms/start invokes section loaders with 2 args (src/cms/sectionLoaders.ts):

const enrichedProps = await loader(section.props, request);      // lines ~129, ~282
return (props, req) => loader(injectPageContext(props, req), req); // line ~366

injectPageContext only adds __pageUrl/__pagePath to props. There is no 3rd ctx arg, so ctx is undefined in every migrated loader → ctx.device / ctx.invoke / ctx.salesforce throw Cannot read properties of undefined. React bails SSR ("Switched to client rendering because the server rendering errored"), the section renders nothing, and a loader-heavy home ends up blank.

Observed on a real migration (/ served a shell with an empty <body>; dev.log):

5x  section loader threw: Cannot read properties of undefined (reading 'salesforce')
4x  Cannot read properties of undefined (reading 'href')
2x  section loader threw: defaultLoader is not defined
1x  reading 'invoke' / 'device' / 'customConfigs'

Related: SEO sections use deco.cx's defaultLoader / DefaultProps, which don't exist in start, so they also throw.

How a working migration solved it (proven pattern)

A hand-fixed migration (granadobr-tanstack) makes the loaders defensive:

// ctx is what setup/section-loaders composes (device, search params). Site-wide
// state (salesforce, features) is not always injected — keep every read optional.
export const loader = (props: Props, _req: Request, ctx?: AppContext) => {
  const isMobile = ctx?.device !== "desktop";
  const ext = ctx?.salesforce?.cartExtension?.[0];
  return { ...props, isMobile, activeCrossellCart: ext?.active ?? false };
};
  • ctx?: AppContext optional + optional-chaining on every read.
  • useDevice() (from @decocms/start/sdk/useDevice) in components instead of ctx.device.
  • Heavy ctx.invoke.* data fetching removed from the section loader (kept to cheap prop derivation).
  • SEO sections rewritten to import renderTemplateString / SEOSection from @decocms/apps/website/components/Seo and inline the title/description/canonical normalization (no defaultLoader/DefaultProps).

Proposed fix (in the migrate transform)

Make scripts/migrate.ts emit start-compatible loaders automatically:

  1. Rewrite loader signatures to (props, req, ctx?: AppContext) and optional-chain all ctx.* reads (ctx?.x, with ?? fallback).
  2. Convert ctx.device usage in components to the useDevice() hook.
  3. For ctx.invoke.* heavy fetches inside section loaders, port to the start invoke/server-fn pattern (or move off the section loader).
  4. Convert deco.cx SEO sections (defaultLoader/DefaultProps) to the @decocms/apps/website/components/Seo helpers.

Alternatively / additionally, sectionLoaders.ts could pass a partial compat ctx (device + search params + invoke) as the 3rd arg so legacy loaders degrade instead of crashing — but the optional-chaining transform above is what the working migration relies on today.

Impact

Every deco.cx → TanStack migration currently produces loaders that crash on first render (blank pages, parity impossible) until fixed by hand. This is the single highest-leverage migrate-transform gap.

Repro

Migrate any deco.cx storefront with section loaders that read ctx (e.g. a VTEX/Magento commerce site), run bun run dev, and load / — the body is empty and dev.log shows the loader ctx errors above.


Filed from the tanstack-migrator pipeline; tagged [framework?] in its cross-migration notes.

Activity

  1. github-actions commented on Jul 26, 2026

    @github-actions

    🎉 This issue has been resolved 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

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions