Skip to content

fix(rules): count the initial viewport in content images, measure the srcset candidate, leave lazy images out of page weight - #1296

Closed
nc9 wants to merge 2 commits into
mainfrom
fix/images-lazy-srcset-1250-1251-1252
Closed

nc9 wants to merge 2 commits into
mainfrom
fix/images-lazy-srcset-1250-1251-1252

Conversation

@nc9

@nc9 nc9 commented Oct 29, 2026 •

Copy link
Copy Markdown
Contributor

Fixes #1250
Fixes #1251
Fixes #1252

What changed

One shared helper, packages/utils/src/image-loading.ts, answers "which image file does a browser download for this <img>" and "is it in the initial viewport". It reads attribute strings only, with fixed reference constants (1280 px viewport, 2x density, a budget of 3 content images, icon = declared width and height under 20,000 square pixels), and is bounded against hostile srcset and sizes values.

  • images/offscreen-lazy: top-of-page image reported below-fold after a few header icons #1250 images/offscreen-lazy: the above-the-fold budget is counted in content images. Icon-sized images (both dimensions declared) neither fill it nor count as below the fold until it is full. Undeclared images count as content, so existing behaviour holds for them.
  • images/image-file-size: measures the src fallback instead of the srcset candidate the browser selects #1251 images/image-file-size: the audit selects the srcset candidate at the reference viewport and density (largest when none covers it; src without srcset; an <img> inside <picture> keeps its src). The collector measures the selected candidate too and marks a file that is only an <img src> behind a srcset as fallbackOnly, which the rule skips. The fallback is still fetched, because images/responsive-size pairs each <img> with its src.
  • perf/total-byte-weight: loading=lazy images counted into the initial page weight #1252 perf/total-byte-weight: lazy images below the initial viewport (deferredPages per resource) are left out of a page's weight and reported as lazyImages in the details; fallbackOnly files are not counted. Eager images and lazy images in the initial viewport still count.
  • Plumbing: ImageData gains optional srcset, sizes, loading, inPicture (parser); ResourceSizeData gains optional fallbackOnly, deferredPages (core-contracts), set only when true so existing records are unchanged; the asset collector gets two optional maps (imagesSelected, imagesEager). perf/lazy-above-fold now reads the shared budget constant (no behaviour change).
  • Grep of other rules that touch these questions: perf/lazy-above-fold keeps its own hidden, carousel and footer exclusions (different question), images/lazy-loading only counts, shared/lcp-candidate and the srcset, picture-element, modern-format rules read srcset for their own purposes; none of them select the loaded file, so none changed.
  • No rule added or removed, rule count unchanged, no new option, no perRuleTally change (golden pins did not move).

One tension in the issues, worth a look

#1252 AC1 says the repro's six lazy images no longer count, AC3 says lazy images above the fold still count. The repro's first three images are in the 3-image budget, so they still count (1,200 KB) and the other three (1,200 KB) move to lazyImages. If maintainers want every loading="lazy" image excluded, that is a one-line change in the collector, but it would contradict AC3.

Tests (failing first)

  • New: utils/tests/image-loading.test.ts, rules/tests/offscreen-lazy-1250.test.ts, rules/tests/image-file-size-srcset-1251.test.ts, rules/tests/total-byte-weight-lazy-1252.test.ts, audit-engine/tests/image-load-occurrences-1250.test.ts (42 tests). With the three rule files reverted, 8 of the rule tests fail; restored, all pass.
  • Passing: those five files, lazy-above-fold, perf-total-byte-weight-per-page, rule-verdict-scope, template-verdict-inputs, template-verdict-page-independence, docs-rule-pages, docs-category-table, streaming-rules-canonical-golden, page-rule-site-context, broken-resources-fixture, offline-own-site-819, parser noscript-resources; make validate in docs/; tsgo on utils, parser, core-contracts, rules, audit-engine; test-suites.ts --check.
  • Not run: the full scripts/test-suites.ts --run rules suite, an end-to-end squirrel audit of the issue pages, a real-site smoke test. Local Bun is not the pinned 1.4.4.

Docs: docs/rules/images/offscreen-lazy.mdx, docs/rules/images/image-file-size.mdx, docs/rules/perf/total-byte-weight.mdx. Skill: none.

Review round 1 (head ede338d)

  • sizes parsing now reads parentheses as balanced groups: the source size is the trailing length or function, the media condition is what precedes it, so (min-width: 750px) calc((100vw - 130px) / 2), (max-width: 600px) calc(100vw - 20px), 400px and (min-width: 40em) 300px, 100vw give widths (575, 400, 300) instead of falling to the largest file. calc() supports px, vw, em, rem with + - * / and parentheses; an entry that cannot be read (min(), clamp(), unknown media) is skipped and the next one tried. Tests with Dawn-style sizes and the three cases above.
  • deferredPages is capped in the published report like sourcePages (capResourceSizes), tested.
  • A srcset whose selected candidate is on another origin keeps the own-host src measured and counted (it used to become fallbackOnly with nothing measured). Tested.
  • Notes: selecting a candidate adds one measured asset request per distinct selected file that differs from src (bounded by the existing resource-check budget); fallbackOnly and deferredPages live only in the in-memory run (and the published report); saveResourceSizes and a report reconstructed from storage do not keep them, so a re-rendered stored report counts every image as before.
  • perf/total-byte-weight: loading=lazy images counted into the initial page weight #1252 AC1 vs AC3: resolution recorded on the issue as a proposal (first 3 lazy images stay counted).

nc9 added 2 commits October 30, 2026 10:23
… srcset candidate, leave lazy images out of page weight #1250 #1251 #1252

Signed-off-by: Nik Cubrilovic <[email protected]>
…es, keep own-host src when the candidate is off-origin #1251 #1252

Signed-off-by: Nik Cubrilovic <[email protected]>
@nc9

nc9 commented Oct 30, 2026

Copy link
Copy Markdown
Contributor Author

Landed in v0.0.143 as e54a36d7 8f6c18bd via #1341. Release: https://github.com/squirrelscan/squirrelscan/releases/tag/v0.0.143. Closing this original.

nik

@nc9 nc9 closed this Oct 30, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

1 participant