Skip to content

Commit 80e30f7

Browse files
authored
fix: support hydrating elements with properties whose getters throw (#13211)
Fixes #10824 If during hydration a getter of a custom element was called, and that getter throwed, hydration would fail - this makes it not do that
1 parent 1a9d8a5 commit 80e30f7

File tree

2 files changed

+23
-1
lines changed
  • packages/svelte
    • src/internal/client/dom/elements
    • tests/hydration/samples/custom-element-with-settable-only-property

2 files changed

+23
-1
lines changed

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

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,13 @@ export function set_xlink_attribute(dom, attribute, value) {
136136
*/
137137
export function set_custom_element_data(node, prop, value) {
138138
if (prop in node) {
139-
var curr_val = node[prop];
139+
// Reading the prop could cause an error, we don't want this to fail everything else
140+
try {
141+
var curr_val = node[prop];
142+
} catch {
143+
set_attribute(node, prop, value);
144+
return;
145+
}
140146
var next_val = typeof curr_val === 'boolean' && value === '' ? true : value;
141147
if (typeof curr_val !== 'object' || curr_val !== next_val) {
142148
node[prop] = next_val;
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<script module>
2+
if (!customElements.get('custom-element-with-settable-only-property')) {
3+
customElements.define('custom-element-with-settable-only-property', class CustomElement extends HTMLElement {
4+
set prop(n) {
5+
this.value = n;
6+
}
7+
8+
get prop() {
9+
// invoking this getter shouldn't make hydration fail
10+
throw new Error('This value is not gettable');
11+
}
12+
});
13+
}
14+
</script>
15+
16+
<custom-element-with-settable-only-property prop="8"></custom-element-with-settable-only-property>

0 commit comments

Comments
 (0)