Skip to content

Commit a5a0b49

Browse files
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 <[email protected]>
1 parent 17a7cf5 commit a5a0b49

File tree

12 files changed

+53
-2
lines changed

12 files changed

+53
-2
lines changed

.changeset/silly-apples-remain.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: handle more hydration mismatches

packages/svelte/src/internal/client/dom/blocks/each.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import {
1212
hydrate_next,
1313
hydrate_node,
1414
hydrating,
15+
read_hydration_instruction,
1516
remove_nodes,
1617
set_hydrate_node,
1718
set_hydrating
@@ -160,7 +161,7 @@ export function each(node, flags, get_collection, get_key, render_fn, fallback_f
160161
let mismatch = false;
161162

162163
if (hydrating) {
163-
var is_else = /** @type {Comment} */ (anchor).data === HYDRATION_START_ELSE;
164+
var is_else = read_hydration_instruction(anchor) === HYDRATION_START_ELSE;
164165

165166
if (is_else !== (length === 0)) {
166167
// hydration mismatch — remove the server-rendered DOM and start over

packages/svelte/src/internal/client/dom/blocks/if.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import {
44
hydrate_next,
55
hydrate_node,
66
hydrating,
7+
read_hydration_instruction,
78
remove_nodes,
89
set_hydrate_node,
910
set_hydrating
@@ -56,7 +57,8 @@ export function if_block(node, fn, [root_index, hydrate_index] = [0, 0]) {
5657

5758
if (hydrating && hydrate_index !== -1) {
5859
if (root_index === 0) {
59-
const data = /** @type {Comment} */ (anchor).data;
60+
const data = read_hydration_instruction(anchor);
61+
6062
if (data === HYDRATION_START) {
6163
hydrate_index = 0;
6264
} else if (data === HYDRATION_START_ELSE) {

packages/svelte/src/internal/client/dom/hydration.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,3 +103,16 @@ export function remove_nodes() {
103103
node = next;
104104
}
105105
}
106+
107+
/**
108+
*
109+
* @param {TemplateNode} node
110+
*/
111+
export function read_hydration_instruction(node) {
112+
if (!node || node.nodeType !== 8) {
113+
w.hydration_mismatch();
114+
throw HYDRATION_ERROR;
115+
}
116+
117+
return /** @type {Comment} */ (node).data;
118+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
import { test } from '../../test';
2+
3+
// https://github.com/sveltejs/svelte/issues/15819
4+
export default test({
5+
expect_hydration_error: true
6+
});
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
<p>start</p><p>cond</p><!---->
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
<!--[--> <p>start</p><!--[--><p>cond</p><!--]--><!--]-->
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<script>
2+
const cond = true;
3+
</script>
4+
5+
<p>start</p>{#if cond}<p>cond</p>{/if}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
import { test } from '../../test';
2+
3+
// https://github.com/sveltejs/svelte/issues/15819
4+
export default test({
5+
expect_hydration_error: true
6+
});
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
<p>start</p> pre123 mid<!---->

0 commit comments

Comments
 (0)