Skip to content

fix(rules): share the LCP candidate finder between lcp rules - #558

Merged
nc9 merged 5 commits into
mainfrom
claude/fix-516-lcp-candidates
Oct 8, 2026
Merged

nc9 merged 5 commits into
mainfrom
claude/fix-516-lcp-candidates

Conversation

@nc9

@nc9 nc9 commented Oct 7, 2026 •

Copy link
Copy Markdown
Contributor

Closes #516

Summary

perf/lcp-hints took the first three non-lazy images with no size check, so small header logos made it warn even when the real hero was preloaded. The better candidate finder from perf/lcp-fetchpriority now lives in packages/rules/src/shared/lcp-candidate.ts and both rules use it:

  • Eligibility: SVG/ICO, tracking pixels, images in header or nav, and images declared under 300px on both sides are not candidates. Images with no declared dimensions stay eligible.
  • Preload matching: a relative versus absolute URL, a query string or fragment difference, or a match through imagesrcset or the image's srcset counts as preloaded. A preload for another image does not.
  • perf/lcp-hints still counts up to three eligible candidates; the count is not capped at 1 here (possible follow-up, per the triage comment).
  • perf/lcp-fetchpriority now also ignores query string and fragment when matching a preload, since it shares the matcher.
  • Removed the unused validateCWVHints; nothing called it.
  • Docs: docs/rules/perf/lcp-hints.mdx describes the new candidate rules, and lcp-fetchpriority.mdx notes the shared finder and matching.

The squirrelscan agent skill needs no change: it links to the rule pages, which are the reference.

Verification

  • bun test tests/rules/lcp-hints.test.ts in apps/cli: 14 pass (reporter repro, no-preload gives 1, undimensioned pair gives 2, absolute-URL, query-string and imagesrcset preloads pass, small avatar, SVG logo and header/nav images not counted, both rules agree on the hero)
  • bun test tests/cwv-reuse.test.ts tests/lcp-fetchpriority.test.ts tests/noscript-content.test.ts in packages/rules: 115 pass
  • bun run typecheck in packages/rules and apps/cli: clean
  • make validate in docs/: all checks passed

Checklist

  • I added or updated tests for behavioral changes.
  • I did not include secrets, customer data, or incompatible third-party material.
  • Every commit includes a DCO Signed-off-by line (git commit -s).

perf/lcp-hints counted the first three non-lazy images, so small header
logos made it warn although the real hero was preloaded. It now uses the
same finder as perf/lcp-fetchpriority (skips SVG, header and nav images
and images declared under 300px), moved to a shared helper. Preload
matching ignores relative versus absolute URL and query string, and
reads imagesrcset. Removes the unused validateCWVHints. Updates the rule
docs.

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

claude Bot commented Oct 7, 2026

Copy link
Copy Markdown

Review of PR 558 (shared LCP candidate finder). I read the diff and grepped for leftover references. I did not run the tests, and CI was still pending when I checked.

The refactor is clean and the new tests cover the reported repro. Findings:

  • (low) packages/rules/src/shared/lcp-candidate.ts urlKey: dropping the query string means two different images that differ only by query (e.g. /img?id=1 and /img?id=2, common with image proxies) now count as the same image. A preload for one would clear the other. This is a false negative, not a false positive. It is an acceptable trade-off for matching ?v=2, but worth a comment in the code. It could also be limited to cases where the path has a file extension.
  • (low) srcsetKeys splits on ,, which mis-parses URLs that contain commas (e.g. Cloudinary-style /w_400,h_300/x.jpg). This was already the case in lcp-fetchpriority, but the logic now feeds lcp-hints too, so more pages are affected.
  • (low) Behaviour change for lcp-hints: SVGs, header/nav images and small declared images no longer count, and the count is no longer limited to the first 3 raw img elements. This looks intended and the docs are updated. Check that existing snapshot or fixture tests, and any score or baseline expectations, still pass once CI finishes.
  • (nit) validateCWVHints was deleted. I found no remaining references, so the removal is fine. Mention it in the PR description, since it is an export removal.
  • (nit) cwv.ts pushes img.getAttribute("src") ?? "". Candidates always have a src, so the ?? "" is dead code and could be a non-null assertion or a typed return.
  • (nit) lcp-hints.test.ts ends with an extra blank line. The formatter check may flag it.

No blocking issues found.

@claude

claude Bot commented Oct 7, 2026

Copy link
Copy Markdown

Review of #558 (shared LCP candidate finder for lcp-hints and lcp-fetchpriority).

