Skip to content

Commit 49595b4

Browse files
committed
avoid getAttribute outside hydration
1 parent 1e0054e commit 49595b4

File tree

2 files changed

+22
-16
lines changed

2 files changed

+22
-16
lines changed

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

Lines changed: 20 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -38,26 +38,30 @@ export function attr_effect(dom, attribute, value) {
3838
export function attr(dom, attribute, value) {
3939
value = value == null ? null : value + '';
4040

41+
var attributes = (dom.__attributes ??= {});
42+
43+
if (hydrating) {
44+
attributes[attribute] = dom.getAttribute(attribute);
45+
}
46+
47+
if (attributes[attribute] === (attributes[attribute] = value)) return;
48+
4149
if (DEV) {
4250
check_src_in_dev_hydration(dom, attribute, value);
4351
}
4452

45-
if (
46-
!hydrating ||
47-
(dom.getAttribute(attribute) !== value &&
48-
// If we reset those, they would result in another network request, which we want to avoid.
49-
// We assume they are the same between client and server as checking if they are equal is expensive
50-
// (we can't just compare the strings as they can be different between client and server but result in the
51-
// same url, so we would need to create hidden anchor elements to compare them)
52-
attribute !== 'src' &&
53-
attribute !== 'href' &&
54-
attribute !== 'srcset')
55-
) {
56-
if (value === null) {
57-
dom.removeAttribute(attribute);
58-
} else {
59-
dom.setAttribute(attribute, value);
60-
}
53+
if (hydrating && (attribute === 'src' || attribute === 'href' || attribute === 'srcset')) {
54+
// If we reset these attributes, they would result in another network request, which we want to avoid.
55+
// We assume they are the same between client and server as checking if they are equal is expensive
56+
// (we can't just compare the strings as they can be different between client and server but result in the
57+
// same url, so we would need to create hidden anchor elements to compare them)
58+
return;
59+
}
60+
61+
if (value === null) {
62+
dom.removeAttribute(attribute);
63+
} else {
64+
dom.setAttribute(attribute, value);
6165
}
6266
}
6367

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,8 @@ export function init_operations() {
6767
text_prototype.__nodeValue = ' ';
6868
// @ts-expect-error
6969
element_prototype.__className = '';
70+
// @ts-expect-error
71+
element_prototype.__attributes = null;
7072

7173
first_child_get = /** @type {(this: Node) => ChildNode | null} */ (
7274
// @ts-ignore

0 commit comments

Comments
 (0)