Skip to content

Commit bbd0b1e

Browse files
authored
fix: remount at any hydration error (#16248)
* fix: remount at any hydration error * re-throw svelte error immediately * log error as warn * fix?
1 parent d8c3f3d commit bbd0b1e

File tree

7 files changed

+43
-11
lines changed

7 files changed

+43
-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: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -136,20 +136,28 @@ 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+
// re-throw Svelte errors - they are certainly not related to hydration
140+
if (
141+
error instanceof Error &&
142+
error.message.split('\n').some((line) => line.startsWith('https://svelte.dev/e/'))
143+
) {
144+
throw error;
145+
}
146+
if (error !== HYDRATION_ERROR) {
147+
// eslint-disable-next-line no-console
148+
console.warn('Failed to hydrate: ', error);
149+
}
147150

148-
set_hydrating(false);
149-
return mount(component, options);
151+
if (options.recover === false) {
152+
e.hydration_failed();
150153
}
151154

152-
throw error;
155+
// If an error occured above, the operations might not yet have been initialised.
156+
init_operations();
157+
clear_text_content(target);
158+
159+
set_hydrating(false);
160+
return mount(component, options);
153161
} finally {
154162
set_hydrating(was_hydrating);
155163
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: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import { test } from '../../test';
2+
3+
export default test({
4+
errors: [
5+
'Failed to hydrate: ',
6+
new DOMException("Node can't be inserted in a #text parent.", 'HierarchyRequestError')
7+
]
8+
});
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>

0 commit comments

Comments
 (0)