-
Notifications
You must be signed in to change notification settings - Fork 309
fix: apply preset sizes when component sizes prop is undefined #1919
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
fix: apply preset sizes when component sizes prop is undefined #1919
Conversation
- Enhanced getSizes function to properly merge preset options using defu - Updated NuxtImg and NuxtPicture components to only pass sizes/densities when explicitly defined - Added tests to verify preset sizes and densities are correctly applied - Fixes issue where undefined props override preset values causing incorrect srcset generation Resolves density-based fallback when width-based srcset should be used from presets Fixes nuxt#1918
|
@tyminko is attempting to deploy a commit to the NuxtLabs Team on Vercel. A member of the Team first needs to authorize it. |
commit: |
| return formats.map((format) => { | ||
| const { srcset, sizes, src } = $img.getSizes(props.src!, { | ||
| ...providerOptions.value, | ||
| sizes: props.sizes || $img.options.screens, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
it looks like we lose this behaviour - should we pass screens as a fallback in getSizes?
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## main #1919 +/- ##
========================================
- Coverage 7.03% 7.02% -0.02%
========================================
Files 77 77
Lines 3567 3573 +6
Branches 138 138
========================================
Hits 251 251
- Misses 3268 3274 +6
Partials 48 48 ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
test/nuxt/picture.test.ts
Outdated
| expect(img.html()).toMatchInlineSnapshot(` | ||
| "<picture> | ||
| <source type="image/webp" sizes="(max-width: 768px) 640px, (max-width: 1024px) 768px, (max-width: 1280px) 1024px, (max-width: 1536px) 1280px, 1536px" srcset="/_ipx/f_webp&s_640x640/image.png 640w, /_ipx/f_webp&s_768x768/image.png 768w, /_ipx/f_webp&s_1024x1024/image.png 1024w, /_ipx/f_webp&s_1280x1280/image.png 1280w, /_ipx/f_webp&s_1536x1536/image.png 1536w, /_ipx/f_webp&s_2048x2048/image.png 2048w, /_ipx/f_webp&s_2560x2560/image.png 2560w, /_ipx/f_webp&s_3072x3072/image.png 3072w"><img width="200" height="200" data-nuxt-pic="" src="/_ipx/f_png&s_3072x3072/image.png" sizes="(max-width: 768px) 640px, (max-width: 1024px) 768px, (max-width: 1280px) 1024px, (max-width: 1536px) 1280px, 1536px" srcset="/_ipx/f_png&s_640x640/image.png 640w, /_ipx/f_png&s_768x768/image.png 768w, /_ipx/f_png&s_1024x1024/image.png 1024w, /_ipx/f_png&s_1280x1280/image.png 1280w, /_ipx/f_png&s_1536x1536/image.png 1536w, /_ipx/f_png&s_2048x2048/image.png 2048w, /_ipx/f_png&s_2560x2560/image.png 2560w, /_ipx/f_png&s_3072x3072/image.png 3072w"> | ||
| <source type="image/webp" srcset="/_ipx/f_webp&s_200x200/image.png 1x, /_ipx/f_webp&s_400x400/image.png 2x"><img width="200" height="200" data-nuxt-pic="" src="/_ipx/f_png&s_400x400/image.png" srcset="/_ipx/f_png&s_200x200/image.png 1x, /_ipx/f_png&s_400x400/image.png 2x"> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
added a test to show that omitting $img.screens changes behaviour
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
thank you!
happy to follow up with another PR to discuss how to use screens as a default (either in img or picture).
I'm aware that preset sizes still do not override screens (for <NuxtPicture>) and this needs more attention.
🔧 Fix
Fixes #1918
Problem
When using image presets with defined
sizes, the preset sizes are not applied if the component'ssizesprop isundefined. This causes fallback to density-based srcset instead of the expected width-based srcset from presets.Changes Made
Enhanced
getSizesfunction (src/runtime/image.ts):defusizesanddensitiesare used when not overridden by component propsUpdated components (
NuxtImg.vue,NuxtPicture.vue):sizesanddensitiesprops when explicitly defined (!== undefined)undefinedfrom overriding preset valuesAdded tests:
Before vs After
Before:
/_ipx/s_300x400/image.png 1x, /_ipx/s_600x800/image.png 2x(density-based)After:
/_ipx/s_320x427/image.png 320w, /_ipx/s_640x853/image.png 640w, /_ipx/s_1024x1365/image.png 1024w(width-based from preset)Testing
pnpm lint)pnpm build)Breaking Changes
None. This is a bug fix that makes the behavior match the documented/expected behavior.