Overriding format
and specifying srcset
-style sizes when eager loading asset transforms
#10599
-
Hi folks, I'm in the weeds a bit with eager-loading and I wanted to check whether what I'm trying to do is actually supported. My previous tactic for responsive images was to define a large number of asset transforms sizing in multiples – e.g. {# `image` is an asset already eager-loaded #}
<img srcset="
{{ image.getUrl('square960') }} {{ image.getWidth('square960') }}w,
{{ image.getUrl('square640') }} {{ image.getWidth('square640') }}w,
{{ image.getUrl('square320') }} {{ image.getWidth('square320') }}w"
src="{{ image.getUrl('square320') }}"> I refactored to use {# `image` is an asset already eager-loaded #}
<img srcset="{{ image.getSrcset(['1920w', '1600w', '1280w', '960w', '640w', '320w'], 'square') }}"
src="{{ image.getUrl('square') }}"
width="{{ image.getWidth('square') }}"
height="{{ image.getHeight('square') }}"> I then decided to refactor again to use {# `image` is an asset already eager-loaded #}
<picture>
<source srcset="{{ image.getSrcset(['1920w', '1600w', '1280w', '960w', '640w', '320w'],
{ transform: 'square', format: 'webp' }) }}"
type="image/webp">
<img srcset="{{ image.getSrcset(['1920w', '1600w', '1280w', '960w', '640w', '320w'], 'square') }}"
src="{{ image.getUrl('square') }}"
width="{{ image.getWidth('square') }}"
height="{{ image.getHeight('square') }}">
</picture> However, this has created a lot of additional database queries (the examples above are minimal cases; in practice I'm using more asset transforms and multiple images) – I assume because of the large number of transforms that result. Up to this point I've been exhaustively using eager-loading for entries and assets, but not for asset transforms. And trying to add eager-loading for the above scenario has got me a bit confused. In the section template that renders the above markup, I have an entry with a Matrix field called {% do craft.app.elements.eagerLoadElements(
className(entry),
[entry],
[
'blocks',
['blocks.standard:image', {
withTransforms: [
'square',
'1920w', '1600w', '1280w', '960w', '640w', '320w',
{ transform: 'square', format: 'webp' },
'1920w', '1600w', '1280w', '960w', '640w', '320w',
'hero',
'1920w', '1600w', '1280w', '960w', '640w', '320w',
{ transform: 'hero', format: 'webp' },
'1920w', '1600w', '1280w', '960w', '640w', '320w',
]
}],
]
) %} For clarity, what I'm trying to do with that is:
This approach is based on documentation I found on overriding named transforms, supplying hashes to The above does seem to work, in the sense that the eager-loading plan is accepted and the page is rendered, the query count seems to decrease but it's a bit tricky to work out if this is actually working completely as intended. So before I start rolling out this lengthy So I guess my question is: should the above Thanks for reading and if I can clarify anything, please ask. |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
Yep that’s exactly correct! The reason it may not be drastically reducing query count initially is because |
Beta Was this translation helpful? Give feedback.
Yep that’s exactly correct! The reason it may not be drastically reducing query count initially is because
withTransforms
will only eager-load transforms that were already generated previously. So if it’s a new asset, or you’ve recently edited/replaced the asset, or you’ve added new transform sizes, it’s expected that you will start seeing an increase in DB queries again, until all the new transforms have been generated.