Skip to content

perf(docsite): serve template preview images through the image optimizer - #11320

Merged
nynexman4464 merged 3 commits into
mainfrom
nynexman4464/perf/template-image-optimizer
Nov 12, 2026
Merged

nynexman4464 merged 3 commits into
mainfrom
nynexman4464/perf/template-image-optimizer

Conversation

@nynexman4464

@nynexman4464 nynexman4464 commented Nov 11, 2026 •

Copy link
Copy Markdown
Contributor

Primary intent

Serve the photos in template and block previews through the Next.js image optimizer. This is a docsite performance change: it restores no broken behavior and adds no capability, so the other templates do not fit.

User or maintainer impact

Anyone who opens /templates, the landing page, or a component page with photo examples. Previews render real templates, and each photo loads as the full-size PNG or JPEG under public/template-assets, whatever its size on screen.

Measured in headless Chromium on a production build (next start), 1440×900:

Before After
/templates first screen, image bytes (45 requests) 28,927 KiB 2,467 KiB
/templates first screen, six tiles rendered, 4× CPU, 10 Mbps / 40 ms (2 runs) 38.6 s 19.4 s
Same profile, network quiet 39.9 s 19.5 s
Landing page, total transfer on first visit 28.9 MB 21.4 MB
All 99 PNG and JPEG files under public/template-assets, fetched once each 111.0 MB 5.7 MB

Template sources do not change, and the code the site shows keeps its /template-assets/ paths. astryx template and the shadcn Install item still replace each fixture with a placeholder (spec:AST-028 FR7); their output is unchanged.

Problem and solution fit

  • Why are the pages heavy? Each preview photo is a PNG of 0.5–1.2 MB, and one gallery screen shows about 30 of them in tiles a few hundred pixels wide.
  • Why does that hurt? On a slow connection the photos saturate the link, and the code for the tiles waits behind them.
  • Why does that matter? /templates and the landing page are where people first see the templates.

One delta: a Webpack loader (scripts/template-image-loader.mjs) rewrites each PNG or JPEG fixture reference to its /_next/image URL, in the modules a preview renders only: page templates, and the block copies under src/generated/showcases and src/generated/examples. The optimizer returns the same picture as WebP at its own pixel size. It never enlarges, so a request at 1920 pixels wide keeps every image that is not wider.

The loader finds references with the CLI's own scanner, so a preview and a copy agree on what a fixture reference is. To share it, replaceDemoMedia in packages/cli/foundation/discovery/template-adapter.mjs now runs on mapFixtureRefs(source, map), which takes the replacement as a function. No behavior change in the CLI: a copy passes the same placeholders, and its tests are unchanged and pass. The Changeset is [chore] Internal.

Not in this PR: a srcset that matches the size of the scaled tile. A prototype took the same first screen to 0.25 MB, but it needs a replacement <img> component for template sources and the tile's scale passed to it. That is a separate decision.

Authority

  • spec:AST-028 FR4 (previews show the real fixture; a rewrite leaves the portable template source unchanged): the optimizer serves the same picture from the same file, and the rewrite happens in the build's copy of the module. Template sources under packages/cli/assets/templates/ are not edited.
  • spec:AST-028 FR5 (references stay deterministic and never resolve to an unrelated file): each URL names its fixture path (/_next/image?url=%2Ftemplate-assets%2F<file>) with one fixed width and quality.
  • spec:AST-028 FR6 (a missing or unsupported fixture fails visibly): the loader rewrites a reference only when public serves a file at the path. A missing file keeps its path, so the browser's failed request still names it, as today. A reference that the scanner cannot replace safely fails the build with the reference in the message, as it fails a copy. Video and vector images are not rewritten, so no fixture renders as another media kind.
  • spec:AST-033 FR23 (surfaces that hand out source): the Webpack rule matches only modules that render. The Code tab, Open in Playground, the Sandbox code view, MCP get(), and the shadcn registry read template source through other paths, and their output is unchanged.
  • The loader follows the precedent of scripts/template-icon-loader.mjs, which also rewrites preview modules with the CLI's own rewrite.