The refactor looks sound. I found nothing that should block the merge.

  • (low) packages/rules/src/shared/lcp-candidate.ts: urlKey drops the query string, so image-proxy URLs that differ only by query (?id=1 vs ?id=2) share a key. A preload for one will suppress the warning for the other. The code comment acknowledges this, and it errs toward not warning, so it is acceptable. A test pinning the behavior would help.
  • (low) findLcpCandidates no longer includes img[fetchpriority="high"] with loading=lazy. The old lcp-hints docs said such an image qualifies, and the new rules exclude it. This is a deliberate unification, but it is a behavior change. It is worth a changelog note.
  • (nit) cwv.ts: img.getAttribute("src") as string is a cast. Having the finder return {el, src} would avoid it.
  • (nit) I did not run the test suite. I grepped for validateCWVHints and found no remaining references, so removing it looks safe. The new tests cover the matching paths well: relative vs absolute URLs, query strings, imagesrcset, header and nav, SVG, small images, and cross-rule agreement.

@claude

claude Bot commented Oct 8, 2026

Copy link
Copy Markdown

Review of #558 (shared LCP candidate finder for lcp-hints and lcp-fetchpriority).

The refactor is clean and I found no blocking problems. pageUrl is in scope in analyzeCWVHints. A grep found no remaining references to the removed validateCWVHints or to cwv-lcp-preload. I did not run the tests or CI.

  • (low) packages/rules/src/shared/lcp-candidate.ts: urlKey drops the query string, so images that differ only by query (image proxies like ?id=1 and ?id=2) share a key. A preload for one then clears the other. The code comment already says this, and it errs toward not warning. A test pinning that behaviour would help.
  • (low) packages/rules/src/performance/cwv.ts: this is a behaviour change for lcp-hints. Pages with header, nav, SVG or small images now warn less. Lazy images with fetchpriority=high no longer count, and the docs dropped that sentence. Check that the changelog or rule docs call it out.
  • (nit) cwv.ts: img.getAttribute("src") as string is safe, because the finder guarantees a src. Having findLcpCandidates return {el, src} would avoid the cast.
  • (nit) Add a test for a srcset on the image matching an imagesrcset preload, and one for an <img> inside <noscript>.

nc9 commented Oct 8, 2026 •

Copy link
Copy Markdown
Contributor Author

Re the two (low) findings: both skipped deliberately. Ignoring the query string in urlKey is the matching the issue asks for (a query-string preload counts), with the proxy trade-off documented in the code. The lcp-hints behaviour change is the point of the issue: both rules now share one candidate finder, and the rule page describes the new eligibility.

@claude

claude Bot commented Oct 8, 2026

Copy link
Copy Markdown

Review of #558 (shared LCP candidate finder for lcp-hints and lcp-fetchpriority)

The refactor looks sound. Both rules now use one finder and one preload matcher, and the tests cover the reporter repro, relative vs absolute URLs, query strings and imagesrcset. I did not run the tests. I grepped for validateCWVHints and found no remaining references, so removing it looks safe.

  • (low) shared/lcp-candidate.ts urlKey: dropping the query string means images that differ only by query (image proxies such as /_next/image?url=…) share a key, so one preload clears all of them. The comment documents this trade-off. Consider keeping the query when the path looks like a proxy endpoint, or at least add a test that pins the current behaviour.
  • (low) shared/lcp-candidate.ts srcsetKeys: splitting on "," breaks URLs that contain commas, such as Cloudinary transforms (/w_100,h_100/x.jpg). Those would produce wrong keys and cause false warnings. This was already true in the old lcp-fetchpriority code, so it is not a regression.
  • (low) cwv.ts: hints.preloadTags is still collected but no longer used for LCP matching. Check that nothing else needs it. If not, the field is only used for the "resource hints" pass check.
  • (nit) cwv.ts: getAttribute("src") as string could be avoided by having the finder return {img, src}.
  • (nit) The tests would also cover candidate-cap behaviour with a case of 4 or more unpreloaded eligible images, which should report 3.

nc9 commented Oct 8, 2026 •

Copy link
Copy Markdown
Contributor Author

Re the (low) findings on the latest head: skipped deliberately. The urlKey query trade-off is answered above. Splitting srcset on commas is pre-existing behaviour carried over from the old finder, so it is not a regression here. hints.preloadTags still feeds the resource-hints pass check, so it stays.

@nc9
nc9 merged commit 1ee0552 into main Oct 8, 2026
15 checks passed
@nc9
nc9 deleted the claude/fix-516-lcp-candidates branch October 8, 2026 03:17
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

perf/lcp-hints: lcp-preload counts small logos/avatars as likely-LCP although the hero image is preloaded

1 participant