Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Delay setting src attribute after <img> is in DOM to prevent extraneous image loading #176

Merged
merged 1 commit into from
Feb 23, 2021

Conversation

simonihmig
Copy link
Owner

@simonihmig simonihmig commented Feb 23, 2021

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 the src 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 setting src) 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 the src 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>.

This is an interesting problem: some browsers (FF, Safari, but not Chrome) would load an image immediately one 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 the `src` 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 setting `src`) 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 the `src` 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>`.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

1 participant