Skip to content

perf(docsite): render template thumbnails without the Suspense reveal delay, and download them ahead of the scroll - #11291

Merged
nynexman4464 merged 5 commits into
mainfrom
nynexman4464/perf/template-thumbnail-preload
Nov 12, 2026
Merged

nynexman4464 merged 5 commits into
mainfrom
nynexman4464/perf/template-thumbnail-preload

Conversation

@nynexman4464

@nynexman4464 nynexman4464 commented Nov 11, 2026 •

Copy link
Copy Markdown
Contributor

Maintainer impact

Live template thumbnails fill in slowly on /templates and on any card that renders TemplateThumbnail. Each tile renders its lazy page component in a new Suspense boundary. 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_MS in react-dom, applied to retry lanes in finishConcurrentRender). 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

  • Tiles still render one at a time through thumbnailRenderScheduler, in idle time, only while they intersect the viewport (rootMargin: '0px').
  • A tile that leaves the viewport still unmounts its preview and queues again on re-entry.
  • The rendered output is the same template component in the same viewport.
  • A fast scroll through the gallery downloads no more than it does today.

Change

  • templateComponents.ts keeps one import() loader per slug (TEMPLATE_LOADERS). TEMPLATE_COMPONENTS wraps those loaders with lazy, so the preview dialog does not change. preloadTemplate(slug) starts or reuses an import, and getLoadedTemplate(slug) returns the component after the import finishes.
  • TemplateThumbnail renders 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.
  • One Effect owns the visibility observer and the near-viewport subscription. Their callbacks start, hold and cancel the download:
    • A visible tile starts its import at once, so the chunks of the visible tiles download in parallel.
    • A tile within one viewport of the screen starts its import in idle time, after every waiting render job (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.
    • During a fast scroll (above two viewport heights per second) every download waits until the scroll slows down or stops. This includes the download that a render turn starts, so a tile that leaves the lead zone before the scroll slows down downloads nothing. A tile that is still within one viewport at that time is prefetched.
    • With Save-Data or a 2G connection there is no lead: only visible tiles download.
  • templatePrefetch.ts owns one shared IntersectionObserver for 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" is main, 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:

Profile Today This PR
No throttling 2.81 s 0.81 s
4× CPU, 10 Mbps / 40 ms 11.9 s 9.0 s (7.3–10.7 s)

/templates gallery:

Scenario Today This PR
First six tiles after navigation, no throttling 6.0 s 3.6 s
First six tiles after navigation, 4× CPU, 10 Mbps / 40 ms 38.6 s (2 runs) 18.3 s (17.1–19.1 s)
Scroll one viewport at 1200 px/s: time from the end of the scroll until the nine new tiles are rendered, no throttling 3.7 s (production) 1.4 s
Same scroll on 1.6 Mbps / 150 ms, no CPU throttling, template photos served as 1080 px WebP so that they do not saturate the link 6.5 s (production) 1.9 s
Scroll to the bottom at 9000 px/s: chunk requests during the scroll 38 (449 KB, production) 33 (402 KB)
Chunk requests for the first screen with Save-Data 108 108 (131 without Save-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's TemplateThumbnail.tsx in place, 10 of the 13 tests in template-thumbnail-loading.test.tsx fail. 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) and thumbnail-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

  • No intended public API, product behavior, visual direction, or policy change.
  • Product changes discovered during the work were removed or split.
  • Public text and artifacts contain no internal Meta context.

Testing

pnpm -F @astryxdesign/docsite exec vitest run src/__tests__/template-thumbnail-loading.test.tsx src/__tests__/template-prefetch.test.ts src/__tests__/thumbnail-render-scheduler.test.ts src/__tests__/template-preview-coverage.test.ts src/__tests__/data-extraction.test.ts src/__tests__/template-gallery-performance.test.ts
pnpm -F @astryxdesign/docsite typecheck
pnpm -F @astryxdesign/docsite build

Draft: pnpm review:pre has not been run yet.

… 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.
@vercel

vercel Bot commented Nov 11, 2026 •

Copy link
Copy Markdown

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated
astryx Ready Ready Preview Nov 11, 2026 11:17pm UTC

Request Review

@meta-cla meta-cla Bot added the CLA Signed This label is managed by the Meta Open Source bot. label Nov 11, 2026
@astracat-bot

astracat-bot Bot commented Nov 11, 2026 •

Copy link
Copy Markdown
Contributor

AI review status for this pull request.

Review status Updated
🟢 Reviewed (for maintainers only) Nov 11, 2026, 11:23 PM UTC

@github-actions github-actions Bot added the needs:design-review Affects visuals — Design should review label Nov 11, 2026
@github-actions

github-actions Bot commented Nov 11, 2026 •

Copy link
Copy Markdown
Contributor

PR Analysis Report

🔎 Preview · 6a0f802

Storybook · Sandbox · built from this PR's head

Current run: The current CI run completed, but its trusted analysis is unavailable.


Generated by PR Enrichment workflow | View current CI run

@astracat-bot astracat-bot Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

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-164 only replays what the IntersectionObserver callback at :141-150 already decided: call preloadTemplate(slug) in that callback when the tile becomes visible, delete the Effect, and add slug to 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.

Automated review (maintainer link)

…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.

@astracat-bot astracat-bot Bot left a comment •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

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-152 decided, so start and clear the timer in that callback, clear it in the observer Effect's cleanup, add slug to that Effect's dependencies, and delete the Effect.
  • The description and the comments at :35 and :159-162 say tiles that only pass through the viewport during a fast scroll download nothing, but the render turn at :207-212 starts 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.tsx now 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 lazy page inside a new Suspense boundary, and the queue released only after that render committed (base TemplateThumbnail.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_COMPONENTS wraps them with lazy, and the preview dialog is unchanged (templateComponents.ts:23-140, TemplatePreviewSurface.tsx:41); the 55 slugs match the base.
  • Verified: preloadTemplate starts or reuses an import, and getLoadedTemplate returns 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 @output line.
  • 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.

Automated review (maintainer link)

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.
@nynexman4464 nynexman4464 changed the title perf(docsite): render template thumbnails without the Suspense reveal delay perf(docsite): render template thumbnails without the Suspense reveal delay, and download them ahead of the scroll Nov 11, 2026

@astracat-bot astracat-bot Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

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-155 and the near-viewport callback at :169 decided, so start, gate and cancel the import in those callbacks (one Effect owning both subscriptions, with slug in its dependencies) and delete this Effect.
  • The description and the comment at :175-176 say tiles that only pass through during a fast scroll download nothing, but the render job at :232-237 still calls preloadTemplate when a visible tile's idle turn arrives mid-scroll, so either route that import through whenScrollIsCalm and 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.

Automated review (maintainer link)

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.

@astracat-bot astracat-bot Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

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.tsx fail against main, which matches the first commit, while each of the 8 tests the file now adds asserts a call main's TemplateThumbnail never makes (for example the two preloads at line 344), so rerun the file against main'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.

Automated review (maintainer link)

@astracat-bot astracat-bot Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

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.

Automated review (maintainer link)

@nynexman4464
nynexman4464 marked this pull request as ready for review November 12, 2026 06:32
@nynexman4464
nynexman4464 merged commit 55b9032 into main Nov 12, 2026
160 of 166 checks passed
@github-actions
github-actions Bot deleted the nynexman4464/perf/template-thumbnail-preload branch November 13, 2026 06:03

This branch was successfully deployed

1 active deployment
Preview — 6a0f802e Deployed Nov 11, 2026 by vercel[bot]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

CLA Signed This label is managed by the Meta Open Source bot. needs:design-review Affects visuals — Design should review

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant