solid
solid copied to clipboard
Cannot lazy load images in Firefox or Chrome
Describe the bug
(I'm putting this first as I think it's important pretext, sincere apologies if this throws anyone off)
FireFox and Safari have rather interesting behavior regarding setting the src
property of an image element, in that the browser will attempt to fetch the media after a microtask, regardless of whether or not the element has been (or will be!) inserted into the DOM. This is rather simple to demonstrate:
const img = document.createElement('img');
img.src = 'https://some.img.url';
Using the Network pane of a browser's devtools, you'll see the immediate fetch of the image.
FF Bugzilla Report: it's unclear whether this is a bug in FF & Safari or a spec issue, but it's been around for a while now.
Because of this, if one wants to set the image to lazy load, in Safari and FF, one needs to ensure the loading
prop is set first, else it's effectively not set at all. Preact, React, and Vue mirror vanilla JS behavior, making loading
an ordered prop in their JSX and SFCs.
As we try to decide what to do about this for Preact, I've been testing this behavior through multiple frameworks to see who accounts for this and who doesn't, but Solid's results have been exceptionally odd: lazy loading doesn't seem to work, regardless of property order, in FireFox and Chrome (tested desktop & mobile for both). However, it DOES work correctly on iOS Safari (I don't have a Mac so can't test desktop, sorry).
In the above mentioned frameworks, Chrome had been handling out-of-order properties just fine, meaning one could use <img src"..." loading="lazy" />
without any issue. I'm not quite sure what about Solid's implementation differs to cause Chrome to now have issues, or why Safari now works as expected, but something seems to be off.
This could very well be browser bug / spec issue territory, of course.
Your Example Website or App
https://github.com/rschristian/solid-lazy-img-bug
Steps to Reproduce the Bug or Issue
-
yarn
-
yarn dev
(Alternatively,yarn build && yarn preview
, issue can be reproduced in both) - Open network pane in Firefox or Chrome
- Navigate to localhost:5173
- Notice the request for
solid.svg
, despite being quite far out of the viewport
Expected behavior
The image should be lazily loaded, only being requested when near the viewport / after a scroll.
Screenshots or Videos
No response
Platform
- OS: Windows, Linux, iOS
- Browser: FF & Chrome
- Version: Latest of both
Additional context
No response