fix(nuxt-img): set imagesrcset preload when densities are set w/o sizes#2176
fix(nuxt-img): set imagesrcset preload when densities are set w/o sizes#2176
imagesrcset preload when densities are set w/o sizes#2176Conversation
…mages and add tests for image preload attributes
commit: |
| }) | ||
|
|
||
| expect(roundToKilobytes(withImage.totalBytes - withoutImage.totalBytes)).toMatchInlineSnapshot(`"12.6k"`) | ||
| expect(roundToKilobytes(withImage.totalBytes - withoutImage.totalBytes)).toMatchInlineSnapshot(`"12.2k"`) |
There was a problem hiding this comment.
Not really sure why this test failed for me locally, or why even it changed.
There was a problem hiding this comment.
Just saw now that it fails now in github pipeline, I think I will just revert this change.
| }) | ||
| } | ||
|
|
||
| it('preloads image with correct attributes', async () => { |
There was a problem hiding this comment.
Not sure if this is the best way to write this test, but I think it is important to have it.
📝 WalkthroughWalkthroughThis pull request introduces changes to image preloading behavior in a Nuxt image component library. A new playground page component ( Estimated code review effort🎯 3 (Moderate) | ⏱️ ~25 minutes ContextThe changes span four files with mixed scope: a new 40-line template component, a single-line conditional logic change in the core component, a 24-line e2e test case, and a 1-line bundle size snapshot update. The primary review focus should be on understanding the rationale for changing from 🚥 Pre-merge checks | ✅ 3✅ Passed checks (3 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches🧪 Generate unit tests (beta)
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. Comment |
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #2176 +/- ##
=======================================
Coverage 32.52% 32.52%
=======================================
Files 7 7
Lines 372 372
Branches 131 131
=======================================
Hits 121 121
Misses 194 194
Partials 57 57 ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
|
Hello @danielroe, Please have a quick look over this one, curious if this PR makes sense for you and if it could be part of the next release. Thanks |
NuxtImg to correctly set imagesrcset for responsive images and add tests for image preload attributesimagesrcset preload when densities are set w/o sizes
🔗 Linked issue
no issue
📚 Description
We have a production issue where a preloaded image can be fetched twice.
When sizes is specified, the generated srcset uses width descriptors (w) instead of density descriptors (x), so hasMultipleDensities becomes false. As a result, Nuxt Image generates a
<link rel="preload">with imagesizes but without imagesrcset.According to the web.dev article on preloading responsive images, responsive image preloads should mirror the
attributes: src → href, srcset → imagesrcset, and sizes → imagesizes. The browser uses imagesrcset and imagesizes together for the same resource selection logic as srcset and sizes on
<img>This change ensures that whenever the preload represents a responsive image, we also emit imagesrcset, not only when the srcset contains density descriptors. That keeps the preload aligned with the eventual
<img>request and avoids duplicate downloads.I also added tests for this but not sure if they are added properly.