Change and evidence

  • Of the 99 raster files, 92 keep their exact pixel size. Six @2x product photos (2048 px) and building.png (6000 px) are wider than 1920 px and come back 1920 px wide.
  • Vector images, video, and paths with no file stay as written. The two does-not-exist-*.jpg fallback fixtures on /components/Avatar still fail to load, as they do today.
  • public/shadcn and the generated registries contain no /_next/image URL.
  • Across the 846 modules the rule matches, the loader rewrites 170 references in 47 modules and refuses none. It leaves Nature-1.mp4 and the two missing fallback fixtures as written.
  • No image request for a /template-assets/ PNG or JPEG remains on /templates, the product-gallery dialog, /components/Avatar (except the two fixtures) or /components/AspectRatio. Console output is unchanged.
  • WebP at quality 75 is slightly softer than the PNG when enlarged: fine grain is smoothed. Source (left) and optimized (right), 1:1 crops enlarged 2×:

Source PNG left, optimized WebP right, 2x crops

Cost: each image is transformed once per format and then cached. This PR requests one width per image, so at most 99 transformations per cache period.

Scope

Components: docsite build configuration, docsite template previews

  • This PR has one primary intent.
  • Independently removable deltas were removed or split.
  • Public text and artifacts contain no internal Meta context.

Testing

pnpm -F @astryxdesign/docsite exec vitest run src/__tests__/template-image-loader.test.ts src/__tests__/template-icon-loader.test.ts
pnpm exec vitest run --project node packages/cli/foundation/discovery/template-adapter.fixture-refs.test.mjs packages/cli/foundation/discovery/template-adapter.test.mjs packages/cli/foundation/discovery/template-adapter.demo-content.test.mjs
pnpm review:pre

template-image-loader.test.ts checks the rewrite, a reference with a query or an upper-case suffix, the references it leaves alone, that no served PNG or JPEG in the template catalog is left out, the build-cache dependencies, the Webpack rule's reach, and that the width and quality are ones the optimizer allows. template-adapter.fixture-refs.test.mjs adds two tests for mapFixtureRefs. pnpm review:pre reports 0 findings.

The byte and timing figures above come from a local production build of the first commit. The second commit changes how references are found, not which of the existing references are rewritten (170 before and after).

Preview deployment of this head (abf73d3b0d, deployment dpl_BUrX94Nv5YmWw4TbrrBmeLx8Mcgj), headless Chromium at 1440×900, every image request recorded (spec:AST-028 FR4–FR6 verification):

Page Fixture images through /_next/image Status 200 Type Direct /template-assets/ PNG or JPEG requests
/templates 31 (2,273 KiB) 31 image/webp 0
/templates?preview=product-gallery (dialog open) 32 (2,378 KiB) 32 image/webp 0
/templates?preview=login-sso (dialog open, CSS background) 32 (2,318 KiB) 32 image/webp 0

The dialog pages include the gallery tiles behind the dialog. In the product-gallery dialog all six images load through the optimizer; five reuse a URL that a tile already loaded, so the page makes one more request. In the login-sso dialog the one fixture, a CSS background (building.png), loads through the optimizer.

Not tested: Safari.

@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 12, 2026 12:11am 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 12, 2026, 12:34 AM UTC

@github-actions

github-actions Bot commented Nov 11, 2026 •

Copy link
Copy Markdown
Contributor

PR Analysis Report

🔎 Preview · abf73d3

Storybook · Sandbox · built from this PR's head

Package size: @astryxdesign/cli 3.91 MB packed (+870 B); no new dependencies.

Not run on this PR: add ci:run-visual, ci:run-a11y or ci:run-rtl to run them.


Generated by PR Enrichment workflow | View full report

Template and block previews load their photos as the full-size PNG and
JPEG files under public/template-assets. The first screen of /templates
downloads 28.9 MB of them.

