Repository navigation
perf(docsite): render template thumbnails without the Suspense reveal delay, and download them ahead of the scroll - #11291
Conversation
… delay Template thumbnails rendered their lazy page component inside a fresh Suspense boundary. Each render suspended on its chunk, showed the fallback, and React 19 then held back the reveal until 300ms after that fallback. The render queue runs one thumbnail at a time, so every tile paid at least 300ms in series. A visible thumbnail now starts its template import at once, so the chunks of all visible tiles download in parallel. Its queue turn waits for the import and renders the loaded component, which does not suspend. Cleanup releases the queue while an import is still in flight.
|
The latest updates on your projects. Learn more about Vercel for GitHub.
|
|
AI review status for this pull request.
|
PR Analysis Report🔎 Preview ·
|
There was a problem hiding this comment.
Requesting changes. The thumbnail speed-up works as described, but one new Effect has to move into the visibility callback.
- The preload Effect at
apps/docsite/src/components/TemplateThumbnail.tsx:157-164only replays what the IntersectionObserver callback at:141-150already decided: callpreloadTemplate(slug)in that callback when the tile becomes visible, delete the Effect, and addslugto the observer Effect's dependencies so the callback never preloads a stale slug.
Non-blocking: if one thumbnail instance ever receives a new slug, it keeps rendering the previous template, because the LoadedTemplate state is never cleared. Rendering getLoadedTemplate(slug) ?? Component fixes that and removes the fallback branch that never runs. CI has not run on this draft head yet.
…sible A fast scroll through the template gallery started a download for every tile it passed. A tile now starts its import after it stays visible for 150 ms. Its render turn still starts the import when it comes first.
There was a problem hiding this comment.
Requesting changes. The reveal speed-up itself is sound, but three things need fixing before approval:
- The 150 ms dwell timer is still started by an Effect (
apps/docsite/src/components/TemplateThumbnail.tsx:163-171) that only replays what the IntersectionObserver callback at:144-152decided, so start and clear the timer in that callback, clear it in the observer Effect's cleanup, addslugto that Effect's dependencies, and delete the Effect. - The description and the comments at
:35and:159-162say tiles that only pass through the viewport during a fast scroll download nothing, but the render turn at:207-212starts the import for any tile still on screen (as the second commit message and the description's own 33 → 50–52 fast-scroll row show), so either make the render turn wait for the dwell or reword the description and both comments. - The Evidence line "5 of the 8 original-plus-new tests … fail against the old component" is stale because
template-thumbnail-loading.test.tsxnow has 9 tests, so re-run it against main and state that count.
Non-blocking: LoadedTemplate is never cleared, so the <Component /> branch at :270 never runs and a same-instance slug change keeps showing the old page; rendering getLoadedTemplate(slug) ?? Component fixes both. CI has not run on this draft head yet, so approval also waits for it.
Claims and files checked
- Verified: the base thumbnail rendered the
lazypage inside a newSuspenseboundary, and the queue released only after that render committed (baseTemplateThumbnail.tsx; head:262-274). - Verified: the rendered output is the same template component in the same viewport (
:268-272). - Verified: tiles still render one at a time in idle time, only while they intersect with
rootMargin: '0px'(:153,:183,thumbnailRenderScheduler.ts). - Verified: a tile that leaves unmounts its preview and queues again on re-entry (
:148-151,:252-254, existing test). - Verified: one loader per slug,
TEMPLATE_COMPONENTSwraps them withlazy, and the preview dialog is unchanged (templateComponents.ts:23-140,TemplatePreviewSurface.tsx:41); the 55 slugs match the base. - Verified:
preloadTemplatestarts or reuses an import, andgetLoadedTemplatereturns the component once it finishes (templateComponents.ts:149-178). - Verified: a visible tile starts its import after 150 ms (
TemplateThumbnail.tsx:163-171). - False: tiles that only pass through during a fast scroll download nothing (
:207-212). - Verified: the render turn starts the import if needed, waits for it, and renders the loaded component, which does not suspend on its chunk (
:207-212,:270). - Verified: cleanup settles the queue while an import is in flight (
:190-192,:216-220, test "releases the queue when a thumbnail unmounts during its import"). - False count: "5 of the 8 original-plus-new tests" (the file has 9 tests).
- Verified by the preview deployment: the docsite production build passes on this head.
- Not verified here: the test and typecheck runs, the timing and request-count table, the 6 parallel downloads, and the same-pixels check.
- Verified: no public API change and no workflow change.
apps/docsite/src/components/TemplateThumbnail.tsx: dwell Effect, pass-through path, stale header@outputline.apps/docsite/src/components/templateComponents.ts: clear.apps/docsite/src/__tests__/template-thumbnail-loading.test.tsx: clear; the dwell test runs the render turn only after the tile has left, so it does not cover a turn that arrives while the tile is on screen.
A fixed 150 ms delay decided when a visible thumbnail downloads its template. At real scroll speeds a tile stays visible longer than that, so a fast scroll through the gallery still downloaded every template. The scroll speed now decides. A visible thumbnail starts its import at once while the page is at rest or scrolls slowly, and waits during a fast scroll. A thumbnail within one viewport of the screen starts its import in idle time, after the visible thumbnails render, so its template is ready when it scrolls in. With Save-Data or a 2G connection only visible thumbnails download.
There was a problem hiding this comment.
Requesting changes. The reveal speed-up is sound, but three things need fixing before approval:
- The new download Effect (
apps/docsite/src/components/TemplateThumbnail.tsx:172-196) only replays what the IntersectionObserver callback at:147-155and the near-viewport callback at:169decided, so start, gate and cancel the import in those callbacks (one Effect owning both subscriptions, withslugin its dependencies) and delete this Effect. - The description and the comment at
:175-176say tiles that only pass through during a fast scroll download nothing, but the render job at:232-237still callspreloadTemplatewhen a visible tile's idle turn arrives mid-scroll, so either route that import throughwhenScrollIsCalmand add a test for a visible tile during a fast scroll, or reword both. - The description still describes the 150 ms dwell commit, so update its Change, Evidence and Testing sections for this head: the one-viewport lead, the Save-Data and 2G opt-out, the scroll-speed gate, fresh measurements, the 11-test count and the new
template-prefetch.test.ts.
The earlier non-blocking note on LoadedTemplate at :295 still applies. CI has not run on this head yet.
Claims and files checked
- Claim: React holds each Suspense reveal 300 ms after the latest fallback, and the serial queue makes every tile pay it: consistent with React's source and the scheduler.
- Claim: tiles still render one at a time, in idle time, only while visible: verified.
- Claim: a tile that leaves unmounts its preview and queues again: verified.
- Claim: the same template component renders in the same viewport: verified.
- Claim: one loader per template, lazy wrappers for the dialog, and preload and loaded-component helpers: verified.
- Claim: a tile downloads only after 150 ms on screen: false at this head.
- Claim: visible tiles download in parallel: verified.
- Claim: tiles that pass through during a fast scroll download nothing: false.
- Claim: the render turn waits for the import and does not suspend: verified.
- Claim: only on-screen tiles download: incomplete, tiles within one viewport also download.
- Claim: cleanup releases the queue during an import: verified.
- Claim: 5 of 8 tests fail on the old code and all 9 pass: stale, the file has 11 tests.
- Claim: same pixels in light and dark: not checked.
- Claim: no CI, public API or internal-context change: verified.
- File
TemplateThumbnail.tsx: two blocking findings and one non-blocking note. - File
templateComponents.ts: clear. - File
templatePrefetch.ts: one non-blocking note. - File
thumbnailRenderScheduler.ts: clear. - File
template-thumbnail-loading.test.tsx: the fast-scroll test covers only nearby tiles. - File
template-prefetch.test.ts: clear. - File
thumbnail-render-scheduler.test.ts: clear.
One Effect now owns the visibility observer and the near-viewport subscription, and their callbacks start, hold and cancel the download. The Effect that replayed those callbacks a render later is gone. The render turn also holds its download during a fast scroll, so a visible tile that leaves before the scroll slows down downloads nothing. A ready tile renders the loaded page for its current slug. The scroll handler reads the viewport height on resize only.
There was a problem hiding this comment.
Requesting changes: the earlier blocking points are fixed (the download is decided in the two observer callbacks, the render turn's import waits for a calm scroll with a test for a visible tile, and the description now covers the lead zone, the data-saver opt-out and the scroll gate), and one thing remains:
- The Evidence section still says 5 of the 8 load-path tests in
template-thumbnail-loading.test.tsxfail againstmain, which matches the first commit, while each of the 8 tests the file now adds asserts a callmain'sTemplateThumbnailnever makes (for example the two preloads at line 344), so rerun the file againstmain's component and restate the count.
Optional: the comment at apps/docsite/src/components/TemplateThumbnail.tsx:231-232 and the matching description sentence could say "leaves the lead zone", since a tile still within one viewport when the scroll slows is prefetched. CI has not run on this draft head yet.
Claims and files checked
- Claim: each tile's lazy render suspends and React holds each reveal 300 ms after the latest fallback, paid in series by the one-at-a-time queue: consistent with the base component and React's work loop.
- Claim: today a tile downloads only at its render turn: verified in the base component.
- Claim: tiles still render one at a time, in idle time, only while visible with a zero root margin: verified.
- Claim: a tile that leaves unmounts its preview and queues again on re-entry: verified.
- Claim: the same template component renders in the same viewport: verified.
- Claim: a fast scroll downloads no more than today: supported by the author's request counts, not reproduced.
- Claim: one loader per template, lazy wrappers for the dialog, and preload and loaded-component helpers: verified; the 55 templates are unchanged.
- Claim: the render turn uses the loaded component and does not suspend, and cleanup releases the queue during an import: verified.
- Claim: one Effect owns both observers, and their callbacks start, hold and cancel the download: verified.
- Claim: visible tiles download at once and in parallel: verified.
- Claim: tiles within one viewport download in idle time after waiting renders: verified.
- Claim: a fast scroll holds every download, the render turn's included: verified; a tile still within one viewport when the scroll slows is then prefetched.
- Claim: no lead under Save-Data or 2G: verified.
- Claim: one shared observer, scroll listener and resize listener exist only while a thumbnail is mounted, and the viewport height is read on resize only: verified.
- Claim: the timings and request counts in the Evidence tables: not reproduced.
- Claim: 5 of the 8 load-path tests fail against main: false at this head.
- Claim: the three test files hold 13, 13 and 7 tests and cover a visible and a nearby tile during a fast scroll: counts and coverage verified; the tests were not run.
- Claim: same pixels in light and dark mode: not checked.
- Claim: no CI, public API or internal-context change: verified.
- File
TemplateThumbnail.tsx: one non-blocking wording note. - File
templateComponents.ts: clear. - File
templatePrefetch.ts: clear. - File
thumbnailRenderScheduler.ts: clear. - File
template-thumbnail-loading.test.tsx: clear; its red-first count in the description is the blocking point. - File
template-prefetch.test.ts: clear. - File
thumbnail-render-scheduler.test.ts: clear.
There was a problem hiding this comment.
Approved pending CI: CI has not run on this draft head yet, and it runs once the pull request is marked ready for review.
The last open point is fixed: the description now says 10 of the 13 tests in template-thumbnail-loading.test.tsx fail with main's component (all 8 new tests and the 2 existing tests that wait for the loaded page), which matches the test file, and the render-turn comment now names the lead zone. Nothing else blocks.
Claims and files checked
- Claim: each tile's lazy render suspends and React holds each reveal until 300 ms after the latest fallback, paid in series by the one-at-a-time queue: consistent with the base component and React 19's work loop; React's source was not reread.
- Claim: today a tile downloads only when its render turn comes: verified in the base component.
- Claim: tiles still render one at a time, in idle time, only while visible with a zero root margin: verified (
TemplateThumbnail.tsx:180,:198-246). - Claim: a tile that leaves unmounts its preview and queues again on re-entry: verified (
:174-177). - Claim: the same template component renders in the same viewport: verified (
:265,:298). - Claim: a fast scroll downloads no more than today: every download waits for a calm scroll (
:156,:159,:233); the request counts are the author's and were not reproduced. - Claim: one loader per template, lazy wrappers for the dialog, a preload helper and a loaded-component lookup: verified; the 55 templates are unchanged (
templateComponents.ts:23-178). - Claim: the render turn uses the loaded component without suspending, and cleanup settles the turn while an import is in flight: verified (
TemplateThumbnail.tsx:228-236,:240-245). - Claim: one Effect owns both observers, and their callbacks start, hold and cancel the download: verified (
:141-192). - Claim: visible tiles download at once and in parallel; tiles within one viewport download in idle time after waiting renders: verified (
:155-166,thumbnailRenderScheduler.ts:23-34). - Claim: a fast scroll holds every download, the render turn's included, and a tile still within one viewport when the scroll slows is prefetched: verified.
- Claim: no lead with Save-Data or a 2G connection: verified (
templatePrefetch.ts:24-35). - Claim: one shared observer, scroll listener and resize listener exist only while a thumbnail is mounted, and the viewport height is read on resize only: verified (
templatePrefetch.ts:99-151). - Claim: one viewport is Safari's lazy-image distance and Chrome uses 1250 px or more: browser facts, not checked.
- Claim: the timings and request counts in the Evidence tables: not reproduced.
- Claim: with
main's component, 10 of the 13 loading tests fail (all 8 new, 2 of the 5 existing): verified by reading each test against that component; not run. - Claim: the three test files hold 13, 13 and 7 tests and pass, covering a visible and a nearby tile during a fast scroll: counts and coverage verified; not run here.
- Claim: same pixels in light and dark mode: not checked.
- Claim: no CI, public API or internal-context change: verified.
- File
TemplateThumbnail.tsx: clear. - File
templateComponents.ts: clear. - File
templatePrefetch.ts: clear. - File
thumbnailRenderScheduler.ts: clear. - File
template-thumbnail-loading.test.tsx: clear. - File
template-prefetch.test.ts: clear. - File
thumbnail-render-scheduler.test.ts: clear.
Maintainer impact
Live template thumbnails fill in slowly on
/templatesand on any card that rendersTemplateThumbnail. Each tile renders its lazy page component in a newSuspenseboundary. The render suspends on the chunk and commits the fallback. React 19 then holds back the reveal until 300 ms after the most recent fallback (FALLBACK_THROTTLE_MSinreact-dom, applied to retry lanes infinishConcurrentRender). The render queue runs one tile at a time, so every tile pays at least 300 ms in series, even when its chunk arrives in 20 ms. Each tile also downloads its chunks only when its turn comes, behind the photos of the tiles before it.Measured in headless Chromium on the landing card (seven live tiles, no throttling): queue start to commit was 300–490 ms per tile, while the tile's chunks took 15–90 ms. All seven tiles took 2.8 s.
Intended invariant
thumbnailRenderScheduler, in idle time, only while they intersect the viewport (rootMargin: '0px').Change
templateComponents.tskeeps oneimport()loader per slug (TEMPLATE_LOADERS).TEMPLATE_COMPONENTSwraps those loaders withlazy, so the preview dialog does not change.preloadTemplate(slug)starts or reuses an import, andgetLoadedTemplate(slug)returns the component after the import finishes.TemplateThumbnailrenders the loaded component in its queue turn. That render does not suspend, so React does not delay the reveal. Cleanup settles the queue job while an import is still in flight, so an import that does not finish never blocks the next tile.scheduleThumbnailPrefetch, a second lane in the scheduler). Its template is then ready when it scrolls in. One viewport is the distance Safari uses for lazy images; Chrome uses 1250 px or more.Save-Dataor a 2G connection there is no lead: only visible tiles download.templatePrefetch.tsowns one sharedIntersectionObserverfor the lead zone, one passive scroll listener and one resize listener. They exist only while a thumbnail is mounted. The scroll handler reads the viewport height on resize only.Evidence
Headless Chromium (Playwright 1.61), production build (
next start), 1440×900, median of 3 runs unless stated. "Today" ismain, or the production site where marked.Landing card (seven live tiles), time from the moment the card scrolls into view to the last tile. The scroll is one jump, so the scroll-speed gate holds the downloads for its 100 ms rest window:
/templatesgallery:Save-DataSave-Data: the lead zone adds 23)Compared alternatives on the same build, behind a query switch that is not in this PR:
A fixed 150 ms visibility delay before the download: a 9000 px/s scroll still requested every template (78 requests, 990 KB).
A lead zone that downloads at once, not in idle time: the first tile after navigation rendered about 1 s later with 4× CPU and 10 Mbps (9.5 s against 8.5 s, 4 runs).
A lead zone that also renders ahead: tiles are ready before they are visible, but the first screen loads 38.6 MB of photos in place of 28.9 MB, and after a fast scroll with 4× CPU the visible tiles finished at 10.2 s in place of 4.4 s. Not in this PR.
Failure before the change: with
main'sTemplateThumbnail.tsxin place, 10 of the 13 tests intemplate-thumbnail-loading.test.tsxfail. All 8 new tests fail, because that component never calls the preload helpers. 2 of the 5 existing tests fail, because the test now tells the loaded page from the lazy wrapper and that component renders the wrapper.Success after the change:
template-thumbnail-loading.test.tsx(13),template-prefetch.test.ts(13) andthumbnail-render-scheduler.test.ts(7) pass. They cover a visible tile and a nearby tile during a fast scroll.Product/runtime behavior verified unchanged: same pixels in light and dark mode on the landing card; queue order, visibility gating, and offscreen unmount are covered by the existing tests, which still pass.
CI impact: none (no workflow change).
Scope
Components: docsite TemplateThumbnail, docsite templateComponents, docsite thumbnailRenderScheduler
Testing
Draft:
pnpm review:prehas not been run yet.