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

Better workaround for lazy loading issue in Firefox #874

Merged
merged 2 commits into from
Dec 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/lemon-queens-sneeze.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@responsive-image/ember": patch
---

Better workaround for lazy loading issue in Firefox
20 changes: 2 additions & 18 deletions packages/ember/src/components/responsive-image.gts
Original file line number Diff line number Diff line change
Expand Up @@ -65,9 +65,6 @@ export default class ResponsiveImageComponent extends Component<ResponsiveImageC
@tracked
isLoaded = false;

@tracked
isRendered = false;

constructor(owner: Owner, args: ResponsiveImageComponentSignature['Args']) {
super(owner, args);
assert('No @src argument supplied for <ResponsiveImage>', args.src);
Expand Down Expand Up @@ -137,13 +134,6 @@ export default class ResponsiveImageComponent extends Component<ResponsiveImageC
* the image source which fits at best for the size and screen
*/
get src(): string | undefined {
// We *must not* set the src attribute before the <img> is actually rendered, and a child of <picture>
// Otherwise some browsers (FF, Safari) will eagerly load it, although the image isn't the one the browser
// should load given the other source/srcset variants. Also prevents native lazy loading.
if (!this.isRendered && typeof FastBoot === 'undefined') {
return undefined;
}

const url = this.args.src.imageUrlFor(this.width ?? 640);
return url;
}
Expand Down Expand Up @@ -229,22 +219,18 @@ export default class ResponsiveImageComponent extends Component<ResponsiveImageC
this.isLoaded = true;
}

@action
setRendered(): void {
this.isRendered = true;
}

<template>
<picture>
{{#each this.sourcesSorted as |s|}}
<source srcset={{s.srcset}} type={{s.mimeType}} sizes={{s.sizes}} />
{{/each}}
<img
{{! set loading before src, otherwise FF will always load eagerly! }}
loading="lazy"
src={{this.src}}
width={{this.width}}
height={{this.height}}
class={{this.classNames}}
loading="lazy"
decoding="async"
...attributes
data-ri-bh={{this.blurhashMeta.hash}}
Expand All @@ -257,8 +243,6 @@ export default class ResponsiveImageComponent extends Component<ResponsiveImageC
)
}}
{{on "load" this.onLoad}}
{{! @glint-expect-error }}
{{(this.setRendered)}}
/>
</picture>
</template>
Expand Down
Loading