Skip to content
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 srcset width filtering #416

Merged
merged 1 commit into from
Jan 14, 2025
Merged

Conversation

bencroker
Copy link

Spent far too long troubleshooting what was going on in a site that was working fine before #407, and outputting the wrong image sizes after updating.

This PR removes the sorting of the set of srcsets to be filtered, which was causing them to get out of sync with the variantSourceWidths.

Here’s an example with 2x retina size selected and sizes [400, 500], to demonstrate the bug:

set: [400, 800, 500, 1000]
variantSourceWidths: [400, 400, 500, 500]

Becomes:

set: [400, 500, 800, 1000]
variantSourceWidths: [400, 400, 500, 500]

@bencroker bencroker requested a review from khalwat as a code owner December 29, 2024 15:15
@khalwat
Copy link
Contributor

khalwat commented Dec 29, 2024

What's the impact on #407 after removing this sorting?

@bencroker
Copy link
Author

It should still work. I don’t think the set should have been sorted in the first place, only variantSourceWidths.

@khalwat
Copy link
Contributor

khalwat commented Jan 9, 2025

So I'm having a look into this, and I'm slightly concerned that it might be a deeper problem than just removing that sorting of the sets.

Can you give me the template code you're been using that surfaced this issue?

@bencroker
Copy link
Author

The template code was {{ optimizedImages.srcsetMaxWidth(size.width) }}, where optimizedImages is an ImageOptimize field on an asset, and size.width is the max width to use.

So it could be simplified as:

{{ image.imageOptimizerField.srcsetMaxWidth(1000) }}

@khalwat
Copy link
Contributor

khalwat commented Jan 14, 2025

Merged, thanks. I'll backport it to the other versions.

Also not sure if you're aware, but I've added a nice fluent API that does a lot of this for you:

    {% set asset = entry.myAssetField.one() %}
    {{ asset.optimizedImagesField.imgTag()
        .loadingStrategy('lazy')
        .render() }}

<img> tags: https://nystudio107.com/docs/image-optimize/using.html#using-imgtag-to-create-img-tags

<picture> tags: https://nystudio107.com/docs/image-optimize/using.html#using-picturetag-to-create-picture-tags

@khalwat khalwat merged commit 6a00b33 into nystudio107:develop-v4 Jan 14, 2025
2 checks passed
@bencroker bencroker deleted the patch-1 branch January 14, 2025 13:59
@bencroker
Copy link
Author

Thanks!

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