@@ -50,7 +50,15 @@ enum DOMNodeTypes {
50
50
COMMENT = 8 ,
51
51
}
52
52
53
- let hasMismatch = false
53
+ let hasLoggedMismatchError = false
54
+ const logMismatchError = ( ) => {
55
+ if ( __TEST__ || hasLoggedMismatchError ) {
56
+ return
57
+ }
58
+ // this error should show up in production
59
+ console . error ( 'Hydration completed but contains mismatches.' )
60
+ hasLoggedMismatchError = true
61
+ }
54
62
55
63
const isSVGContainer = ( container : Element ) =>
56
64
container . namespaceURI ! . includes ( 'svg' ) &&
@@ -102,14 +110,10 @@ export function createHydrationFunctions(
102
110
container . _vnode = vnode
103
111
return
104
112
}
105
- hasMismatch = false
113
+
106
114
hydrateNode ( container . firstChild ! , vnode , null , null , null )
107
115
flushPostFlushCbs ( )
108
116
container . _vnode = vnode
109
- if ( hasMismatch && ! __TEST__ ) {
110
- // this error should show up in production
111
- console . error ( `Hydration completed but contains mismatches.` )
112
- }
113
117
}
114
118
115
119
const hydrateNode = (
@@ -170,7 +174,6 @@ export function createHydrationFunctions(
170
174
}
171
175
} else {
172
176
if ( ( node as Text ) . data !== vnode . children ) {
173
- hasMismatch = true
174
177
; ( __DEV__ || __FEATURE_PROD_HYDRATION_MISMATCH_DETAILS__ ) &&
175
178
warn (
176
179
`Hydration text mismatch in` ,
@@ -180,6 +183,7 @@ export function createHydrationFunctions(
180
183
) } ` +
181
184
`\n - expected on client: ${ JSON . stringify ( vnode . children ) } ` ,
182
185
)
186
+ logMismatchError ( )
183
187
; ( node as Text ) . data = vnode . children as string
184
188
}
185
189
nextNode = nextSibling ( node )
@@ -409,7 +413,6 @@ export function createHydrationFunctions(
409
413
)
410
414
let hasWarned = false
411
415
while ( next ) {
412
- hasMismatch = true
413
416
if (
414
417
( __DEV__ || __FEATURE_PROD_HYDRATION_MISMATCH_DETAILS__ ) &&
415
418
! hasWarned
@@ -421,21 +424,24 @@ export function createHydrationFunctions(
421
424
)
422
425
hasWarned = true
423
426
}
427
+ logMismatchError ( )
428
+
424
429
// The SSRed DOM contains more nodes than it should. Remove them.
425
430
const cur = next
426
431
next = next . nextSibling
427
432
remove ( cur )
428
433
}
429
434
} else if ( shapeFlag & ShapeFlags . TEXT_CHILDREN ) {
430
435
if ( el . textContent !== vnode . children ) {
431
- hasMismatch = true
432
436
; ( __DEV__ || __FEATURE_PROD_HYDRATION_MISMATCH_DETAILS__ ) &&
433
437
warn (
434
438
`Hydration text content mismatch on` ,
435
439
el ,
436
440
`\n - rendered on server: ${ el . textContent } ` +
437
441
`\n - expected on client: ${ vnode . children as string } ` ,
438
442
)
443
+ logMismatchError ( )
444
+
439
445
el . textContent = vnode . children as string
440
446
}
441
447
}
@@ -455,7 +461,7 @@ export function createHydrationFunctions(
455
461
( __DEV__ || __FEATURE_PROD_HYDRATION_MISMATCH_DETAILS__ ) &&
456
462
propHasMismatch ( el , key , props [ key ] , vnode , parentComponent )
457
463
) {
458
- hasMismatch = true
464
+ logMismatchError ( )
459
465
}
460
466
if (
461
467
( forcePatch &&
@@ -545,7 +551,6 @@ export function createHydrationFunctions(
545
551
// because server rendered HTML won't contain a text node
546
552
insert ( ( vnode . el = createText ( '' ) ) , container )
547
553
} else {
548
- hasMismatch = true
549
554
if (
550
555
( __DEV__ || __FEATURE_PROD_HYDRATION_MISMATCH_DETAILS__ ) &&
551
556
! hasWarned
@@ -557,6 +562,8 @@ export function createHydrationFunctions(
557
562
)
558
563
hasWarned = true
559
564
}
565
+ logMismatchError ( )
566
+
560
567
// the SSRed DOM didn't contain enough nodes. Mount the missing ones.
561
568
patch (
562
569
null ,
@@ -603,7 +610,8 @@ export function createHydrationFunctions(
603
610
} else {
604
611
// fragment didn't hydrate successfully, since we didn't get a end anchor
605
612
// back. This should have led to node/children mismatch warnings.
606
- hasMismatch = true
613
+ logMismatchError ( )
614
+
607
615
// since the anchor is missing, we need to create one and insert it
608
616
insert ( ( vnode . anchor = createComment ( `]` ) ) , container , next )
609
617
return next
@@ -618,7 +626,6 @@ export function createHydrationFunctions(
618
626
slotScopeIds : string [ ] | null ,
619
627
isFragment : boolean ,
620
628
) : Node | null => {
621
- hasMismatch = true
622
629
; ( __DEV__ || __FEATURE_PROD_HYDRATION_MISMATCH_DETAILS__ ) &&
623
630
warn (
624
631
`Hydration node mismatch:\n- rendered on server:` ,
@@ -631,6 +638,8 @@ export function createHydrationFunctions(
631
638
`\n- expected on client:` ,
632
639
vnode . type ,
633
640
)
641
+ logMismatchError ( )
642
+
634
643
vnode . el = null
635
644
636
645
if ( isFragment ) {
0 commit comments