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>
pull/16195/head
floriskn 3 months ago committed by GitHub
parent a5f500e7c0
commit 579d0e6636
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

@ -0,0 +1,5 @@
---
'svelte': patch
---
fix: remove undefined attributes on hydration

@ -345,7 +345,11 @@ export function set_attributes(element, prev, next, css_hash, skip_warning = fal
}
var prev_value = current[key];
if (value === prev_value) continue;
// Skip if value is unchanged, unless it's `undefined` and the element still has the attribute
if (value === prev_value && !(value === undefined && element.hasAttribute(key))) {
continue;
}
current[key] = value;

@ -0,0 +1,11 @@
import { test } from '../../test';
export default test({
server_props: {
browser: false
},
props: {
browser: true
}
});

@ -0,0 +1,9 @@
<script>
const { browser } = $props();
const attributes = {
"data-test": browser ? undefined : ""
};
</script>
<div {...attributes}></div>
Loading…
Cancel
Save