Skip to content

fix(tanstack): deferred section never loads when the reader is already past it - #599

Merged
guitavano merged 1 commit into
mainfrom
fix/deferred-section-already-passed
Oct 9, 2026
Merged

guitavano merged 1 commit into
mainfrom
fix/deferred-section-already-passed

Conversation

@aline-pereira

@aline-pereira aline-pereira commented Oct 2, 2026 •

Copy link
Copy Markdown
Contributor

The bug

IntersectionObserver fires on entering. DeferredSectionWrapper attaches its observer one frame after hydration — deliberately, so the router's scroll reset lands first (deferredObserverTiming.test.tsx guards that ordering). But at that moment the reader is not necessarily at the top of the document.

Browser scroll restoration on reload, back/forward, an #anchor, Lenis restoring position, or a programmatic scrollTo all land below the fold. A deferred section left above the viewport then receives exactly one callback with isIntersecting: false, and never fires again while the reader keeps scrolling down.

It stays on its LoadingFallback for the rest of the visit.

Nothing rescues it:

  • there is no time / requestIdleCallback / visibilitychange fallback on this path;
  • export const eager = true does not help — an admin ⚡ wins in shouldDeferSection (blocks resolve.ts:1688) before any code flag is consulted;
  • the newer <Await promise={deferredPromises[...]}> path in this same file is unreachable in 7.67.0 (nothing produces deferredPromises — loadCmsPage returns only deferredSections, and promises can't cross the serverFn JSON boundary), so every deferred section goes through this observer.

Measured

Storefront /prime, iPhone 390×844, landing forced at y=5200 with the product shelf at 3164–3829 (i.e. entirely above the viewport):

8s 13s 43s 83s after scrolling back in
Fresh (production) skeleton 16 products — — 16
TanStack 7.67.0 skeleton skeleton skeleton skeleton 16

The section in question is a product shelf with subscriber pricing — a conversion surface — so this is a revenue-visible UX bug, not cosmetic. Crawlers are unaffected (resolve.ts:1664-1667 renders eagerly for bots), which is why it never showed up in SEO checks.

Worth noting: the Fresh side recovers partly because its document is unstable while sections resolve, which drags the viewport back into the section. TanStack reserves the correct height (identical CLS, 0.0664) and therefore loses that accidental rescue.

The fix

Load when the element is intersecting or already entirely above the viewport:

return entry.isIntersecting || entry.boundingClientRect.bottom < 0;

This cannot reintroduce the thundering herd the frame gate guards against. That herd is sections below a stale scroll offset on SPA navigation; by the time the observer attaches, the router has reset scroll to the top, where nothing is above the viewport. The two conditions are disjoint.

rootMargin: "300px" already reports an element whose bottom sits between −300 and 0 as intersecting, so the new branch only adds the genuinely-passed case; disconnect() guards against any double fire regardless.

The decision is extracted as an exported shouldLoadOnObservation purely so it can be tested — jsdom has no layout and never fires a real IntersectionObserver, so the interesting case cannot be produced by rendering the component.

Tests

packages/tanstack/src/hooks/deferredObserverPassed.test.tsx — 4 cases: already passed (loads), in view (loads), still below (does not load — the case deferral exists for), and the bottom === 0 boundary (does not count as passed).

  • bun run --filter @decocms/tanstack typecheck → clean
  • bun run --filter @decocms/tanstack test → 283 passed, 2 failed; both failures are pre-existing cache-header tests unrelated to this change (verified on a clean tree: same 2 fail, 279 passed).

Downstream

Found during a Fresh → TanStack parity QA of oficina-tanstack; tracked there as deco-sites/oficina-tanstack#419.

🤖 Generated with Claude Code


Summary by cubic

Fixes deferred sections staying stuck on their loading fallback for the rest of the visit when the reader is already past them.

  • IntersectionObserver only fires on entering, so a section already above the viewport when the observer attaches gets one isIntersecting: false callback and never fires again — scroll restoration, #anchor links, and programmatic scrollTo all hit this.
  • Deferred sections now also load when boundingClientRect.bottom < 0 (entirely above the viewport), which can't reintroduce the thundering herd the frame gate guards against.
  • Extracts the decision as shouldLoadOnObservation so it's testable in jsdom, which never fires a real IntersectionObserver.

Written for commit 141b7e3. Summary will update on new commits.

Review in cubic

…y past it

`IntersectionObserver` fires on ENTERING. `DeferredSectionWrapper` attaches its
observer one frame after hydration (deliberately — the frame gate lets the
router's scroll reset land first, see deferredObserverTiming.test.tsx), and at
that moment the reader is not necessarily at the top of the document: browser
scroll restoration on reload, back/forward, an `#anchor`, or a programmatic
`scrollTo` all land below the fold.

A deferred section left above the viewport then gets exactly one callback with
`isIntersecting: false` and never fires again while the reader keeps scrolling
down. It stays on its LoadingFallback for the whole visit. There is no
time/idle/visibility fallback on this path, and `export const eager = true`
does not help: an admin ⚡ wins in `shouldDeferSection` before any code flag is
consulted.

Measured on a storefront `/prime`, iPhone 390x844, landing forced at y=5200
with the product shelf at 3164-3829:

| | 8s | 13s | 43s | 83s | after scrolling back in |
|---|---|---|---|---|---|
| Fresh (prod) | skeleton | 16 products | - | - | 16 |
| TanStack 7.67.0 | skeleton | skeleton | skeleton | skeleton | 16 |

Load when the element is intersecting OR already entirely above the viewport
(`boundingClientRect.bottom < 0`). This cannot reintroduce the thundering herd
the frame gate guards against: that herd is sections BELOW a stale scroll
offset, and by the time the observer attaches the router has reset scroll to
the top, where nothing is above the viewport.

The decision is extracted as `shouldLoadOnObservation` so it can be tested —
jsdom has no layout and never fires a real IntersectionObserver.

Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
@aline-pereira
aline-pereira requested a review from a team October 2, 2026 12:42

@cubic-dev-ai cubic-dev-ai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

2 issues found across 2 files

Prompt for AI agents (unresolved issues)

Check if these issues are valid — if so, understand the root cause of each and fix them. When an issue isn't valid or won't be fixed in this PR, reply in its thread with the reason and then resolve the thread. If appropriate, use sub-agents to investigate and fix each issue separately.


<file name="packages/tanstack/src/hooks/deferredObserverPassed.test.tsx">

<violation number="1" location="packages/tanstack/src/hooks/deferredObserverPassed.test.tsx:49">
P3: This case says a section at the viewport top does not load, but the configured `rootMargin: "300px"` makes it intersecting and the predicate loads it. Rename the case to say it tests that `bottom === 0` is not treated as “already passed” when `isIntersecting` is false.</violation>
</file>

<file name="packages/tanstack/src/hooks/DecoPageRenderer.tsx">

<violation number="1" location="packages/tanstack/src/hooks/DecoPageRenderer.tsx:275">
P3: This predicate only triggers when the section is already above the viewport at the moment the observer attaches. If browser scroll restoration (reload/back-forward) or an anchor jump lands after the one-frame rAF attach — the ordering the frame gate only guarantees for TanStack's own SPA reset — the section is below the fold when the initial callback fires (`bottom > 0`, `isIntersecting: false`), so no load happens; the restoration/jump then moves the viewport past it without crossing a threshold, and `IntersectionObserver` fires nothing further while the reader keeps scrolling down. The skeleton-stuck failure this PR fixes remains reachable in that ordering. Consider a backstop for the restore window: after `observer.observe(el)`, register a one-shot passive `scroll` listener (or check on `pageshow`) that re-evaluates `shouldLoadOnObservation` against `el.getBoundingClientRect()` and removes itself once `triggered.current` is set.</violation>
</file>

Reply with feedback, questions, or to request a fix.

Re-trigger cubic

);
});

