perf: serve MDX screenshots as WebP through Next's image optimizer - #2010
Draft
marcleblanc2 wants to merge 3 commits into
Draft
marcleblanc2 wants to merge 3 commits into
marcleblanc2 wants to merge 3 commits into
Conversation
ZoomableImage renders every doc image eagerly, so a long page downloads every screenshot on it before first paint. loading="lazy" defers the ones below the fold until the reader scrolls near them. Amp-Thread-ID: https://ampcode.com/threads/T-01a0b274-3173-77ed-8f71-82301b157a16 Co-authored-by: Amp <amp@ampcode.com>
Amp-Thread-ID: https://ampcode.com/threads/T-01a0b274-3173-77ed-8f71-82301b157a16 Co-authored-by: Amp <amp@ampcode.com>
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>
|
The latest updates on your projects. Learn more about Vercel for GitHub.
|
Contributor
Contributor
|
marcleblanc2
force-pushed
the
marc/perf/lazy-load-images
branch
from
September 18, 2026 04:58
3369fd5 to
f6911a6
Compare
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.
Stacked on #1975. Diff against that branch is the only thing to review here.
Problem
MDX screenshots are uncompressed PNGs on GCS (
sourcegraphstatic.com,storage.googleapis.com/sourcegraph-assets), served withmax-age=3600and no format negotiation./batch-changes/delete-a-batch-changeships 1.2 MB of PNG for four images.Those images sit behind no CDN today. Response headers show the request path is Cloudflare → Google front end → Vercel edge for HTML and static assets, but
sourcegraphstatic.comis a separate zone on Google Cloud DNS pointing straight at a GCS load balancer: no Cloudflare, no Cloud CDN, no Vercel. Vercel's image optimizer has never been used on this project (zero transformations in the billing export for August and September).Fix
ZoomableImagepoints allow-listed remote PNG/JPEGs at/_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 pass through unchanged. The zoom modal keeps the original full-resolution file.Measured with
next start, the four images on the page above:/_next/imageCost
From the Vercel billing API (
/v1/billing/charges) for theSourcegraphteam, which is on Pro:docs-legacy-versions), $0.docs/, one variant each (w=1920&q=75, WebP), refreshed weekly ≈ 1–2K transformations and ~10–20K cache-write units a month. Pro's included allotment covers that; past it, transformations are $0.05 per 1K.sourcegraph-docsruns 40–60 GB/month. Even at 500K image-page views a month (~100 GB) this stays inside the included capacity, at $0.Trade-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. The zoom modal still loads the original.sourcegraphstatic.combehind Cloudflare would be a further improvement (free egress, closer edge) but is an infra change to a shared bucket's DNS, tracked separately.Verification
tsc,eslint,cspellclean. Onnext start(earlier revision of this branch): 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. Will re-check on the Vercel preview once it deploys.