@@ -24,8 +24,14 @@ import { create_attribute, is_custom_element_node, is_element_node } from '../..
2424import { error } from '../../../errors.js' ;
2525import { binding_properties } from '../../bindings.js' ;
2626import { regex_starts_with_newline , regex_whitespaces_strict } from '../../patterns.js' ;
27- import { DOMBooleanAttributes , HYDRATION_END , HYDRATION_START } from '../../../../constants.js' ;
27+ import {
28+ DOMBooleanAttributes ,
29+ HYDRATION_END ,
30+ HYDRATION_END_ELSE ,
31+ HYDRATION_START
32+ } from '../../../../constants.js' ;
2833import { sanitize_template_string } from '../../../utils/sanitize_template_string.js' ;
34+ import { BLOCK_CLOSE , BLOCK_CLOSE_ELSE } from '../../../../internal/server/hydration.js' ;
2935
3036export const block_open = t_string ( `<!--${ HYDRATION_START } -->` ) ;
3137export const block_close = t_string ( `<!--${ HYDRATION_END } -->` ) ;
@@ -1499,55 +1505,46 @@ const template_visitors = {
14991505 b . update ( '++' , index , false ) ,
15001506 b . block ( each )
15011507 ) ;
1508+
1509+ const close = b . stmt ( b . assignment ( '+=' , b . id ( '$$payload.out' ) , b . literal ( BLOCK_CLOSE ) ) ) ;
1510+
15021511 if ( node . fallback ) {
1503- const fallback_stmts = create_block ( node , node . fallback . nodes , context ) ;
1504- fallback_stmts . unshift (
1505- b . stmt ( b . assignment ( '+=' , b . id ( '$$payload.out' ) , b . literal ( '<!ssr:each_else>' ) ) )
1506- ) ;
1512+ const fallback = create_block ( node , node . fallback . nodes , context ) ;
1513+
1514+ fallback . push ( b . stmt ( b . assignment ( '+=' , b . id ( '$$payload.out' ) , b . literal ( BLOCK_CLOSE_ELSE ) ) ) ) ;
1515+
15071516 state . template . push (
15081517 t_statement (
15091518 b . if (
15101519 b . binary ( '!==' , b . member ( array_id , b . id ( 'length' ) ) , b . literal ( 0 ) ) ,
1511- for_loop ,
1512- b . block ( fallback_stmts )
1520+ b . block ( [ for_loop , close ] ) ,
1521+ b . block ( fallback )
15131522 )
15141523 )
15151524 ) ;
15161525 } else {
1517- state . template . push ( t_statement ( for_loop ) ) ;
1526+ state . template . push ( t_statement ( for_loop ) , t_statement ( close ) ) ;
15181527 }
1519- state . template . push ( block_close ) ;
15201528 } ,
15211529 IfBlock ( node , context ) {
15221530 const state = context . state ;
15231531 state . template . push ( block_open ) ;
15241532
1525- // Insert ssr:if:true/false anchors in addition to the other anchors so that
1526- // the if block can catch hydration mismatches (false on the server, true on the client and vice versa)
1527- // and continue hydration without having to re-render everything from scratch.
1528-
15291533 const consequent = create_block ( node , node . consequent . nodes , context ) ;
1530- consequent . unshift (
1531- b . stmt ( b . assignment ( '+=' , b . id ( '$$payload.out' ) , b . literal ( '<!ssr:if:true>' ) ) )
1532- ) ;
1534+ const alternate = node . alternate ? create_block ( node , node . alternate . nodes , context ) : [ ] ;
15331535
1534- const alternate = node . alternate
1535- ? /** @type {import('estree').BlockStatement } */ ( context . visit ( node . alternate ) )
1536- : b . block ( [ ] ) ;
1537- alternate . body . unshift (
1538- b . stmt ( b . assignment ( '+=' , b . id ( '$$payload.out' ) , b . literal ( '<!ssr:if:false>' ) ) )
1539- ) ;
1536+ consequent . push ( b . stmt ( b . assignment ( '+=' , b . id ( '$$payload.out' ) , b . literal ( BLOCK_CLOSE ) ) ) ) ;
1537+ alternate . push ( b . stmt ( b . assignment ( '+=' , b . id ( '$$payload.out' ) , b . literal ( BLOCK_CLOSE_ELSE ) ) ) ) ;
15401538
15411539 state . template . push (
15421540 t_statement (
15431541 b . if (
15441542 /** @type {import('estree').Expression } */ ( context . visit ( node . test ) ) ,
1545- b . block ( /** @type { import('estree').Statement[] } */ ( consequent ) ) ,
1546- alternate
1543+ b . block ( consequent ) ,
1544+ b . block ( alternate )
15471545 )
15481546 )
15491547 ) ;
1550- state . template . push ( block_close ) ;
15511548 } ,
15521549 AwaitBlock ( node , context ) {
15531550 const state = context . state ;
0 commit comments