Fix srcsetWidth methods to properly handle retina variants #432
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Description
The
getSrcsetSubsetArray()method fails when retina variants (2x, 3x) are configured, causingsrcsetWidth(),srcsetWidthWebP(), and related methods to return wrong variants.Index-based mapping between
variantSourceWidths(duplicated for retina) and sortedoptimizedImageUrlskeys breaks when looking up variants.Implemented Fix
Replace fragile index mapping with direct width analysis:
What's Been Tested
srcsetWidth*(),srcsetMinWidth*(),srcsetMaxWidth*()w) vs density descriptors (x)Configuration: 5 base widths (360, 442, 466, 559, 573) × 2 retina variants = 10 total
Before Fix (Broken)
{{ optimizedImages.srcsetWidthWebP(559, true) }} → .../884px/...webp 1x, .../932px/...webp 1x {# Wrong variants! #}After Fix (Correct)
{{ optimizedImages.srcsetWidthWebP(559, true) }} → .../559px/...webp 1x, .../1118px/...webp 2x {# Correct! #} {{ optimizedImages.srcsetWidthWebP(559, false) }} → .../559px/...webp 559w {# Only 1x variant #} {{ optimizedImages.srcsetMaxWidth(466, true) }} → 360px@1x, 720px@2x, 442px@1x, 884px@2x, 466px@1x, 932px@2xRelated Issues