fix(08-02): retry scroll restoration across frames until layout finalizes

The window scrolls (not <main>) on this layout, and the document content
height isn't fully laid out by the first rAF after rows render — so
window.scrollTo gets clamped to maxScroll, leaving the user near top.
Retry up to 30 frames (~500ms) until the actual scroll position matches
the target within 4px.
This commit is contained in:
lorentz 2026-05-07 23:01:22 -04:00
parent 8834db981d
commit 435051ddc8

View file

@ -205,12 +205,24 @@ export default function MobileEngagementPage() {
if (!raw) { restoredScrollRef.current = true; return; }
try {
const { main: mainTop, win: winTop } = JSON.parse(raw) as { main: number; win: number };
// Defer one frame so layout has settled with the new rows in place.
requestAnimationFrame(() => {
// Retry across frames until document height supports the target scroll;
// initial render may compute heights lazily and clamp scrollTo to a small
// maxScroll. Cap attempts so we never loop forever.
let attempts = 0;
const tryRestore = () => {
const main = document.querySelector('main');
if (main && mainTop) main.scrollTop = mainTop;
if (winTop) window.scrollTo(0, winTop);
});
const winNow = window.scrollY;
const mainNow = main?.scrollTop ?? 0;
const winOk = !winTop || Math.abs(winNow - winTop) <= 4;
const mainOk = !mainTop || Math.abs(mainNow - mainTop) <= 4;
if ((!winOk || !mainOk) && attempts < 30) {
attempts++;
requestAnimationFrame(tryRestore);
}
};
requestAnimationFrame(tryRestore);
} catch {
/* corrupt entry — ignore */
}