A Webpack loader now addresses each of those files by its /_next/image
URL in the modules a preview renders: page templates, and the block
copies under src/generated. The optimizer returns the same picture in
the format the browser accepts, at its own pixel size up to 1920 pixels
wide. Template sources, the code the site shows, the shadcn registry,
and `astryx template` output keep their /template-assets/ paths. Vector
images, video, and paths with no file stay as written.

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

Changes requested. The loader cuts preview image weight as described, and every surface that hands out source keeps its /template-assets/ paths. Three things need fixing before approval:

  • apps/docsite/scripts/template-image-loader.mjs:24-26 adds a second fixture-reference scanner beside the CLI's replaceDemoMedia in packages/cli/foundation/discovery/template-adapter.mjs, and the two already disagree (a ?v=2 query or an upper-case .PNG counts as a fixture on copy but is left unoptimized in previews), so please export the CLI scanner as a mapper and call it from the loader, the way template-icon-loader.mjs reuses the CLI's icon rewrite.
  • The Authority section says no spec record governs docsite preview assets, but spec:AST-028 FR4–FR6 govern them (and spec:AST-033 FR23 governs the surfaces that hand out source), so please cite those claims and say how the loader meets each.
  • The verification for spec:AST-028 FR4–FR6 calls for real request checks on the pull-request preview, so please state that /templates, the product-gallery dialog and login-sso on this head's Vercel preview load every fixture from /_next/image with status 200.

CI has not run on this head yet, because CI admission reported CI full, so a re-run is also needed.

Claims and files checked
  • Previews today load each full-size fixture file: verified.
  • First-screen bytes, timings and landing-page transfer: measurements, not reproduced.
  • The 99 raster fixtures total 111.0 MB: verified; the optimized 5.7 MB total was not reproduced.
  • Template sources, displayed code, the shadcn registry and astryx template output keep their paths: verified.
  • Each preview photo is a 0.5–1.2 MB PNG: approximately true (64 of the 77 referenced photos; building.png is 4.2 MB).
  • One loader rewrites only page templates and the generated block copies: verified.
  • The optimizer returns the same picture as WebP at its own size and never enlarges: not checked against the Next.js source; consistent with the 92/7 split.
  • No spec record governs docsite preview assets: false (spec:AST-028 FR4–FR6).
  • The loader follows the icon-loader precedent: partly; both rewrite preview modules only, but the icon loader reuses the CLI's rewrite.
  • 92 of 99 fixtures keep their pixel size, and seven come back 1920 px wide: verified.
  • Vector images, video and missing files stay as written, and the two fallback fixtures still fail: verified.
  • The shadcn output and the generated registries contain no optimizer URL: verified by how they are written.
  • No fixture PNG or JPEG request remains on the listed pages: browser measurement, not reproduced.
  • WebP at quality 75 is slightly softer: consistent with the quality setting.
  • One width per image, so at most 99 transformations per cache period: verified.
  • The new test file covers what the description lists: verified.
  • Test, typecheck, build and pre-review results: not reproduced; the Vercel build of this head succeeded.
  • apps/docsite/scripts/template-image-loader.mjs: finding (shared scanner).
  • apps/docsite/next.config.mjs: clear.
  • apps/docsite/README.md: clear.
  • apps/docsite/src/__tests__/template-image-loader.test.ts: clear, apart from its part in the shared-scanner finding.
  • apps/docsite/src/__tests__/template-icon-loader.test.ts: clear.

Automated review (maintainer link)

The image loader had its own pattern for fixture references, and it
disagreed with the scanner a copy uses: a reference with a query, or an
upper-case suffix, was a fixture on copy but not in a preview.

The CLI's scanner now takes the replacement as a function
(`mapFixtureRefs`). A copy passes its placeholders, as before. The image
loader passes the optimizer URL for a PNG or JPEG value that the docsite
serves, and keeps every other reference as written.

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

