Skip to content

Commit e5e7ec2

Browse files
authored
fix: better handle img loading attribute (#11635)
* fix: better handle img loading attribute * better fix * switch to symbol
1 parent 6f28e41 commit e5e7ec2

File tree

5 files changed

+35
-2
lines changed

5 files changed

+35
-2
lines changed

.changeset/eight-cougars-watch.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"svelte": patch
3+
---
4+
5+
fix: better handle img loading attribute

packages/svelte/src/internal/client/constants.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,3 +17,4 @@ export const EFFECT_TRANSPARENT = 1 << 14;
1717
export const LEGACY_DERIVED_PROP = 1 << 15;
1818

1919
export const STATE_SYMBOL = Symbol('$state');
20+
export const LOADING_ATTR_SYMBOL = Symbol('');

packages/svelte/src/internal/client/dom/elements/attributes.js

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import { delegate } from './events.js';
66
import { autofocus } from './misc.js';
77
import { effect, effect_root } from '../../reactivity/effects.js';
88
import * as w from '../../warnings.js';
9+
import { LOADING_ATTR_SYMBOL } from '../../constants.js';
910

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

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

55+
if (attribute === 'loading') {
56+
// @ts-expect-error
57+
element[LOADING_ATTR_SYMBOL] = value;
58+
}
59+
5460
if (value === null) {
5561
element.removeAttribute(attribute);
5662
} else {
@@ -345,10 +351,15 @@ export function handle_lazy_img(element) {
345351
// templates.
346352
if (!hydrating && element.loading === 'lazy') {
347353
var src = element.src;
348-
element.removeAttribute('loading');
354+
// @ts-expect-error
355+
element[LOADING_ATTR_SYMBOL] = null;
356+
element.loading = 'eager';
349357
element.removeAttribute('src');
350358
requestAnimationFrame(() => {
351-
element.loading = 'lazy';
359+
// @ts-expect-error
360+
if (element[LOADING_ATTR_SYMBOL] !== 'eager') {
361+
element.loading = 'lazy';
362+
}
352363
element.src = src;
353364
});
354365
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import { test } from '../../test';
2+
3+
export default test({
4+
async test({ assert, target }) {
5+
assert.htmlEqual(target.innerHTML, `<img alt="Svelte" loading="eager" src="foo.png">`);
6+
}
7+
});
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<script>
2+
let loading = $state('lazy')
3+
4+
$effect(() => {
5+
loading = 'eager'
6+
})
7+
</script>
8+
9+
<img alt="Svelte" src="foo.png" {loading} />

0 commit comments

Comments
 (0)