perf: lazy-load MDX images and serve them as WebP - #1975
Draft
marcleblanc2 wants to merge 2 commits into
Draft
marcleblanc2 wants to merge 2 commits into
marcleblanc2 wants to merge 2 commits into
Conversation
|
The latest updates on your projects. Learn more about Vercel for GitHub.
|
Contributor
ZoomableImage renders every doc image eagerly, so a page with several 1 MB PNGs downloads all of them before first paint.
marcleblanc2
force-pushed
the
marc/perf/lazy-load-images
branch
from
September 17, 2026 02:32
20f4f95 to
974b0fc
Compare
The docs embed ~360 PNG screenshots from GCS (sourcegraphstatic.com and storage.googleapis.com/sourcegraph-assets), uncompressed and with a one-hour cache. Route allow-listed PNG/JPEG sources through /_next/image, which converts to WebP, caps width at 1920px and caches at Vercel's edge for a week. The zoom modal keeps the original full-resolution file. Amp-Thread-ID: https://ampcode.com/threads/T-01a0ac9d-704f-73fe-8211-1b3e5840969f Co-authored-by: Amp <amp@ampcode.com>
Contributor
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
A HAR of
/docs/batch-changes/delete-a-batch-changeshows 1.2 MB of PNG screenshots. They are uncompressed PNGs on GCS (sourcegraphstatic.com,storage.googleapis.com/sourcegraph-assets) served withmax-age=3600and no WebP negotiation, andZoomableImagerenders them as a bare<img>that loads eagerly.Lazy loading alone does not help this page: it is 3748 px tall with images at 666 / 870 / 1515 / 2293 px, and Chrome starts fetching
loading="lazy"images 1250 px ahead of the viewport (2500 px on slow connections), so all four load anyway. Measured on the preview vs prod: identical image bytes (1215 KB), FCP/LCP within noise. On a long page (/code-navigation/auto-indexing, 16 images) lazy loading cuts 2.8 MB → 28 KB beforeload.Fix
loading="lazy" decoding="async"on the inline image. Saves bandwidth on long pages./_next/image: WebP, capped at 1920 px wide, cached at Vercel's edge for a week (minimumCacheTTL). The allow-list lives indocs.config.jssonext.config.js(images.remotePatterns) andZoomableImageshare it. SVG, GIF and other hosts (imgur, githubusercontent) pass through unchanged. The zoom modal keeps the original full-resolution file.Measured with
next starton this branch, the four images on the HAR page:/_next/imageTrade-offs
minimumCacheTTLto fall back to Next's 4 h default if that matters.w=1920is the largest of Next's defaultdeviceSizesthat still covers the content column on a 2x display; wider screenshots are downscaled.Verification
tsc,pnpm lint,pnpm buildclean. Onnext start: everysourcegraphstatic.comPNG on the page renders through/_next/imageasimage/webpwithCache-Control: public, max-age=604800; a PNG from a bucket outside the allow-list returns 400. Page renders unchanged (images at intrinsic size within the column).