Keep loaded gallery thumbnails mounted to stop scroll-back flicker

The gallery virtualiser removed a tile's <a>/<img> from the DOM whenever it
left the viewport ± 1 buffer and rebuilt it from scratch on re-entry, so
scrolling back up replayed the thumbhash placeholder and opacity fade on
thumbnails that were already loaded - even for small albums where no network
fetch happens.

Track tile indices whose <img> has fired `load` in a bounded, LRU-ordered
`stickyTiles` set. virtualize() keeps those tiles in the "needed" set so
syncRendered never unmounts them; not-yet-loaded tiles still get the normal
lazy create/remove + data-src treatment, so large albums keep lazy-loading
on first scroll-into-view.

The set is trimmed to STICKY_TILE_LIMIT (1500) each pass - currently visible
tiles are bumped to most-recently-used and the oldest off-screen overflow is
released back to normal virtualisation - so a very large album (100k+ photos)
cannot accumulate unbounded mounted DOM or retained image memory as the user
scrolls. Cleared on a layout recompute since tile geometry changes there.
Also adds a fast-path skip in loadVisibleTiles() for finished loads now that
the rendered-tile map can be larger.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
This commit is contained in:
Vignan Posham
2026-09-07 01:10:23 -04:00
parent 5884e5afc9
commit 06741b47bb
3 changed files with 53 additions and 1 deletions
+17
View File
@@ -15,6 +15,13 @@ export const MOBILE_COLS = 3
export const IMAGE_LOAD_MARGIN_PX = 300
export const SCROLL_SETTLE_MS = 100
export const BUFFER_VIEWPORTS = 1
// Upper bound on how many already-loaded tiles we keep permanently mounted
// (see state.stickyTiles). ~1500 covers dozens of screens of scroll-back so
// revisiting recently-seen thumbnails never flickers, while capping retained
// DOM / decoded-image memory so a 100k-photo album can't grow unbounded as
// the user scrolls. Least-recently-visible tiles past this count fall back to
// normal virtualisation and re-pin if scrolled back to.
export const STICKY_TILE_LIMIT = 1500
// Height reserved for each month header (must agree with .group-header CSS)
export const HEADER_HEIGHT = 48
// Vertical gap between groups
@@ -73,6 +80,16 @@ export const state = {
lastContainerW: 0,
renderedTiles: new Map<number, HTMLAnchorElement>(),
renderedHeaders: new Map<string, HTMLElement>(),
// Tile indices whose <img> has fired `load` at least once. While an index
// is in this set virtualize() never removes its tile, so scrolling back to
// an already-seen thumbnail shows the cached image with no placeholder /
// fade-in flicker. Insertion order is maintained as most-recently-visible-
// last and the set is trimmed to STICKY_TILE_LIMIT each virtualize() pass,
// so a huge album can't accumulate unbounded mounted DOM. Tiles that have
// not loaded yet get the normal lazy create / remove treatment, so large
// albums still lazy-load on first scroll-into-view. Cleared on a layout
// recompute (see computeLayoutAndRender) because tile geometry changes.
stickyTiles: new Set<number>(),
// Selection mode (mode-toggle UX: hidden checkmarks until activated)
selectMode: false,
selected: new Set<string>(),