From 6cbdf7823b0c961190bee5b7c117b7f2bbeb832f Mon Sep 17 00:00:00 2001 From: edison Date: Wed, 5 Nov 2025 17:05:50 +0800 Subject: [PATCH] fix(hydration): avoid mismatch during hydrate text with newlines in interpolation (#9232) close #9229 --- packages/runtime-core/__tests__/hydration.spec.ts | 5 +++++ packages/runtime-core/src/hydration.ts | 12 ++++++++---- 2 files changed, 13 insertions(+), 4 deletions(-) diff --git a/packages/runtime-core/__tests__/hydration.spec.ts b/packages/runtime-core/__tests__/hydration.spec.ts index c0f25ab0f6..dffa2bc224 100644 --- a/packages/runtime-core/__tests__/hydration.spec.ts +++ b/packages/runtime-core/__tests__/hydration.spec.ts @@ -82,6 +82,11 @@ describe('SSR hydration', () => { expect(`Hydration children mismatch in
`).not.toHaveBeenWarned() }) + test('text w/ newlines', async () => { + mountWithHydration('
1\n2\n3
', () => 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) diff --git a/packages/runtime-core/src/hydration.ts b/packages/runtime-core/src/hydration.ts index bdebed5960..a9ce641321 100644 --- a/packages/runtime-core/src/hydration.ts +++ b/packages/runtime-core/src/hydration.ts @@ -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 } } -- 2.47.3