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

Dynamically enhanced images have srcset with only 2 variants #12099

Open
vladshcherbin opened this issue Apr 10, 2024 · 4 comments
Open

Dynamically enhanced images have srcset with only 2 variants #12099

vladshcherbin opened this issue Apr 10, 2024 · 4 comments
Labels
bug Something isn't working pkg:enhanced-img

Comments

@vladshcherbin
Copy link

vladshcherbin commented Apr 10, 2024

Describe the bug

Hey 👋

I can't figure out how to dynamically enhance images properly. I've added ?enhanced part and images are processed into different formats. However, srcset has only 2 variants:

<script lang="ts">
  import bostonOverviewImage from '$lib/assets/images/boston-overview.jpg?enhanced'
</script>

<enhanced:img
  src={bostonOverviewImage}
  class="image"
  alt="boston overview"
  sizes="(width >= 700px) 500px, 100px"
/>

Images have different formats but srcset with 2 variants:

image

When importing same file directly with a path, images with multiple srcset are created:

<enhanced:img
  src="$lib/assets/images/boston-overview.jpg"
  class="image"
  alt="boston overview"
  sizes="(width >= 700px) 500px, 100px"
/>

Same image a lot more srcset variants:

image

What can be the reason?

I'd love to have the second variant but for ?enhanced images.
Tried importing using various variants from #11535 with no luck.

Reproduction

https://github.com/vladshcherbin/svelte-lab

Can be run with standard pnpm dev (or other package manager)

Logs

No response

System Info

System:
  OS: macOS 14.4.1
  CPU: (8) arm64 Apple M2
  Memory: 73.22 MB / 8.00 GB
  Shell: 5.9 - /bin/zsh
Binaries:
  Node: 20.10.0 - /opt/homebrew/opt/node@20/bin/node
  pnpm: 8.15.6 - /opt/homebrew/opt/node@20/bin/pnpm
Browsers:
  Chrome: 123.0.6312.107
npmPackages:
  @sveltejs/adapter-vercel: ^5.2.0 => 5.2.0 
  @sveltejs/enhanced-img: ^0.2.0 => 0.2.0 
  @sveltejs/kit: ^2.5.5 => 2.5.5 
  @sveltejs/vite-plugin-svelte: ^3.0.2 => 3.0.2 
  svelte: ^4.2.12 => 4.2.12 
  vite: ^5.2.8 => 5.2.8

Severity

blocking an upgrade

Additional Information

No response

@vladshcherbin
Copy link
Author

Reproduction created - https://github.com/vladshcherbin/svelte-lab

Can be run with standard pnpm dev (or other package manager)

@eltigerchino
Copy link
Member

eltigerchino commented Apr 10, 2024

You may have to add a w query parameter for sizes smaller than 540px.

The smallest picture generated automatically will have a width of 540px. If you'd like smaller images or would otherwise like to specify custom widths, you can do that with the w query parameter:

see https://kit.svelte.dev/docs/images#sveltejs-enhanced-img-srcset-and-sizes

It does seem like a bug if the srcset is different between an inline src and an imported src

@eltigerchino eltigerchino added bug Something isn't working pkg:enhanced-img labels Apr 10, 2024
@vladshcherbin
Copy link
Author

@eltigerchino yes, but sizes like 540px are never generated

I've debugged to directives, on line 76

qs.get('imgSizes') is supposed to have sizes from img tag but it's empty for ?enhanced imports.

Imports:

'$lib/assets/images/boston-overview.jpg?enhanced' - qs.get('imgSizes') is empty, even though <enhanced:img ... sizes /> has it

'$lib/assets/images/boston-overview.jpg?enhanced&imgSizes="(width >= 700px) 500px, 100px"' - qs.get('imgSizes') is populated correctly and 540px and other sizes are generated

The remaining part is to find why imgSizes is not populated from img tag 🕵️‍♂️🐛

@vladshcherbin
Copy link
Author

@benmccann can you please take a quick look, maybe there is a quick fix? I've tried to figure out with no luck 🍀

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working pkg:enhanced-img
Projects
None yet
Development

No branches or pull requests

3 participants
@vladshcherbin @eltigerchino and others