Skip to content

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

tyminko
Copy link

@tyminko tyminko commented Aug 10, 2025

🔧 Fix

Fixes #1918

Problem

When using image presets with defined sizes, the preset sizes are not applied if the component's sizes prop is undefined. This causes fallback to density-based srcset instead of the expected width-based srcset from presets.

Changes Made

  1. Enhanced getSizes function (src/runtime/image.ts):

    • Added proper preset merging using defu
    • Ensures preset sizes and densities are used when not overridden by component props
  2. Updated components (NuxtImg.vue, NuxtPicture.vue):

    • Only pass sizes and densities props when explicitly defined (!== undefined)
    • Prevents undefined from overriding preset values
  3. Added tests:

    • Test for preset sizes application when component sizes is undefined
    • Test for preset densities application when component densities is undefined

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

  • ✅ All existing tests pass (26/26 image tests)
  • ✅ New tests verify the fix works correctly
  • ✅ Linting passes (pnpm lint)
  • ✅ Build succeeds (pnpm build)
  • ✅ No regressions in core functionality

Breaking Changes

None. This is a bug fix that makes the behavior match the documented/expected behavior.

- 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 tyminko requested a review from danielroe as a code owner August 10, 2025 15:18
Copy link

vercel bot commented Aug 10, 2025

@tyminko is attempting to deploy a commit to the NuxtLabs Team on Vercel.

A member of the Team first needs to authorize it.

Copy link

pkg-pr-new bot commented Aug 10, 2025

Open in StackBlitz

npm i https://pkg.pr.new/@nuxt/image@1919

commit: b27e17d

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.

Preset sizes not applied when component sizes prop is undefined
1 participant