Repository navigation
fix(tanstack): deferred section never loads when the reader is already past it - #599
Conversation
…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]>
There was a problem hiding this comment.
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", () => { |
There was a problem hiding this comment.
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>
| 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; |
There was a problem hiding this comment.
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>
|
🎉 This PR is included in version 7.77.5 🎉 The release is available on:
Your semantic-release bot 📦🚀 |
The bug
IntersectionObserverfires on entering.DeferredSectionWrapperattaches its observer one frame after hydration — deliberately, so the router's scroll reset lands first (deferredObserverTiming.test.tsxguards 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 programmaticscrollToall land below the fold. A deferred section left above the viewport then receives exactly one callback withisIntersecting: false, and never fires again while the reader keeps scrolling down.It stays on its
LoadingFallbackfor the rest of the visit.Nothing rescues it:
requestIdleCallback/visibilitychangefallback on this path;export const eager = truedoes not help — an admin ⚡ wins inshouldDeferSection(blocksresolve.ts:1688) before any code flag is consulted;<Await promise={deferredPromises[...]}>path in this same file is unreachable in 7.67.0 (nothing producesdeferredPromises—loadCmsPagereturns onlydeferredSections, 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 aty=5200with the product shelf at 3164–3829 (i.e. entirely above the viewport):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-1667renders 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:
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
shouldLoadOnObservationpurely so it can be tested — jsdom has no layout and never fires a realIntersectionObserver, 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 thebottom === 0boundary (does not count as passed).bun run --filter @decocms/tanstack typecheck→ cleanbun 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.
IntersectionObserveronly fires on entering, so a section already above the viewport when the observer attaches gets oneisIntersecting: falsecallback and never fires again — scroll restoration,#anchorlinks, and programmaticscrollToall hit this.boundingClientRect.bottom < 0(entirely above the viewport), which can't reintroduce the thundering herd the frame gate guards against.shouldLoadOnObservationso it's testable in jsdom, which never fires a realIntersectionObserver.Written for commit 141b7e3. Summary will update on new commits.