svelte 4 min
Why my image is not hydrating correctly when Svelte initializes
Props spreading in Svelte destroys and recreates elements at hydration. Images reloaded, layout shifted, I found the function that does it.
Preface
I am writing a Shopify theme using liquivelte which is, in essence, Liquid-rendered HTML being hydrated with Svelte.
I have added support for the image_tag filter which outputs an img tag so it is not an input-output case for the Svelte side. Normally how I handle Liquid filter counterparts on the Svelte side is like this. Let’s say there is an image_url filter
<img src="{{- somevalue | image_url: width: 300 -}}" >
liquivelte converts it to
<img src="{ liquid.image_url(somevalue, {width: 300}) }">
This is fine for input-output cases. However image_tag is different. One might say this is a more complex filter. Then I moved forward with adding an exception for image_tag and it became like this.
Let’s say we have an image tag expression:
<div class="image-container">
{{- product.images[0] | image_url: width: 300 | image_tag }}
</div>
It would output
<div class="image-container">
<img {...liquid.image_tag( liquid.image_url(product.images[0], {width: 300}), {} ) } />
</div>
I thought why not, let’s add image_tag filter as a utility function and spread props that come out of it. I did not see what was coming.
The Problem
I realized images are loading correctly, however there was flashing. Images would reload. This is a no-no, this means large layout shifts, this means bad UX. But what was wrong, I checked the network panel for a single image and it was loading exactly the same image twice. Here I am comparing 2 URLs thinking they had to be different.

I went to Svelte issues and spent some time on this better hydration issue but I was thinking “this is about iframes and stuff, images are so essential for the web it would be much more fuss if this was happening on images all the time”. Then I went back to trustworthy DevTools to find my way around. Started debugging and added a debugger; statement at the end of the image statement as an inline script. That would stop HTML parsing at that point so I know at that point the initial image is there. At this point I went to “Elements” panel and added DOM breakpoints to the element itself to find out which function is replacing or modifying the element.

Indeed I found the function that replaces the node and adds a new one. It is a core Svelte function called claim_element_base.

This function is checking if the existing element has the attributes of the Svelte counterpart of that element. If it does not have an attribute it marks it for removal.
This is what GPT has to say about it:

Conclusion
At the end of the day, the answer to this article’s question is: if you use props spreading in Svelte, your elements will get destroyed and re-created when hydrating. I now converted the conversion logic to use attributes one by one and it works like a charm.
When there is an expression on the Liquid side with an image tag I convert it to
<div>
<img src="{liquid.image_url(product.images[0], {width: 300})}" srcset="..." alt="..." />
</div>
instead of prop spreading like this:
<div class="image-container">
<img {...liquid.image_tag( liquid.image_url(product.images[0], {width: 300}), {} ) } />
</div>
Outro
I am not someone with an obsession to address layout shift issues. Layout shifts are unacceptable in today’s more and more competitive web development industry. They are one of the best known UX spoilers and we should eliminate them at whatever cost I believe.
Doing so is easier and easier nowadays. With the death of IE and browser vendors adopting improvements faster than ever, you should at least ship code with aspect-ratio CSS property to ensure things do not jump around when the page is loading or styles are applied consecutively.
Related Topics
If you care I would suggest learning these concepts for a performance competitive or UX acceptable web page.