Skip to content

Commit 4dfad73

Browse files
committed
fix: remount at any hydration error
1 parent da2feaf commit 4dfad73

File tree

10 files changed

+52
-11
lines changed

10 files changed

+52
-11
lines changed

.changeset/hip-eagles-yawn.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: remount at any hydration error

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

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -136,20 +136,21 @@ export function hydrate(component, options) {
136136

137137
return /** @type {Exports} */ (instance);
138138
} catch (error) {
139-
if (error === HYDRATION_ERROR) {
140-
if (options.recover === false) {
141-
e.hydration_failed();
142-
}
143-
144-
// If an error occured above, the operations might not yet have been initialised.
145-
init_operations();
146-
clear_text_content(target);
139+
if (error !== HYDRATION_ERROR) {
140+
// eslint-disable-next-line no-console
141+
console.error('Failed to hydrate: ', error);
142+
}
147143

148-
set_hydrating(false);
149-
return mount(component, options);
144+
if (options.recover === false) {
145+
e.hydration_failed();
150146
}
151147

152-
throw error;
148+
// If an error occured above, the operations might not yet have been initialised.
149+
init_operations();
150+
clear_text_content(target);
151+
152+
set_hydrating(false);
153+
return mount(component, options);
153154
} finally {
154155
set_hydrating(was_hydrating);
155156
set_hydrate_node(previous_hydrate_node);
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
<p>nested</p>
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import { test } from '../../test';
2+
3+
/** @type {string[]} */
4+
let logs = [];
5+
/** @type {typeof console['error']} */
6+
let console_error;
7+
8+
export default test({
9+
before_test() {
10+
console_error = console.error;
11+
console.error = (...args) => logs.push(args.join(''));
12+
},
13+
after_test() {
14+
console.error = console_error;
15+
},
16+
test({ deepEqual }) {
17+
deepEqual(logs, [
18+
"Failed to hydrate: HierarchyRequestError: Node can't be inserted in a #text parent."
19+
]);
20+
}
21+
});
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
<main><p>nested</p><!----></main>
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
<!--[-->
2+
<main><p>nested</p><!----></main><!--]-->
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<script>
2+
import Nested from './Nested.svelte';
3+
</script>
4+
5+
<main>
6+
<Nested />
7+
</main>

packages/svelte/tests/runtime-legacy/samples/keyed-each-dev-unique/_config.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { test } from '../../test';
22

33
export default test({
4+
mode: ['server', 'client'],
45
compileOptions: {
56
dev: true
67
},

packages/svelte/tests/runtime-runes/samples/inspect-state-unsafe-mutation/_config.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { flushSync } from 'svelte';
22
import { test } from '../../test';
33

44
export default test({
5+
mode: ['client'],
56
compileOptions: {
67
dev: true
78
},

packages/svelte/tests/runtime-runes/samples/props-bound-fallback/_config.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import { test } from '../../test';
55
// uses a prop it does not write to but has a fallback value
66
export default test({
77
accessors: false, // so that prop actually becomes $.prop and not $.prop_source
8+
mode: ['server', 'client'],
89
html: `<button>0</button><span>0</span>`,
910

1011
test({ assert, target }) {

0 commit comments

Comments
 (0)