Skip to content

Commit 579d0e6

Browse files
fix: ensure undefined attributes are removed during hydration (#16178)
* fix: ensure undefined attributes are removed during hydration Attributes that are `undefined` on the client should be removed during hydration, even if their value hasn't changed compared to `prev_value`. * Create plenty-wasps-sleep.md * added test * Update .changeset/plenty-wasps-sleep.md --------- Co-authored-by: Rich Harris <hello@rich-harris.dev>
1 parent a5f500e commit 579d0e6

File tree

5 files changed

+31
-1
lines changed

5 files changed

+31
-1
lines changed

.changeset/plenty-wasps-sleep.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: remove undefined attributes on hydration

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

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -345,7 +345,11 @@ export function set_attributes(element, prev, next, css_hash, skip_warning = fal
345345
}
346346

347347
var prev_value = current[key];
348-
if (value === prev_value) continue;
348+
349+
// Skip if value is unchanged, unless it's `undefined` and the element still has the attribute
350+
if (value === prev_value && !(value === undefined && element.hasAttribute(key))) {
351+
continue;
352+
}
349353

350354
current[key] = value;
351355

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import { test } from '../../test';
2+
3+
export default test({
4+
server_props: {
5+
browser: false
6+
},
7+
8+
props: {
9+
browser: true
10+
}
11+
});
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
<!--[--><div></div><!--]-->
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<script>
2+
const { browser } = $props();
3+
4+
const attributes = {
5+
"data-test": browser ? undefined : ""
6+
};
7+
</script>
8+
9+
<div {...attributes}></div>

0 commit comments

Comments
 (0)