solid: 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
yarnyarn 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
About this issue
- Original URL
- State: open
- Created 3 years ago
- Reactions: 3
- Comments: 31 (18 by maintainers)
Commits related to this issue
- refactor `createPartialCustomElement` to `createPartialImportNode`. fix firefox has problems with `img`s and `loading="lazy"`. see https://github.com/solidjs/solid/issues/1828 — committed to potahtml/pota by titoBouzout 2 years ago
- refactor `createPartialCustomElement` to `createPartialImportNode`. fix firefox has problems with `img`s and `loading="lazy"`. see https://github.com/solidjs/solid/issues/1828 — committed to potahtml/pota by titoBouzout 2 years ago
Final going to be fixed soon in 1.9.
Fixed by commit: https://github.com/solidjs/solid/commit/2a3a1980643268b9b285976e58070ce646e09247
It looks like they are implementing my suggestion higher in the thread https://github.com/solidjs/solid/issues/1828#issuecomment-1671949231
I just have been hesitant because it means worse perf for everyone. But it is probably ultimately the right decision. We just haven’t had a minor release in Solid for a while. Not sure when I want to get it in.
I know the fix but it is slower. I didn’t want to make a call here without figuring out of this is spec or a firefox bug.
Ah good find!
Niice!
Thanks @danieltroger very useful, so uh, there is indeed a difference between
cloneNodeandimportNode. The difference with the previous code I had is that I was cloning/importing the whole template, not the first child.You can toggle
useCloneNodeon the following example (and click refresh button from the playground) to reproduce, it works withimportNode(in firefox) and doesn’t work withcloneNode(in firefox) https://playground.solidjs.com/anonymous/bc0b3503-7cc8-4d69-bdf7-099007fb3dfcIt seems to also work with dynamic values https://playground.solidjs.com/anonymous/c7cc0e4a-fdc8-4419-8906-dfcd4263ffbc
I have updated the PR to remove adding
loading=lazyby default https://github.com/ryansolid/dom-expressions/pull/374So I suppose we are good to go and this PR should in theory work.
I think the problematic use case is when
srcis static and alsoloadingis dynamic. The template becomes<img src="image.jpg"/>and the image is loaded as soon as you callimportedNode, but then it’s too late for applyingloading=lazy. For this reason, the idea is to make of the template<img loading=lazy src="image.jpg"/>when we know that the user is providing a dynamicloading. Not sure if that clarifies it. I’m not sure if I understood what you mean @ryansolidA possible issue I could think of, is when both the
srcandloadingare dynamic, IIRC the order of the props matters. Mmh, now that I think, doesn’t matter anymore with this change because the template becomes<img loading=lazy/>Yeah that would be great! Would appreciate that.
I was thinking the same, pretending
loadingis alwayslazyand then change it at runtime (as originally intended)? To clarify,<img loading={signal()} src="image.jpg"/>becomes<img loading="lazy" src="image.jpg"/>but then its changed as soon asimportNoded so there should be no noticeable different I suppose.Yeah limit of the compiler analysis. So dynamic expressions can’t be hoisted. I suppose we could just look for the loading prop in general and assume that you want to do the slower method regardless of true/false.
To be fair this issue is about whether lazy works in the given browser. My testing indicated that only Firefox wasn’t lazy loading images. That is something we are looking at fixing.
If images themselves are rendering slow that’s a different thing and probably has nothing to do with Solid. If Chrome or Safari are locking up with images it comes down to how they are rendering them because all we are doing is setting the src attribute. That could be a reason to use lazy but that is outside this issue (unless lazy isn’t working as the spec intends).
Ok I finally looked at this.
My results for your Solid example:
MacOS: Chrome - delayed Firefox - immediate Safari - delayed
Windows Chrome - delayed Firefox - immediate
Basically I only see the issue in Firefox in both OSs. In Chrome changing the order didn’t seem to matter either. But Firefox seems to always eagerly load.
I think I may have figured out the difference with Lit. They use
document.importNodeto clone instead ofelement.cloneNodethis seems to “fix” the issue in Firefox. We already do this swap when there are custom elements I suppose we could always use it instead.