@@ -11,6 +11,7 @@ import {
1111 current_hydration_fragment ,
1212 get_hydration_fragment ,
1313 hydrate_block_anchor ,
14+ hydrating ,
1415 set_current_hydration_fragment
1516} from './hydration.js' ;
1617import { 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
6162 /** @type {null | import('./types.js').EffectSignal } */
6263 let render = null ;
6364
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+ */
6569 let mismatch = false ;
6670
6771 block . r =
@@ -107,7 +111,7 @@ function each(anchor_node, collection, flags, key_fn, render_fn, fallback_fn, re
107111 // If the each block is controlled, then the anchor node will be the surrounding
108112 // element in which the each block is rendered, which requires certain handling
109113 // depending on whether we're in hydration mode or not
110- if ( current_hydration_fragment === null ) {
114+ if ( ! hydrating ) {
111115 // Create a new anchor on the fly because there's none due to the optimization
112116 anchor = empty ( ) ;
113117 block . a . appendChild ( anchor ) ;
@@ -153,13 +157,13 @@ function each(anchor_node, collection, flags, key_fn, render_fn, fallback_fn, re
153157
154158 const length = array . length ;
155159
156- if ( current_hydration_fragment !== null ) {
160+ if ( hydrating ) {
157161 const is_each_else_comment =
158162 /** @type {Comment } */ ( current_hydration_fragment ?. [ 0 ] ) ?. data === 'ssr:each_else' ;
159163 // Check for hydration mismatch which can happen if the server renders the each fallback
160164 // but the client has items, or vice versa. If so, remove everything inside the anchor and start fresh.
161165 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 ) ;
163167 set_current_hydration_fragment ( null ) ;
164168 mismatch = true ;
165169 } else if ( is_each_else_comment ) {
@@ -306,22 +310,22 @@ function reconcile_indexed_array(
306310 }
307311 } else {
308312 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 ;
310315 b_blocks = Array ( b ) ;
311- if ( is_hydrating ) {
316+ if ( hydrating ) {
312317 // Hydrate block
313318 var hydration_list = /** @type {import('./types.js').TemplateNode[] } */ (
314319 current_hydration_fragment
315320 ) ;
316321 var hydrating_node = hydration_list [ 0 ] ;
317322 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 ) ;
321324 set_current_hydration_fragment ( fragment ) ;
322325 if ( ! fragment ) {
323326 // If fragment is null, then that means that the server rendered less items than what
324327 // the client code specifies -> break out and continue with client-side node creation
328+ mismatch = true ;
325329 break ;
326330 }
327331
@@ -357,7 +361,7 @@ function reconcile_indexed_array(
357361 }
358362 }
359363
360- if ( is_hydrating && current_hydration_fragment === null ) {
364+ if ( mismatch ) {
361365 // Server rendered less nodes than the client -> set empty array so that Svelte continues to operate in hydration mode
362366 set_current_hydration_fragment ( [ ] ) ;
363367 }
@@ -425,23 +429,23 @@ function reconcile_tracked_array(
425429 var key ;
426430 var item ;
427431 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 ;
429434 b_blocks = Array ( b ) ;
430- if ( is_hydrating ) {
435+ if ( hydrating ) {
431436 // Hydrate block
432437 var fragment ;
433438 var hydration_list = /** @type {import('./types.js').TemplateNode[] } */ (
434439 current_hydration_fragment
435440 ) ;
436441 var hydrating_node = hydration_list [ 0 ] ;
437442 while ( b > 0 ) {
438- fragment = /** @type {Array<Text | Comment | Element> } */ (
439- get_hydration_fragment ( hydrating_node )
440- ) ;
443+ fragment = get_hydration_fragment ( hydrating_node ) ;
441444 set_current_hydration_fragment ( fragment ) ;
442445 if ( ! fragment ) {
443446 // If fragment is null, then that means that the server rendered less items than what
444447 // the client code specifies -> break out and continue with client-side node creation
448+ mismatch = true ;
445449 break ;
446450 }
447451
@@ -594,7 +598,7 @@ function reconcile_tracked_array(
594598 }
595599 }
596600
597- if ( is_hydrating && current_hydration_fragment === null ) {
601+ if ( mismatch ) {
598602 // Server rendered less nodes than the client -> set empty array so that Svelte continues to operate in hydration mode
599603 set_current_hydration_fragment ( [ ] ) ;
600604 }
0 commit comments