Skip to content

fix(blocks): layout sections are never deferred, even when marked ⚡ - #573

Closed
aline-pereira wants to merge 1 commit into
mainfrom
fix/layout-sections-never-defer
Closed

aline-pereira wants to merge 1 commit into
mainfrom
fix/layout-sections-never-defer

Conversation

@aline-pereira

@aline-pereira aline-pereira commented Sep 25, 2026 •

Copy link
Copy Markdown
Contributor

The guard already exists — it is just in the wrong place

shouldDeferSection has exactly the right check. It sits below the admin ⚡ check, so it never runs for the case it was written for:

// ~1656
if (cfg.respectCmsLazy && isCmsDeferralWrapped(section, matcherCtx)) return true;
…
// ~1667 — unreachable for any ⚡ layout section
// "Layout sections (Header, Footer, Theme) … deferring them would flash a
//  skeleton on every navigation."
if (isLayoutSection(finalKey)) return false;

A named Footer block whose root is Lazy.tsx is ordinary in decofiles carried over from Deco on Fresh — the editor never chose it, the migration did. With the guard below the ⚡, that footer defers.

Measured on a production storefront

Turning respectCmsLazy on, on a real migrated store:

Footer on arrival at every route skeleton (data-deferred="true", 2 links)
After a SPA navigation back to skeleton
Time to come back ~530ms, and only after scrolling to the bottom
/            → deferred: [banner-collection, product-card-carousel, the-post, Footer-Footer]
  scroll     → deferred: [], footerLinks 17, "Encontre uma Oficina próxima a você"
click → /roupas/calcas
             → deferred: ["Footer-Footer"], footerLinks 2, "Buscando uma Oficina próxima a você"
  scroll     → resolved after 529ms

The content sections deferring is correct and desirable — that is the whole point of ⚡, and it took that store's home from 534 KB to 221 KB of HTML. The footer doing it is the failure.

It also crashes on the way there

The framework does not serialize rawProps to the client, so a LoadingFallback is called with {}:

// DeferredSectionSkeleton, @decocms/tanstack
// rawProps are no longer serialized to the client — pass empty object.
// LoadingFallback components should be pure layout skeletons.
return createElement(options.loadingFallback, deferred.rawProps ?? {});

A footer fallback that renders the real component — a convention this repo's own comment at DecoPageRenderer.tsx:438 documents ("see Footer.tsx/ETCImageContent.tsx in a production storefront migration's T64") — then hits <Newsletter {...undefined} /> and dies:

TypeError: Cannot read properties of undefined (reading 'success')
    at Newsletter (…/worker-entry.js:14462:121)

taking the whole footer subtree down with it.

What the site had to do instead

setAsyncRenderingConfig({ respectCmsLazy: false }) — which forces every ⚡ content section to render eagerly too. On that storefront's home: 534 KB of HTML against the 160 KB its previous stack served, plus two JSON-LD ItemList blocks the old site never published.

The change

Moves isLayoutSection above the ⚡ check. It becomes the only thing besides the bot short-circuit that outranks the admin, and deliberately so:

Tests

isLayoutSection was mocked to a constant false, so no test could reach the guard. The mock is now a controllable key set (default still false — every existing test is unaffected), plus 4 cases:

  • never defers a layout section the editor marked ⚡
  • still defers a ⚡ content section on the same page
  • never defers a layout section by position either (finite foldThreshold)
  • export const deferred = true still wins for a layout section

Gate Evidence

Gate Resultado
blocks test ✅ 811 passed / 26 failed — baseline é 807 / 26 em main; as mesmas 26 falhas pré-existentes em loader.test.ts, +4 testes novos
blocks typecheck ✅ limpo

Relacionado: deco-sites/oficina-tanstack#396 (a PR do site que liga respectCmsLazy e depende desta), #569.

🤖 Generated with Claude Code


Summary by cubic

Fixes layout sections (Header, Footer, Theme) deferring when marked ⚡, which caused chrome skeletons to flash on every SPA navigation and could crash the footer subtree when LoadingFallback received empty props.

Bug Fixes

  • Moved the layout-section guard above the admin ⚡ check so it now runs for lazily wrapped chrome; previously it was unreachable for those sections.
  • Keeps export const deferred = true winning and leaves neverDefer/alwaysEager ordering untouched.
  • Mitigates the crash from missing rawProps serialization and the 530ms skeleton‑and‑scroll delay measured on a migrated storefront.

Written for commit 14bfd65. Summary will update on new commits.

Review in cubic