Thanks for moving the loader onto the CLI's fixture scanner: a preview and a copy now agree on what a fixture reference is, which settles the earlier scanner comment. Changes are still requested for these points:

  • The description's Authority section says no spec record governs docsite preview assets, but docs/specs/AST-028/spec.md FR4–FR6 governs maintainer previews, including the docsite and pull-request previews, and docs/specs/AST-033/spec.md FR23 governs the source surfaces, so please cite them and say how the loader meets each.
  • Miss: apps/docsite/README.md lines 113–114, and the description, say the files astryx template copies keep their /template-assets/ paths, but template copy, template show and the shadcn registry replace every fixture with a placeholder (AST-028 FR7), so please say that the template sources and the code the site shows keep their paths while copies still use placeholders.
  • AST-028's verification for FR4–FR6 names real request checks on the pull-request preview, and the description says the deployed optimizer was not tested, so please load /templates, the product-gallery dialog and login-sso on this head's preview and state that every /_next/image request for a template fixture returns 200 with an image.
Claims and files checked
  • Claim, previews load the full-size committed files today: verified (the base has no rewrite).
  • Claim, the loader rewrites served PNG and JPEG fixtures only in page templates and the generated block copies: verified (next.config.mjs:47-48; test template-image-loader.test.ts:164-184).
  • Claim, the optimizer returns the same picture at its own size, never enlarged, up to 1920 px: verified against Next's defaults and the files; 92 of 99 keep their size, and six @2x photos and building.png are capped at 1920 px.
  • Claim, template sources and the code the site shows keep their paths: verified.
  • Claim, the shadcn registry and astryx template output keep their /template-assets/ paths: false; both carry placeholders.
  • Claim, vector images, video and missing files stay as written, and the Avatar fallback fixtures still fail: verified (template-image-loader.mjs:53-61; tests :51-55, :70-79).
  • Claim, public/shadcn and the generated registries contain no optimizer URL: verified by reading; they are written from disk, not through Webpack.
  • Claim, no spec record governs docsite preview assets: false (AST-028 FR4).
  • Claim, the 99 files total 111.0 MB before: the committed total is 111.0 MiB (116.4 MB); the optimized total and the timings are the author's measurements and were not reproduced.
  • Claim, each preview photo is a 0.5–1.2 MB PNG: roughly; 64 of 99 files are in that range and some referenced files are larger (building.png is 4.2 MB).
  • Claim, at most 99 transformations per cache period: holds for browsers that accept WebP (one width, default formats).
  • Claim, the tests cover the rewrite, untouched references, catalog coverage, cache dependencies, the rule's reach and the allowed width and quality: verified by reading.
  • Changeset claim, the scanner takes the replacement as a function and no command's output changes: verified (replaceDemoMedia returns the same placeholders and count).
  • File .changeset/cli-fixture-reference-mapper.md: clear.
  • File apps/docsite/README.md: finding at lines 113–114.
  • File apps/docsite/next.config.mjs: clear.
  • File apps/docsite/scripts/template-image-loader.mjs: clear.
  • File apps/docsite/src/__tests__/template-icon-loader.test.ts: clear.
  • File apps/docsite/src/__tests__/template-image-loader.test.ts: clear.
  • File packages/cli/foundation/discovery/template-adapter.fixture-refs.test.mjs: clear.
  • File packages/cli/foundation/discovery/template-adapter.mjs: clear.

Automated review (maintainer link)

Comment thread apps/docsite/README.md Resolved

@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. The README now says copies still use placeholders, the description cites docs/specs/AST-028/spec.md FR4–FR6 and docs/specs/AST-033/spec.md FR23 accurately, and it reports the pull-request-preview check that AST-028 asks for, so the three points from the last review are settled.

