Skip to content

Commit bc7698d

Browse files
authoredDec 8, 2023
feat(ssr): add __VUE_PROD_HYDRATION_MISMATCH_DETAILS__ feature flag (#9550)
1 parent 2ffc1e8 commit bc7698d

File tree

8 files changed

+27
-8
lines changed

8 files changed

+27
-8
lines changed
 

‎packages/global.d.ts

+1
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ declare var __COMPAT__: boolean
1717
declare var __FEATURE_OPTIONS_API__: boolean
1818
declare var __FEATURE_PROD_DEVTOOLS__: boolean
1919
declare var __FEATURE_SUSPENSE__: boolean
20+
declare var __FEATURE_PROD_HYDRATION_MISMATCH_DETAILS__: boolean
2021

2122
// for tests
2223
declare namespace jest {

‎packages/runtime-core/src/featureFlags.ts

+5
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,11 @@ export function initFeatureFlags() {
2020
getGlobalThis().__VUE_PROD_DEVTOOLS__ = false
2121
}
2222

23+
if (typeof __FEATURE_PROD_HYDRATION_MISMATCH_DETAILS__ !== 'boolean') {
24+
__DEV__ && needWarn.push(`__VUE_PROD_HYDRATION_MISMATCH_DETAILS__`)
25+
getGlobalThis().__VUE_PROD_HYDRATION_MISMATCH_DETAILS__ = false
26+
}
27+
2328
if (__DEV__ && needWarn.length) {
2429
const multi = needWarn.length > 1
2530
console.warn(

‎packages/runtime-core/src/hydration.ts

+13-7
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ export function createHydrationFunctions(
8181

8282
const hydrate: RootHydrateFunction = (vnode, container) => {
8383
if (!container.hasChildNodes()) {
84-
__DEV__ &&
84+
;(__DEV__ || __FEATURE_PROD_HYDRATION_MISMATCH_DETAILS__) &&
8585
warn(
8686
`Attempting to hydrate existing markup but container is empty. ` +
8787
`Performing full mount instead.`
@@ -159,7 +159,7 @@ export function createHydrationFunctions(
159159
} else {
160160
if ((node as Text).data !== vnode.children) {
161161
hasMismatch = true
162-
__DEV__ &&
162+
;(__DEV__ || __FEATURE_PROD_HYDRATION_MISMATCH_DETAILS__) &&
163163
warn(
164164
`Hydration text mismatch in`,
165165
node.parentNode,
@@ -326,7 +326,7 @@ export function createHydrationFunctions(
326326
rendererInternals,
327327
hydrateNode
328328
)
329-
} else if (__DEV__) {
329+
} else if (__DEV__ || __FEATURE_PROD_HYDRATION_MISMATCH_DETAILS__) {
330330
warn('Invalid HostVNode type:', type, `(${typeof type})`)
331331
}
332332
}
@@ -398,7 +398,10 @@ export function createHydrationFunctions(
398398
let hasWarned = false
399399
while (next) {
400400
hasMismatch = true
401-
if (__DEV__ && !hasWarned) {
401+
if (
402+
(__DEV__ || __FEATURE_PROD_HYDRATION_MISMATCH_DETAILS__) &&
403+
!hasWarned
404+
) {
402405
warn(
403406
`Hydration children mismatch on`,
404407
el,
@@ -414,7 +417,7 @@ export function createHydrationFunctions(
414417
} else if (shapeFlag & ShapeFlags.TEXT_CHILDREN) {
415418
if (el.textContent !== vnode.children) {
416419
hasMismatch = true
417-
__DEV__ &&
420+
;(__DEV__ || __FEATURE_PROD_HYDRATION_MISMATCH_DETAILS__) &&
418421
warn(
419422
`Hydration text content mismatch on`,
420423
el,
@@ -525,7 +528,10 @@ export function createHydrationFunctions(
525528
continue
526529
} else {
527530
hasMismatch = true
528-
if (__DEV__ && !hasWarned) {
531+
if (
532+
(__DEV__ || __FEATURE_PROD_HYDRATION_MISMATCH_DETAILS__) &&
533+
!hasWarned
534+
) {
529535
warn(
530536
`Hydration children mismatch on`,
531537
container,
@@ -595,7 +601,7 @@ export function createHydrationFunctions(
595601
isFragment: boolean
596602
): Node | null => {
597603
hasMismatch = true
598-
__DEV__ &&
604+
;(__DEV__ || __FEATURE_PROD_HYDRATION_MISMATCH_DETAILS__) &&
599605
warn(
600606
`Hydration node mismatch:\n- Client vnode:`,
601607
vnode.type,

‎packages/vue/README.md

+1
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ Starting with 3.0.0-rc.3, `esm-bundler` builds now exposes global feature flags
3737

3838
- `__VUE_OPTIONS_API__` (enable/disable Options API support, default: `true`)
3939
- `__VUE_PROD_DEVTOOLS__` (enable/disable devtools support in production, default: `false`)
40+
- `__VUE_PROD_HYDRATION_MISMATCH_DETAILS__` (enable/disable detailed warnings for hydration mismatches in production, default: `false`)
4041

4142
The build will work without configuring these flags, however it is **strongly recommended** to properly configure them in order to get proper tree-shaking in the final bundle. To configure these flags:
4243

‎rollup.config.js

+3
Original file line numberDiff line numberDiff line change
@@ -180,6 +180,9 @@ function createConfig(format, output, plugins = []) {
180180
: `true`,
181181
__FEATURE_PROD_DEVTOOLS__: isBundlerESMBuild
182182
? `__VUE_PROD_DEVTOOLS__`
183+
: `false`,
184+
__FEATURE_PROD_HYDRATION_MISMATCH_DETAILS__: isBundlerESMBuild
185+
? `__VUE_PROD_HYDRATION_MISMATCH_DETAILS__`
183186
: `false`
184187
}
185188

‎scripts/dev.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,8 @@ esbuild
124124
__COMPAT__: String(target === 'vue-compat'),
125125
__FEATURE_SUSPENSE__: `true`,
126126
__FEATURE_OPTIONS_API__: `true`,
127-
__FEATURE_PROD_DEVTOOLS__: `false`
127+
__FEATURE_PROD_DEVTOOLS__: `false`,
128+
__FEATURE_PROD_HYDRATION_MISMATCH_DETAILS__: `false`
128129
}
129130
})
130131
.then(ctx => ctx.watch())

‎scripts/usage-size.ts

+1
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@ async function generateBundle(preset: Preset) {
7171
replace({
7272
'process.env.NODE_ENV': '"production"',
7373
__VUE_PROD_DEVTOOLS__: 'false',
74+
__VUE_PROD_HYDRATION_MISMATCH_DETAILS__: 'false',
7475
__VUE_OPTIONS_API__: 'true',
7576
preventAssignment: true
7677
})

‎vitest.config.ts

+1
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ export default defineConfig({
1515
__FEATURE_OPTIONS_API__: true,
1616
__FEATURE_SUSPENSE__: true,
1717
__FEATURE_PROD_DEVTOOLS__: false,
18+
__FEATURE_PROD_HYDRATION_MISMATCH_DETAILS__: false,
1819
__COMPAT__: true
1920
},
2021
resolve: {

0 commit comments

Comments
 (0)
Please sign in to comment.