@@ -11,6 +11,7 @@ import {
11
11
current_hydration_fragment ,
12
12
get_hydration_fragment ,
13
13
hydrate_block_anchor ,
14
+ hydrating ,
14
15
set_current_hydration_fragment
15
16
} from './hydration.js' ;
16
17
import { clear_text_content , empty , map_get , map_set } from './operations.js' ;
@@ -61,7 +62,10 @@ function each(anchor_node, collection, flags, key_fn, render_fn, fallback_fn, re
61
62
/** @type {null | import('./types.js').EffectSignal } */
62
63
let render = null ;
63
64
64
- /** Whether or not there was a "rendered fallback but want to render items" (or vice versa) hydration mismatch */
65
+ /**
66
+ * Whether or not there was a "rendered fallback but want to render items" (or vice versa) hydration mismatch.
67
+ * Needs to be a `let` or else it isn't treeshaken out
68
+ */
65
69
let mismatch = false ;
66
70
67
71
block . r =
@@ -107,7 +111,7 @@ function each(anchor_node, collection, flags, key_fn, render_fn, fallback_fn, re
107
111
// If the each block is controlled, then the anchor node will be the surrounding
108
112
// element in which the each block is rendered, which requires certain handling
109
113
// depending on whether we're in hydration mode or not
110
- if ( current_hydration_fragment === null ) {
114
+ if ( ! hydrating ) {
111
115
// Create a new anchor on the fly because there's none due to the optimization
112
116
anchor = empty ( ) ;
113
117
block . a . appendChild ( anchor ) ;
@@ -153,13 +157,13 @@ function each(anchor_node, collection, flags, key_fn, render_fn, fallback_fn, re
153
157
154
158
const length = array . length ;
155
159
156
- if ( current_hydration_fragment !== null ) {
160
+ if ( hydrating ) {
157
161
const is_each_else_comment =
158
162
/** @type {Comment } */ ( current_hydration_fragment ?. [ 0 ] ) ?. data === 'ssr:each_else' ;
159
163
// Check for hydration mismatch which can happen if the server renders the each fallback
160
164
// but the client has items, or vice versa. If so, remove everything inside the anchor and start fresh.
161
165
if ( ( is_each_else_comment && length ) || ( ! is_each_else_comment && ! length ) ) {
162
- remove ( /** @type { import('./types.js').TemplateNode[] } */ ( current_hydration_fragment ) ) ;
166
+ remove ( current_hydration_fragment ) ;
163
167
set_current_hydration_fragment ( null ) ;
164
168
mismatch = true ;
165
169
} else if ( is_each_else_comment ) {
@@ -306,22 +310,22 @@ function reconcile_indexed_array(
306
310
}
307
311
} else {
308
312
var item ;
309
- var is_hydrating = current_hydration_fragment !== null ;
313
+ /** `true` if there was a hydration mismatch. Needs to be a `let` or else it isn't treeshaken out */
314
+ let mismatch = false ;
310
315
b_blocks = Array ( b ) ;
311
- if ( is_hydrating ) {
316
+ if ( hydrating ) {
312
317
// Hydrate block
313
318
var hydration_list = /** @type {import('./types.js').TemplateNode[] } */ (
314
319
current_hydration_fragment
315
320
) ;
316
321
var hydrating_node = hydration_list [ 0 ] ;
317
322
for ( ; index < length ; index ++ ) {
318
- var fragment = /** @type {Array<Text | Comment | Element> } */ (
319
- get_hydration_fragment ( hydrating_node )
320
- ) ;
323
+ var fragment = get_hydration_fragment ( hydrating_node ) ;
321
324
set_current_hydration_fragment ( fragment ) ;
322
325
if ( ! fragment ) {
323
326
// If fragment is null, then that means that the server rendered less items than what
324
327
// the client code specifies -> break out and continue with client-side node creation
328
+ mismatch = true ;
325
329
break ;
326
330
}
327
331
@@ -357,7 +361,7 @@ function reconcile_indexed_array(
357
361
}
358
362
}
359
363
360
- if ( is_hydrating && current_hydration_fragment === null ) {
364
+ if ( mismatch ) {
361
365
// Server rendered less nodes than the client -> set empty array so that Svelte continues to operate in hydration mode
362
366
set_current_hydration_fragment ( [ ] ) ;
363
367
}
@@ -425,23 +429,23 @@ function reconcile_tracked_array(
425
429
var key ;
426
430
var item ;
427
431
var idx ;
428
- var is_hydrating = current_hydration_fragment !== null ;
432
+ /** `true` if there was a hydration mismatch. Needs to be a `let` or else it isn't treeshaken out */
433
+ let mismatch = false ;
429
434
b_blocks = Array ( b ) ;
430
- if ( is_hydrating ) {
435
+ if ( hydrating ) {
431
436
// Hydrate block
432
437
var fragment ;
433
438
var hydration_list = /** @type {import('./types.js').TemplateNode[] } */ (
434
439
current_hydration_fragment
435
440
) ;
436
441
var hydrating_node = hydration_list [ 0 ] ;
437
442
while ( b > 0 ) {
438
- fragment = /** @type {Array<Text | Comment | Element> } */ (
439
- get_hydration_fragment ( hydrating_node )
440
- ) ;
443
+ fragment = get_hydration_fragment ( hydrating_node ) ;
441
444
set_current_hydration_fragment ( fragment ) ;
442
445
if ( ! fragment ) {
443
446
// If fragment is null, then that means that the server rendered less items than what
444
447
// the client code specifies -> break out and continue with client-side node creation
448
+ mismatch = true ;
445
449
break ;
446
450
}
447
451
@@ -594,7 +598,7 @@ function reconcile_tracked_array(
594
598
}
595
599
}
596
600
597
- if ( is_hydrating && current_hydration_fragment === null ) {
601
+ if ( mismatch ) {
598
602
// Server rendered less nodes than the client -> set empty array so that Svelte continues to operate in hydration mode
599
603
set_current_hydration_fragment ( [ ] ) ;
600
604
}
0 commit comments