]> git.ipfire.org Git - thirdparty/vuejs/core.git/commitdiff
fix(hydration): avoid mismatch during hydrate text with newlines in interpolation...
authoredison <daiwei521@126.com>
Wed, 5 Nov 2025 09:05:50 +0000 (17:05 +0800)
committerGitHub <noreply@github.com>
Wed, 5 Nov 2025 09:05:50 +0000 (17:05 +0800)
close #9229

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

index c0f25ab0f649d48e17278ce626932374d99ec4fc..dffa2bc2246f44462a8c734b0d438681b08b0922 100644 (file)
@@ -82,6 +82,11 @@ describe('SSR hydration', () => {
     expect(`Hydration children mismatch in <div>`).not.toHaveBeenWarned()
   })
 
+  test('text w/ newlines', async () => {
+    mountWithHydration('<div>1\n2\n3</div>', () => h('div', '1\r\n2\r3'))
+    expect(`Hydration text mismatch`).not.toHaveBeenWarned()
+  })
+
   test('comment', () => {
     const { vnode, container } = mountWithHydration('<!---->', () => null)
     expect(vnode.el).toBe(container.firstChild)
index bdebed5960284d8bd708c9971eeff2bffe0d1410..a9ce641321fdb5d5d8d29fe5f1be6a23c6e691a7 100644 (file)
@@ -460,18 +460,22 @@ export function createHydrationFunctions(
         ) {
           clientText = clientText.slice(1)
         }
-        if (el.textContent !== clientText) {
+        const { textContent } = el
+        if (
+          textContent !== clientText &&
+          // innerHTML normalize \r\n or \r into a single \n in the DOM
+          textContent !== clientText.replace(/\r\n|\r/g, '\n')
+        ) {
           if (!isMismatchAllowed(el, MismatchTypes.TEXT)) {
             ;(__DEV__ || __FEATURE_PROD_HYDRATION_MISMATCH_DETAILS__) &&
               warn(
                 `Hydration text content mismatch on`,
                 el,
-                `\n  - rendered on server: ${el.textContent}` +
-                  `\n  - expected on client: ${vnode.children as string}`,
+                `\n  - rendered on server: ${textContent}` +
+                  `\n  - expected on client: ${clientText}`,
               )
             logMismatchError()
           }
-
           el.textContent = vnode.children as string
         }
       }