Skip to content

Commit c450cdb

Browse files
authored
fix: better support for lazy img elements (#11545)
* fix: better support for lazy img elements * tune * fix
1 parent fcc72ae commit c450cdb

File tree

3 files changed

+19
-2
lines changed

3 files changed

+19
-2
lines changed

.changeset/rare-mirrors-act.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 support for lazy img elements

packages/svelte/src/compiler/phases/3-transform/client/visitors/template.js

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1889,11 +1889,23 @@ export const template_visitors = {
18891889
let is_content_editable = false;
18901890
let has_content_editable_binding = false;
18911891

1892-
if (is_custom_element) {
1892+
if (
18931893
// cloneNode is faster, but it does not instantiate the underlying class of the
18941894
// custom element until the template is connected to the dom, which would
18951895
// cause problems when setting properties on the custom element.
18961896
// Therefore we need to use importNode instead, which doesn't have this caveat.
1897+
is_custom_element ||
1898+
// If we have an <img loading="lazy"> occurance, we need to use importNode for FF
1899+
// otherwise, the image won't be lazy. If we detect an attribute for "loading" then
1900+
// just fallback to using importNode. Also if we have a spread attribute on the img,
1901+
// then it might contain this property, so we also need to fallback there too.
1902+
(node.name === 'img' &&
1903+
node.attributes.some(
1904+
(attribute) =>
1905+
attribute.type === 'SpreadAttribute' ||
1906+
(attribute.type === 'Attribute' && attribute.name === 'loading')
1907+
))
1908+
) {
18971909
metadata.context.template_needs_import_node = true;
18981910
}
18991911

packages/svelte/src/internal/client/dom/template.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -251,7 +251,7 @@ export function text(anchor) {
251251
return push_template_node(node);
252252
}
253253

254-
export const comment = template('<!>', TEMPLATE_FRAGMENT);
254+
export const comment = template('<!>', TEMPLATE_FRAGMENT | TEMPLATE_USE_IMPORT_NODE);
255255

256256
/**
257257
* Assign the created (or in hydration mode, traversed) dom elements to the current block

0 commit comments

Comments
 (0)