Skip to content

fix: better handle img loading attribute #11635

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

Merged
merged 3 commits into from
May 15, 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/eight-cougars-watch.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"svelte": patch
---

fix: better handle img loading attribute
1 change: 1 addition & 0 deletions packages/svelte/src/internal/client/constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,4 @@ export const EFFECT_TRANSPARENT = 1 << 14;
export const LEGACY_DERIVED_PROP = 1 << 15;

export const STATE_SYMBOL = Symbol('$state');
export const LOADING_ATTR_SYMBOL = Symbol('');
15 changes: 13 additions & 2 deletions packages/svelte/src/internal/client/dom/elements/attributes.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { delegate } from './events.js';
import { autofocus } from './misc.js';
import { effect, effect_root } from '../../reactivity/effects.js';
import * as w from '../../warnings.js';
import { LOADING_ATTR_SYMBOL } from '../../constants.js';

/**
* The value/checked attribute in the template actually corresponds to the defaultValue property, so we need
Expand Down Expand Up @@ -51,6 +52,11 @@ export function set_attribute(element, attribute, value) {

if (attributes[attribute] === (attributes[attribute] = value)) return;

if (attribute === 'loading') {
// @ts-expect-error
element[LOADING_ATTR_SYMBOL] = value;
}

if (value === null) {
element.removeAttribute(attribute);
} else {
Expand Down Expand Up @@ -345,10 +351,15 @@ export function handle_lazy_img(element) {
// templates.
if (!hydrating && element.loading === 'lazy') {
var src = element.src;
element.removeAttribute('loading');
// @ts-expect-error
element[LOADING_ATTR_SYMBOL] = null;
element.loading = 'eager';
element.removeAttribute('src');
requestAnimationFrame(() => {
element.loading = 'lazy';
// @ts-expect-error
if (element[LOADING_ATTR_SYMBOL] !== 'eager') {
element.loading = 'lazy';
}
element.src = src;
});
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { test } from '../../test';

export default test({
async test({ assert, target }) {
assert.htmlEqual(target.innerHTML, `<img alt="Svelte" loading="eager" src="foo.png">`);
}
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<script>
let loading = $state('lazy')

$effect(() => {
loading = 'eager'
})
</script>

<img alt="Svelte" src="foo.png" {loading} />
Loading