Skip to content

fix: remount at any hydration error #16248

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/hip-eagles-yawn.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'svelte': patch
---

fix: remount at any hydration error
30 changes: 19 additions & 11 deletions packages/svelte/src/internal/client/render.js
Original file line number Diff line number Diff line change
Expand Up @@ -136,20 +136,28 @@ export function hydrate(component, options) {

return /** @type {Exports} */ (instance);
} catch (error) {
if (error === HYDRATION_ERROR) {
if (options.recover === false) {
e.hydration_failed();
}

// If an error occured above, the operations might not yet have been initialised.
init_operations();
clear_text_content(target);
// re-throw Svelte errors - they are certainly not related to hydration
if (
error instanceof Error &&
error.message.split('\n').some((line) => line.startsWith('https://svelte.dev/e/'))
) {
throw error;
}
if (error !== HYDRATION_ERROR) {
// eslint-disable-next-line no-console
console.warn('Failed to hydrate: ', error);
}

set_hydrating(false);
return mount(component, options);
if (options.recover === false) {
e.hydration_failed();
}

throw error;
// If an error occured above, the operations might not yet have been initialised.
init_operations();
clear_text_content(target);

set_hydrating(false);
return mount(component, options);
} finally {
set_hydrating(was_hydrating);
set_hydrate_node(previous_hydrate_node);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<p>nested</p>
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { test } from '../../test';

export default test({
errors: [
'Failed to hydrate: ',
new DOMException("Node can't be inserted in a #text parent.", 'HierarchyRequestError')
]
});
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<main><p>nested</p><!----></main>
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
<!--[-->
<main><p>nested</p><!----></main><!--]-->
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<script>
import Nested from './Nested.svelte';
</script>

<main>
<Nested />
</main>