]> git.ipfire.org Git - thirdparty/vuejs/core.git/commitdiff
fix(hydration): fix css vars hydration mismatch false positive on non-root nodes
authorEvan You <yyx990803@gmail.com>
Tue, 13 Feb 2024 10:00:09 +0000 (18:00 +0800)
committerEvan You <yyx990803@gmail.com>
Tue, 13 Feb 2024 10:00:23 +0000 (18:00 +0800)
close #10317
test case from #10325

packages/runtime-core/__tests__/hydration.spec.ts
packages/runtime-core/src/hydration.ts

index 3fa0d7e732ec280df05b0d113d856de5a4f937b7..da00d7635999af047b3eebbbbf76752c25c55b91 100644 (file)
@@ -1554,5 +1554,22 @@ describe('SSR hydration', () => {
       app.mount(container)
       expect(`Hydration style mismatch`).not.toHaveBeenWarned()
     })
+
+    // #10317 - test case from #10325
+    test('css vars should only be added to expected on component root dom', () => {
+      const container = document.createElement('div')
+      container.innerHTML = `<div style="--foo:red;"><div style="color:var(--foo);" /></div>`
+      const app = createSSRApp({
+        setup() {
+          useCssVars(() => ({
+            foo: 'red',
+          }))
+          return () =>
+            h('div', null, [h('div', { style: { color: 'var(--foo)' } })])
+        },
+      })
+      app.mount(container)
+      expect(`Hydration style mismatch`).not.toHaveBeenWarned()
+    })
   })
 })
index 1e9200ce27b943c00d87ce2d0243adc4c5a8cbb4..de02ae46d8a1d0848f4c31d5567de7e8a08c3455 100644 (file)
@@ -753,9 +753,15 @@ function propHasMismatch(
       }
     }
 
-    const cssVars = instance?.getCssVars?.()
-    for (const key in cssVars) {
-      expectedMap.set(`--${key}`, String(cssVars[key]))
+    const root = instance?.subTree
+    if (
+      vnode === root ||
+      (root?.type === Fragment && (root.children as VNode[]).includes(vnode))
+    ) {
+      const cssVars = instance?.getCssVars?.()
+      for (const key in cssVars) {
+        expectedMap.set(`--${key}`, String(cssVars[key]))
+      }
     }
 
     if (!isMapEqual(actualMap, expectedMap)) {