`shouldDeferSection` already has the guard — it is just below the admin ⚡
check, so it never runs for the case it was written for:

    // ~1656
    if (cfg.respectCmsLazy && isCmsDeferralWrapped(section, matcherCtx)) return true;
    …
    // ~1667 — unreachable for any ⚡ layout section
    // "deferring them would flash a skeleton on every navigation"
    if (isLayoutSection(finalKey)) return false;

A named `Footer` block whose root is `Lazy.tsx` is ordinary in decofiles
carried over from Deco on Fresh — the editor never chose it, the migration did.
With the guard below the ⚡, that footer defers: it re-enters its skeleton on
every SPA navigation and only comes back once the user scrolls it into view.
Measured on a production storefront: skeleton on arrival at every route, ~530ms
to resolve after scrolling to the bottom.

It also crashes on the way there. The framework does not serialize `rawProps`
to the client, so a LoadingFallback is called with `{}` — and a footer fallback
that renders the real component (a documented convention; the same storefront's
Footer.tsx and ETCImageContent.tsx both do it) then hits
`<Newsletter {...undefined} />` and dies on `messages.success`, taking the
whole footer subtree down with it.

The site's only recourse was `setAsyncRenderingConfig({ respectCmsLazy: false })`,
which forces every ⚡ CONTENT section to render eagerly too. On that storefront's
home that meant 534 KB of HTML against the 160 KB its previous stack served, and
two JSON-LD `ItemList` blocks that the old site never published.

This moves `isLayoutSection` above the ⚡ check. It is now the only thing besides
the bot short-circuit that outranks the admin, and deliberately so:
`neverDefer`/`alwaysEager` stay below, where #277 put them. Those express a
section author's opinion about a CONTENT section and must not beat an editor's
explicit ⚡. "Should the site chrome disappear between pages" is a different
question — not one the ⚡ toggle is meant to answer, and the admin has no
separate control to say otherwise.

`export const deferred = true` still wins for a layout section: it sits above
this check, and a site that writes it means it.

Gate Evidence
  blocks test       ✅ 811 passed / 26 failed — as baseline (main: 807 / 26,
                    mesmas falhas pré-existentes em loader.test.ts); +4 testes
                    novos
  blocks typecheck  ✅ limpo

Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
@aline-pereira
aline-pereira requested a review from a team September 25, 2026 16:01
@aline-pereira
aline-pereira deleted the fix/layout-sections-never-defer branch September 25, 2026 16:15
aline-pereira added a commit that referenced this pull request Sep 29, 2026
`isCmsDeferralWrapped` treats every `website/sections/Rendering/Lazy.tsx`
wrapper as a deferral, whatever props it carries. Fresh does not: the
wrapper's own loader short-circuits on `loading: "eager"` and renders the
inner section inline, with no FallbackWrapper.

```ts
// website/sections/Rendering/Lazy.tsx
const shouldRender = loading === "eager" || shouldForceRender({ … });
if (shouldRender) return { loading: "eager", section: await section() };
```

```tsx
if (loading === "lazy") {
  return <ctx.FallbackWrapper …><section.Component … /></ctx.FallbackWrapper>;
}
return <section.Component {...section.props} />;   // eager → inline
```

Decofiles carried over from Deco on Fresh use exactly that shape for the
named `Footer` block — `Lazy.tsx` at the root plus `loading: "eager"` — so
ignoring the prop turns a section prod server-renders into a skeleton that
only materializes client-side.

Measured on a migrated storefront (`/sobre`, `respectCmsLazy: true`), with
JavaScript disabled — i.e. what a non-rendering consumer of the HTML sees:

|                        | prod (Fresh) | migrated target |
|------------------------|--------------|-----------------|
| `<footer>` links       | 17           | 2               |
| `<footer>` text length | 474          | 0               |
| links in the document  | 27           | 11              |

Gone from the served HTML: the newsletter heading, the three social links,
all fourteen sitemap links, the postal address and the CNPJ. With JavaScript
on, the footer stayed empty at `scrollY = 0` for as long as it was observed
(16s) — it only filled once scrolled into view. Bots are unaffected:
`shouldDeferSection` already short-circuits on `isBotReq`.

The fix keeps walking inward after an eager wrapper instead of returning
`false` outright, so a nested lazy wrapper further down still defers —
mirroring Fresh, which resolves the inner section and lets its own loader
decide.

This is not a code-level override of the admin: `loading` is authored in the
CMS, so the editor stays the source of truth. It reads the whole instruction
instead of half of it, which is why it does not run into the objection that
closed #573.

Refs #573, #580, deco-sites/oficina-tanstack#419

Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
@github-actions

github-actions Bot commented Oct 8, 2026

Copy link
Copy Markdown

🎉 This PR is included in version 7.77.2 🎉

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.

1 participant