Skip to content

fix(blocks): honour loading: "eager" on a CMS Lazy wrapper - #583

Merged
aline-pereira merged 1 commit into
mainfrom
fix/lazy-loading-eager
Oct 8, 2026
Merged

aline-pereira merged 1 commit into
mainfrom
fix/lazy-loading-eager

Conversation

@aline-pereira

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

Copy link
Copy Markdown
Contributor

The prop Fresh reads and we do not

isCmsDeferralWrapped returns true for any website/sections/Rendering/Lazy.tsx wrapper, whatever props it carries:

if (rt === WELL_KNOWN_TYPES.LAZY || rt === WELL_KNOWN_TYPES.DEFERRED) {
  return true;
}

Fresh's own Lazy.tsx does not. loading: "eager" short-circuits its loader, which awaits the inner section and renders it inline — no FallbackWrapper, no second hop:

const shouldRender = loading === "eager" || shouldForceRender({ ctx, searchParams });
if (shouldRender) {
  return { loading: "eager", section: isDeferred<Section>(section) ? await section() : section };
}
if (loading === "lazy") {
  return <ctx.FallbackWrapper props={{ loading: "eager" }}><section.Component {...section.props} /></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". The editor never chose it; the migration produced it. So a section prod server-renders becomes a skeleton that only materializes client-side.

On the storefront that surfaced this, it is the only block with the prop: 8 blocks are Lazy-wrapped, exactly 1 is eager.

Measured end to end

bun run preview (production Cloudflare Workers build) of a migrated storefront, /sobre, respectCmsLazy: true, browser User-Agent, cache MISS. Same harness, same build, only this commit differs:

before after prod (Fresh)
data-deferred on the footer 1 0 —
<footer> links in the served HTML 2 17 17
<footer> text length 90 515 474
links in the document 11 26 27

Missing from the served HTML before the fix: 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 when scrolled into view.

Instrumenting shouldDeferSection confirms the predicate is what flips:

[DBG-FOOTER] {"rt":"Footer","finalKey":"site/sections/Footer/Footer.tsx",
              "respectCmsLazy":true,"wrapped":false,"alwaysDefer":false,"layout":true}

wrapped was true before this commit. With it false, the already-present isLayoutSection guard below finally gets to run — which is the check the file's own comment says exists for Header/Footer/Theme.

Two traps worth flagging for anyone reproducing this:

  • a bare curl is classified as a bot, and shouldDeferSection short-circuits on isBotReq, so it takes the eager path regardless and shows no difference. Use a browser UA.
  • the worker caches the HTML response; a repeat request returns x-cache: HIT from the pre-fix build. Bust the URL.

vite dev is not a usable harness: it emits no Suspense placeholders and no data-deferred, and server-renders the footer even unpatched.

Bots were never affected by the bug, which is why crawlers still saw the full footer.

The change

One branch in isCmsDeferralWrapped. An eager wrapper keeps walking inward rather than 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.

The eager render path needs nothing: internalResolve already unwraps LAZY to obj.section.

Why this is not #573 again

#573 proposed moving the isLayoutSection guard above the admin ⚡ check, which fought the documented rule that the admin is the source of truth and no code flag may override it. This adds no code-level override: loading is authored in the CMS. The editor stays the source of truth — we just read the whole instruction instead of half of it.

Tests

Three added to shouldDeferSection: an eager wrapper is not deferred; loading: "lazy" and a wrapper with no loading still defer; an eager wrapper around a lazy one still defers. resolve.test.ts green (82/82). tsc --noEmit on packages/blocks clean.

No existing test in the monorepo constructs a Lazy/Deferred wrapper with a loading prop (zero grep hits across every *.test.ts* before this PR), so the change is inert for the rest of the suite.

packages/blocks has pre-existing failures on main and the suite is flaky under load — three runs of near-identical code gave 41, 43 and 60 failures. layoutCacheRace.test.ts was checked directly and fails identically with and without this commit.

Prettier already flags both touched files on main, so they were left unformatted rather than reflowed into an unrelated diff.

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

🤖 Generated with Claude Code

@aline-pereira
aline-pereira requested a review from a team September 29, 2026 18:14
`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]>
@aline-pereira
aline-pereira merged commit 8297241 into main Oct 8, 2026
1 check passed
@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.

2 participants