Skip to content

Commit 1f0700f

Browse files
fix: mark subtree dynamic for img with loading attribute (#14317)
* fix: mark subtree dynamic for img with loading attribute * chore: unify conditions * chore: change conditions
1 parent efc65d4 commit 1f0700f

File tree

5 files changed

+23
-6
lines changed

5 files changed

+23
-6
lines changed

.changeset/rotten-turkeys-sniff.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: mark subtree dynamic for img with loading attribute

packages/svelte/src/compiler/phases/2-analyze/visitors/Attribute.js

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,14 @@ export function Attribute(node, context) {
1818

1919
const parent = /** @type {SvelteNode} */ (context.path.at(-1));
2020

21-
// special case
22-
if (node.name === 'value') {
23-
if (parent.type === 'RegularElement' && parent.name === 'option') {
21+
if (parent.type === 'RegularElement') {
22+
// special case <option value="" />
23+
if (node.name === 'value' && parent.name === 'option') {
24+
mark_subtree_dynamic(context.path);
25+
}
26+
27+
// special case <img loading="lazy" />
28+
if (node.name === 'loading' && parent.name === 'img') {
2429
mark_subtree_dynamic(context.path);
2530
}
2631
}

packages/svelte/tests/snapshot/samples/skip-static-subtree/_expected/client/index.svelte.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import "svelte/internal/disclose-version";
22
import * as $ from "svelte/internal/client";
33

4-
var root = $.template(`<header><nav><a href="/">Home</a> <a href="/away">Away</a></nav></header> <main><h1> </h1> <div class="static"><p>we don't need to traverse these nodes</p></div> <p>or</p> <p>these</p> <p>ones</p> <!> <p>these</p> <p>trailing</p> <p>nodes</p> <p>can</p> <p>be</p> <p>completely</p> <p>ignored</p></main> <cant-skip><custom-elements></custom-elements></cant-skip> <div><input></div> <div><source></div> <select><option>a</option></select> <img src="..." alt="" loading="lazy">`, 3);
4+
var root = $.template(`<header><nav><a href="/">Home</a> <a href="/away">Away</a></nav></header> <main><h1> </h1> <div class="static"><p>we don't need to traverse these nodes</p></div> <p>or</p> <p>these</p> <p>ones</p> <!> <p>these</p> <p>trailing</p> <p>nodes</p> <p>can</p> <p>be</p> <p>completely</p> <p>ignored</p></main> <cant-skip><custom-elements></custom-elements></cant-skip> <div><input></div> <div><source></div> <select><option>a</option></select> <img src="..." alt="" loading="lazy"> <div><img src="..." alt="" loading="lazy"></div>`, 3);
55

66
export default function Skip_static_subtree($$anchor, $$props) {
77
var fragment = root();
@@ -42,8 +42,12 @@ export default function Skip_static_subtree($$anchor, $$props) {
4242
$.reset(select);
4343

4444
var img = $.sibling(select, 2);
45+
var div_2 = $.sibling(img, 2);
46+
var img_1 = $.child(div_2);
4547

48+
$.reset(div_2);
4649
$.template_effect(() => $.set_text(text, $$props.title));
4750
$.handle_lazy_img(img);
51+
$.handle_lazy_img(img_1);
4852
$.append($$anchor, fragment);
4953
}

packages/svelte/tests/snapshot/samples/skip-static-subtree/_expected/server/index.svelte.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,5 @@ import * as $ from "svelte/internal/server";
33
export default function Skip_static_subtree($$payload, $$props) {
44
let { title, content } = $$props;
55

6-
$$payload.out += `<header><nav><a href="/">Home</a> <a href="/away">Away</a></nav></header> <main><h1>${$.escape(title)}</h1> <div class="static"><p>we don't need to traverse these nodes</p></div> <p>or</p> <p>these</p> <p>ones</p> ${$.html(content)} <p>these</p> <p>trailing</p> <p>nodes</p> <p>can</p> <p>be</p> <p>completely</p> <p>ignored</p></main> <cant-skip><custom-elements with="attributes"></custom-elements></cant-skip> <div><input autofocus></div> <div><source muted></div> <select><option value="a">a</option></select> <img src="..." alt="" loading="lazy">`;
6+
$$payload.out += `<header><nav><a href="/">Home</a> <a href="/away">Away</a></nav></header> <main><h1>${$.escape(title)}</h1> <div class="static"><p>we don't need to traverse these nodes</p></div> <p>or</p> <p>these</p> <p>ones</p> ${$.html(content)} <p>these</p> <p>trailing</p> <p>nodes</p> <p>can</p> <p>be</p> <p>completely</p> <p>ignored</p></main> <cant-skip><custom-elements with="attributes"></custom-elements></cant-skip> <div><input autofocus></div> <div><source muted></div> <select><option value="a">a</option></select> <img src="..." alt="" loading="lazy"> <div><img src="..." alt="" loading="lazy"></div>`;
77
}

packages/svelte/tests/snapshot/samples/skip-static-subtree/index.svelte

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,4 +44,7 @@
4444
<option value="a">a</option>
4545
</select>
4646

47-
<img src="..." alt="" loading="lazy" />
47+
<img src="..." alt="" loading="lazy" />
48+
<div>
49+
<img src="..." alt="" loading="lazy" />
50+
</div>

0 commit comments

Comments
 (0)