fix: remount at any hydration error

pull/16248/head
7nik 3 months ago
parent da2feafe67
commit 4dfad73786

@ -0,0 +1,5 @@
---
'svelte': patch
---
fix: remount at any hydration error

@ -136,20 +136,21 @@ 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);
if (error !== HYDRATION_ERROR) {
// eslint-disable-next-line no-console
console.error('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);

@ -0,0 +1,21 @@
import { test } from '../../test';
/** @type {string[]} */
let logs = [];
/** @type {typeof console['error']} */
let console_error;
export default test({
before_test() {
console_error = console.error;
console.error = (...args) => logs.push(args.join(''));
},
after_test() {
console.error = console_error;
},
test({ deepEqual }) {
deepEqual(logs, [
"Failed to hydrate: HierarchyRequestError: Node can't be inserted in a #text parent."
]);
}
});

@ -0,0 +1,2 @@
<!--[-->
<main><p>nested</p><!----></main><!--]-->

@ -0,0 +1,7 @@
<script>
import Nested from './Nested.svelte';
</script>
<main>
<Nested />
</main>

@ -1,6 +1,7 @@
import { test } from '../../test';
export default test({
mode: ['server', 'client'],
compileOptions: {
dev: true
},

@ -2,6 +2,7 @@ import { flushSync } from 'svelte';
import { test } from '../../test';
export default test({
mode: ['client'],
compileOptions: {
dev: true
},

@ -5,6 +5,7 @@ import { test } from '../../test';
// uses a prop it does not write to but has a fallback value
export default test({
accessors: false, // so that prop actually becomes $.prop and not $.prop_source
mode: ['server', 'client'],
html: `<button>0</button><span>0</span>`,
test({ assert, target }) {

Loading…
Cancel
Save