fix: update_branch with (anchor).data possible undefined on ios devices (#15851)

* fix: update_branch with (anchor).data possible undefined

* error sooner

* add tests

* changeset

---------

Co-authored-by: Rich Harris <rich.harris@vercel.com>
pull/15939/head
Matteo Battista 5 months ago committed by GitHub
parent 17a7cf51e4
commit a5a0b49003
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

@ -0,0 +1,5 @@
---
'svelte': patch
---
fix: handle more hydration mismatches

@ -12,6 +12,7 @@ import {
hydrate_next, hydrate_next,
hydrate_node, hydrate_node,
hydrating, hydrating,
read_hydration_instruction,
remove_nodes, remove_nodes,
set_hydrate_node, set_hydrate_node,
set_hydrating set_hydrating
@ -160,7 +161,7 @@ export function each(node, flags, get_collection, get_key, render_fn, fallback_f
let mismatch = false; let mismatch = false;
if (hydrating) { if (hydrating) {
var is_else = /** @type {Comment} */ (anchor).data === HYDRATION_START_ELSE; var is_else = read_hydration_instruction(anchor) === HYDRATION_START_ELSE;
if (is_else !== (length === 0)) { if (is_else !== (length === 0)) {
// hydration mismatch — remove the server-rendered DOM and start over // hydration mismatch — remove the server-rendered DOM and start over

@ -4,6 +4,7 @@ import {
hydrate_next, hydrate_next,
hydrate_node, hydrate_node,
hydrating, hydrating,
read_hydration_instruction,
remove_nodes, remove_nodes,
set_hydrate_node, set_hydrate_node,
set_hydrating set_hydrating
@ -56,7 +57,8 @@ export function if_block(node, fn, [root_index, hydrate_index] = [0, 0]) {
if (hydrating && hydrate_index !== -1) { if (hydrating && hydrate_index !== -1) {
if (root_index === 0) { if (root_index === 0) {
const data = /** @type {Comment} */ (anchor).data; const data = read_hydration_instruction(anchor);
if (data === HYDRATION_START) { if (data === HYDRATION_START) {
hydrate_index = 0; hydrate_index = 0;
} else if (data === HYDRATION_START_ELSE) { } else if (data === HYDRATION_START_ELSE) {

@ -103,3 +103,16 @@ export function remove_nodes() {
node = next; node = next;
} }
} }
/**
*
* @param {TemplateNode} node
*/
export function read_hydration_instruction(node) {
if (!node || node.nodeType !== 8) {
w.hydration_mismatch();
throw HYDRATION_ERROR;
}
return /** @type {Comment} */ (node).data;
}

@ -0,0 +1,6 @@
import { test } from '../../test';
// https://github.com/sveltejs/svelte/issues/15819
export default test({
expect_hydration_error: true
});

@ -0,0 +1 @@
<!--[--> <p>start</p><!--[--><p>cond</p><!--]--><!--]-->

@ -0,0 +1,5 @@
<script>
const cond = true;
</script>
<p>start</p>{#if cond}<p>cond</p>{/if}

@ -0,0 +1,6 @@
import { test } from '../../test';
// https://github.com/sveltejs/svelte/issues/15819
export default test({
expect_hydration_error: true
});

@ -0,0 +1 @@
<!--[--><p>start</p>pre<a>123</a> <!--[-->mid<!--]--><!--]-->

@ -0,0 +1,9 @@
<script>
let cond = true;
</script>
<p>start</p>
pre123
{#if cond}
mid
{/if}
Loading…
Cancel
Save