Delay setting src attribute after <img> is in DOM to prevent extraneous image loading #176
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This is an interesting problem: some browsers (FF, Safari, but not Chrome) would load an image immediately once you set the
src
attribute of<img>
. While this is expected (think of programmatically preloading images), it poses a problem when you actually want to use the<img>
as a child of<picture>
, as thesrc
attribute represents the fallback for browsers not supporting<source>
.But this is actually what happens if you render the markup in Ember: it creates the element detached from DOM (
document.createElement()
), sets the attributes (element.setAttribute()
) and only after that attaches it to its parent. Chrome handles this more cleverly, but FF would (upon settingsrc
) load the fallback image (jpeg), later sees the better<source>
alternative (e.g. webp) and would load that also. Note that this does not happen, when the<img>
is already part of<picture>
, as the browser probably can understand now that thesrc
attribute is not the only possible image URL.Also all of that does not happen in a server-rendered HTML page (FastBoot, or any other server-rendered page), only when JS/Ember re-renders the page with JavaScript would that wrong behaviour show up again.
The solution seems simple enough: delay setting
src
until the<img>
is in DOM and part of<picture>
.