alphagov / govuk_publishing_components

A gem to document and distribute frontend components for GOV.UK applications
https://components.publishing.service.gov.uk
MIT License
65 stars 20 forks source link

`srcset` isn't working on image card component #2644

Open maxgds opened 2 years ago

maxgds commented 2 years ago

For some reason when srcset is used assets aren't loading on the image card - this can be seen in the preview currently. Instead of trying to load something of the pattern image-randomstring.extension it tries to load image.extension, so it's probably something to do with the rails asset pipeline. Would be good to address this as the component is now used to provide the image promos on the homepage.

KludgeKML commented 2 years ago

To add a little more information - it looks like the component is expecting image urls passed to it to already be processed through (eg) the image_tag() method. We can see in frontend here:

https://github.com/alphagov/frontend/blob/main/app/views/homepage/_promotion-slots.html.erb#L26-L34

that we're passing in the image attribute like this: image_src: image_url(item[:image_src]) ...but the src_set without being passed through image_url(): srcset: item[:srcset],

if we process the srcset before calling like this: srcset: item[:srcset].to_h {|url, size| [image_url(url), size]},

...so that all images passed to the component are already passed through image_url, it works.

So I guess - should the default method of passing image urls to this (and all) components be with image_url or without?