Claims and files checked
  • Claim, each preview photo loads today as the full-size committed file: verified (the base config has no image rule).
  • Claim, the byte and timing reductions in the measurement table: the author's measurements, not reproduced; the committed folder holds 99 PNGs totalling 111.0 MiB (116.4 MB).
  • Claim, template sources do not change and the code the site shows keeps its fixture paths: verified (next.config.mjs:47-48; shown source is read from disk into the registries).
  • Claim, astryx template and the shadcn Install item still replace each fixture with a placeholder, with unchanged output: verified (replaceDemoMedia passes the same placeholders and count, template-adapter.mjs:360-365).
  • Claim, each preview photo is a 0.5–1.2 MB PNG and a gallery screen shows about 30: roughly; 58 of the 77 referenced PNGs are in that range and 14 are larger, and the preview table counts 31 fixture requests on /templates.
  • Claim, the loader rewrites PNG and JPEG fixture references only in page templates and the generated block copies: verified (next.config.mjs:47-48, :195-207; test template-image-loader.test.ts:164-184).
  • Claim, the optimizer returns the same picture as WebP at its own size, never enlarged, up to 1920 px: verified against Next's defaults and the files.
  • Claim, the loader uses the CLI's scanner through mapFixtureRefs, with no CLI behavior change and unchanged CLI tests: verified (template-adapter.mjs:391-421; the existing tests are untouched).
  • Changeset claim, the scanner takes the replacement as a function and no command's output changes: verified.
  • Claim, the rewrite meets AST-028 FR4 by happening only in the build's copy of a module: verified.
  • Claim, each optimizer URL is deterministic, meeting AST-028 FR5: verified (template-image-loader.mjs:35-37).
  • Claim, a missing file keeps its path, a refused reference fails the build naming it, and video and vector images are not rewritten, meeting AST-028 FR6: verified (template-image-loader.mjs:53-60; template-adapter.mjs:1035, :1125).
  • Claim, surfaces that hand out source are unchanged, meeting AST-033 FR23: verified (CI's docsite build of this head has no optimizer URL in the shadcn output or the generated registries).
  • Claim, the loader follows template-icon-loader.mjs, which also reuses CLI code: verified.
  • Claim, 92 of the 99 raster files keep their size and six @2x photos and building.png come back 1920 px wide: verified from the file headers.
  • Claim, vector images, video and missing paths stay as written and the two Avatar fallback fixtures still fail to load: verified (tests :51-55, :70-79; CI's docsite build).
  • Claim, the loader rewrites 170 references in 47 modules and refuses none: not reproduced exactly; consistent with CI's passing docsite build, which addresses 77 distinct fixtures.
  • Claim, no direct fixture PNG request remains on the named pages and console output is unchanged: the author's local measurement; consistent with CI's docsite build.
  • Claim, WebP at quality 75 is slightly softer when enlarged: the author's crops; not reproduced.
  • Claim, at most 99 transformations per cache period: holds per output format, since one width is requested.
  • Claim, the new tests cover the rewrite, untouched references, catalog coverage, cache dependencies, the rule's reach and the allowed width and quality, and the CLI test adds two mapFixtureRefs tests: verified by reading.
  • Claim, on the pull-request preview every fixture request goes through /_next/image and returns 200 as WebP, with no direct PNG request: the author's statement for the previous commit, whose built code equals this head's (the later commit edits only the docsite README).
  • Claim, the second commit does not change which references are rewritten: consistent with the shared scanner and the catalog test.
  • File .changeset/cli-fixture-reference-mapper.md: clear.
  • File apps/docsite/README.md: clear.
  • File apps/docsite/next.config.mjs: clear.
  • File apps/docsite/scripts/template-image-loader.mjs: clear.
  • File apps/docsite/src/__tests__/template-icon-loader.test.ts: clear.
  • File apps/docsite/src/__tests__/template-image-loader.test.ts: clear.
  • File packages/cli/foundation/discovery/template-adapter.fixture-refs.test.mjs: clear.
  • File packages/cli/foundation/discovery/template-adapter.mjs: clear.

Automated review (maintainer link)

@nynexman4464
nynexman4464 merged commit d084760 into main Nov 12, 2026
68 checks passed
@github-actions
github-actions Bot deleted the nynexman4464/perf/template-image-optimizer branch November 12, 2026 06:06

This branch was successfully deployed

1 active deployment
Preview — abf73d3b Deployed Nov 12, 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.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant