Skip to content

fix(web-vitals): correct next-gen image format detection logic#273

Merged
huang-julien merged 1 commit intonuxt:mainfrom
oritwoen:fix/web-vitals-image-format-check
Mar 11, 2026
Merged

fix(web-vitals): correct next-gen image format detection logic#273
huang-julien merged 1 commit intonuxt:mainfrom
oritwoen:fix/web-vitals-image-format-check

Conversation

@oritwoen
Copy link
Contributor

The LCP image format check warns users to serve images in next-gen formats (webp/avif), but the condition !src.includes('webp') || !src.includes('avif') is always true because no URL contains both format strings simultaneously. By De Morgan's law, !A || !B = !(A && B), so even a .webp image triggers the warning.

Changing to && makes the check work as intended: only warn when the image is neither webp nor avif.

The condition used || instead of &&, which made it always true
(a URL can never contain both 'webp' and 'avif'). This caused
false positive warnings for images already using webp or avif.
@coderabbitai
Copy link

coderabbitai bot commented Mar 11, 2026

📝 Walkthrough

Walkthrough

The change modifies the image-format validation logic in src/runtime/web-vitals/plugin.client.ts for Largest Contentful Paint (LCP) image elements. The condition was altered from a disjunction to a conjunction, shifting from triggering a warning when the source lacks webp OR avif, to triggering a warning only when the source lacks both webp AND avif. This single-line modification narrows the warning criteria to cases where neither preferred format is present.

Estimated code review effort

🎯 1 (Trivial) | ⏱️ ~3 minutes

🚥 Pre-merge checks | ✅ 3
✅ Passed checks (3 passed)
Check name Status Explanation
Title check ✅ Passed The title clearly and concisely summarizes the main change: fixing incorrect image format detection logic for LCP images.
Description check ✅ Passed The description provides a detailed explanation of the bug and the fix, correctly explaining the logical error and how it's corrected.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🧹 Nitpick comments (1)
src/runtime/web-vitals/plugin.client.ts (1)

83-84: Optional: Consider more precise format detection.

The current includes() check could theoretically match false positives in path segments (e.g., https://example.com/webpages/photo.png would match 'webp'). If you want stricter matching, you could check file extensions or use a regex pattern like /\.(webp|avif)(\?|$)/i.

That said, this is pre-existing behavior and many CDNs use format in query params (e.g., ?format=webp), so the current approach may be intentionally permissive.

🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@src/runtime/web-vitals/plugin.client.ts` around lines 83 - 84, The
string-includes checks against performanceEntry.element.src can yield false
positives (e.g., matching "webp" in a path segment); update the detection in
plugin.client.ts to use a stricter test: parse or test the URL with a regex that
matches file extensions or format query params (e.g., test /\.
(webp|avif)(\?|$)/i or check URLSearchParams for a format param) and replace the
two includes(...) checks with that regex/URL-based check on
performanceEntry.element.src to ensure only true webp/avif images are excluded.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Nitpick comments:
In `@src/runtime/web-vitals/plugin.client.ts`:
- Around line 83-84: The string-includes checks against
performanceEntry.element.src can yield false positives (e.g., matching "webp" in
a path segment); update the detection in plugin.client.ts to use a stricter
test: parse or test the URL with a regex that matches file extensions or format
query params (e.g., test /\. (webp|avif)(\?|$)/i or check URLSearchParams for a
format param) and replace the two includes(...) checks with that regex/URL-based
check on performanceEntry.element.src to ensure only true webp/avif images are
excluded.

ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: 0d1b4969-0970-46fc-9ddb-22516e72d59c

📥 Commits

Reviewing files that changed from the base of the PR and between 21d6c7f and 0d71536.

📒 Files selected for processing (1)
  • src/runtime/web-vitals/plugin.client.ts

@pkg-pr-new
Copy link

pkg-pr-new bot commented Mar 11, 2026

Open in StackBlitz

npm i https://pkg.pr.new/@nuxt/hints@273

commit: 342d38d

@huang-julien
Copy link
Member

Nice catch ! Thanks!

@huang-julien huang-julien merged commit a08b9b9 into nuxt:main Mar 11, 2026
3 checks passed
@oritwoen oritwoen deleted the fix/web-vitals-image-format-check branch March 11, 2026 11:51
@huang-julien huang-julien mentioned this pull request Mar 11, 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

Development

Successfully merging this pull request may close these issues.

2 participants