it("does NOT load a section sitting exactly at the viewport top", () => {

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P3: This case says a section at the viewport top does not load, but the configured rootMargin: "300px" makes it intersecting and the predicate loads it. Rename the case to say it tests that bottom === 0 is not treated as “already passed” when isIntersecting is false.

Prompt for AI agents
Check if this issue is valid — if so, understand the root cause and fix it. When an issue isn't valid or won't be fixed in this PR, reply in its thread with the reason and then resolve the thread. At packages/tanstack/src/hooks/deferredObserverPassed.test.tsx, line 49:

<comment>This case says a section at the viewport top does not load, but the configured `rootMargin: "300px"` makes it intersecting and the predicate loads it. Rename the case to say it tests that `bottom === 0` is not treated as “already passed” when `isIntersecting` is false.</comment>

<file context>
@@ -0,0 +1,58 @@
+    );
+  });
+
+  it("does NOT load a section sitting exactly at the viewport top", () => {
+    // bottom === 0 is the boundary: nothing of it is visible yet, but it has
+    // not been passed either. `rootMargin: 300px` means the real observer
</file context>
Suggested change
it("does NOT load a section sitting exactly at the viewport top", () => {
it("does not treat bottom === 0 as already passed when not intersecting", () => {

isIntersecting: boolean;
boundingClientRect: { bottom: number };
}): boolean {
return entry.isIntersecting || entry.boundingClientRect.bottom < 0;

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P3: This predicate only triggers when the section is already above the viewport at the moment the observer attaches. If browser scroll restoration (reload/back-forward) or an anchor jump lands after the one-frame rAF attach — the ordering the frame gate only guarantees for TanStack's own SPA reset — the section is below the fold when the initial callback fires (bottom > 0, isIntersecting: false), so no load happens; the restoration/jump then moves the viewport past it without crossing a threshold, and IntersectionObserver fires nothing further while the reader keeps scrolling down. The skeleton-stuck failure this PR fixes remains reachable in that ordering. Consider a backstop for the restore window: after observer.observe(el), register a one-shot passive scroll listener (or check on pageshow) that re-evaluates shouldLoadOnObservation against el.getBoundingClientRect() and removes itself once triggered.current is set.

Prompt for AI agents
Check if this issue is valid — if so, understand the root cause and fix it. When an issue isn't valid or won't be fixed in this PR, reply in its thread with the reason and then resolve the thread. At packages/tanstack/src/hooks/DecoPageRenderer.tsx, line 275:

<comment>This predicate only triggers when the section is already above the viewport at the moment the observer attaches. If browser scroll restoration (reload/back-forward) or an anchor jump lands after the one-frame rAF attach — the ordering the frame gate only guarantees for TanStack's own SPA reset — the section is below the fold when the initial callback fires (`bottom > 0`, `isIntersecting: false`), so no load happens; the restoration/jump then moves the viewport past it without crossing a threshold, and `IntersectionObserver` fires nothing further while the reader keeps scrolling down. The skeleton-stuck failure this PR fixes remains reachable in that ordering. Consider a backstop for the restore window: after `observer.observe(el)`, register a one-shot passive `scroll` listener (or check on `pageshow`) that re-evaluates `shouldLoadOnObservation` against `el.getBoundingClientRect()` and removes itself once `triggered.current` is set.</comment>

<file context>
@@ -247,6 +247,34 @@ interface DeferredSectionWrapperProps {
+  isIntersecting: boolean;
+  boundingClientRect: { bottom: number };
+}): boolean {
+  return entry.isIntersecting || entry.boundingClientRect.bottom < 0;
+}
+
</file context>

@guitavano
guitavano merged commit c315c72 into main Oct 9, 2026
1 check passed
@github-actions

github-actions Bot commented Oct 9, 2026

Copy link
Copy Markdown

🎉 This PR is included in version 7.77.5 🎉

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