Skip to content

Commit 29ce686

Browse files
committed
tune
1 parent 4f8fee4 commit 29ce686

File tree

4 files changed

+15
-12
lines changed

4 files changed

+15
-12
lines changed

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2034,9 +2034,9 @@ export const template_visitors = {
20342034
}
20352035
}
20362036

2037-
// For FF we need to apply the src and loading attributes after the element is appended to the document
2037+
// Apply the src and loading attributes for <img> elements after the element is appended to the document
20382038
if (img_might_be_lazy) {
2039-
context.state.after_update.push(b.stmt(b.call('$.handle_ff_lazy_img', node_id)));
2039+
context.state.after_update.push(b.stmt(b.call('$.handle_lazy_img', node_id)));
20402040
}
20412041

20422042
// class/style directives must be applied last since they could override class/style attributes

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

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,9 @@ export function set_attribute(element, attribute, value) {
3939
attributes[attribute] = element.getAttribute(attribute);
4040

4141
if (attribute === 'src' || attribute === 'href' || attribute === 'srcset') {
42-
check_src_in_dev_hydration(element, attribute, value);
42+
if (attribute === 'src' && /** @type {HTMLImageElement} */ (element).loading !== 'lazy') {
43+
check_src_in_dev_hydration(element, attribute, value);
44+
}
4345

4446
// If we reset these attributes, they would result in another network request, which we want to avoid.
4547
// We assume they are the same between client and server as checking if they are equal is expensive
@@ -338,16 +340,17 @@ function srcset_url_equal(element, srcset) {
338340
* @param {HTMLImageElement} element
339341
* @returns {void}
340342
*/
341-
export function handle_ff_lazy_img(element) {
342-
// If we're using Firefox and the image has a lazy loading attribute, we need to
343-
// apply this attribute, along with the src, after it has been appended to the document
344-
// otherwise the lazy behaviour will not work due to our cloneNode heuristic for templates.
345-
if (/Firefox/.test(navigator.userAgent) && element.getAttribute('loading') === 'lazy') {
343+
export function handle_lazy_img(element) {
344+
// If we're using an image that has a lazy loading attribute, we need to apply
345+
// the loading and src after the img element has been appended to the document.
346+
// Otherwise the lazy behaviour will not work due to our cloneNode heuristic for
347+
// templates.
348+
if (element.loading === 'lazy') {
346349
var src = element.src;
347350
element.removeAttribute('loading');
348351
element.removeAttribute('src');
349-
requestIdleCallback(() => {
350-
element.setAttribute('loading', 'lazy');
352+
requestAnimationFrame(() => {
353+
element.loading = 'lazy';
351354
element.src = src;
352355
});
353356
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ export function hydrate_anchor(node) {
4040
var current = /** @type {Node | null} */ (node);
4141

4242
// TODO this could have false positives, if a user comment consisted of `[`. need to tighten that up
43-
if (/** @type {Comment} */ (current)?.data !== HYDRATION_START) {
43+
if (/** @type {Comment} */ (current).data !== HYDRATION_START) {
4444
return node;
4545
}
4646

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ export {
2727
set_custom_element_data,
2828
set_dynamic_element_attributes,
2929
set_xlink_attribute,
30-
handle_ff_lazy_img
30+
handle_lazy_img
3131
} from './dom/elements/attributes.js';
3232
export { set_class, set_svg_class, set_mathml_class, toggle_class } from './dom/elements/class.js';
3333
export { event, delegate } from './dom/elements/events.js';

0 commit comments

Comments
 (0)