@@ -4,9 +4,16 @@ import { is_promise } from '../../../shared/utils.js';
4
4
import { block , branch , pause_effect , resume_effect } from '../../reactivity/effects.js' ;
5
5
import { internal_set , mutable_source , source } from '../../reactivity/sources.js' ;
6
6
import { flushSync , set_active_effect , set_active_reaction } from '../../runtime.js' ;
7
- import { hydrate_next , hydrate_node , hydrating } from '../hydration.js' ;
7
+ import {
8
+ hydrate_next ,
9
+ hydrate_node ,
10
+ hydrating ,
11
+ remove_nodes ,
12
+ set_hydrate_node ,
13
+ set_hydrating
14
+ } from '../hydration.js' ;
8
15
import { queue_micro_task } from '../task.js' ;
9
- import { UNINITIALIZED } from '../../../../constants.js' ;
16
+ import { HYDRATION_START_ELSE , UNINITIALIZED } from '../../../../constants.js' ;
10
17
import {
11
18
component_context ,
12
19
is_runes ,
@@ -113,6 +120,19 @@ export function await_block(node, get_input, pending_fn, then_fn, catch_fn) {
113
120
var effect = block ( ( ) => {
114
121
if ( input === ( input = get_input ( ) ) ) return ;
115
122
123
+ /** Whether or not there was a hydration mismatch. Needs to be a `let` or else it isn't treeshaken out */
124
+ // @ts -ignore coercing `anchor` to a `Comment` causes TypeScript and Prettier to fight
125
+ let mismatch = hydrating && is_promise ( input ) === ( anchor . data === HYDRATION_START_ELSE ) ;
126
+
127
+ if ( mismatch ) {
128
+ // Hydration mismatch: remove everything inside the anchor and start fresh
129
+ anchor = remove_nodes ( ) ;
130
+
131
+ set_hydrate_node ( anchor ) ;
132
+ set_hydrating ( false ) ;
133
+ mismatch = true ;
134
+ }
135
+
116
136
if ( is_promise ( input ) ) {
117
137
var promise = input ;
118
138
@@ -155,6 +175,11 @@ export function await_block(node, get_input, pending_fn, then_fn, catch_fn) {
155
175
update ( THEN , false ) ;
156
176
}
157
177
178
+ if ( mismatch ) {
179
+ // continue in hydration mode
180
+ set_hydrating ( true ) ;
181
+ }
182
+
158
183
// Set the input to something else, in order to disable the promise callbacks
159
184
return ( ) => ( input = UNINITIALIZED ) ;
160
185
} ) ;
0 commit comments