Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 6cbdf78

Browse files
authored
fix(hydration): avoid mismatch during hydrate text with newlines in interpolation (#9232)
close #9229
1 parent 006a0c1 commit 6cbdf78

File tree

2 files changed

+13
-4
lines changed

2 files changed

+13
-4
lines changed

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,11 @@ describe('SSR hydration', () => {
8282
expect(`Hydration children mismatch in <div>`).not.toHaveBeenWarned()
8383
})
8484

85+
test('text w/ newlines', async () => {
86+
mountWithHydration('<div>1\n2\n3</div>', () => h('div', '1\r\n2\r3'))
87+
expect(`Hydration text mismatch`).not.toHaveBeenWarned()
88+
})
89+
8590
test('comment', () => {
8691
const { vnode, container } = mountWithHydration('<!---->', () => null)
8792
expect(vnode.el).toBe(container.firstChild)

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

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -460,18 +460,22 @@ export function createHydrationFunctions(
460460
) {
461461
clientText = clientText.slice(1)
462462
}
463-
if (el.textContent !== clientText) {
463+
const { textContent } = el
464+
if (
465+
textContent !== clientText &&
466+
// innerHTML normalize \r\n or \r into a single \n in the DOM
467+
textContent !== clientText.replace(/\r\n|\r/g, '\n')
468+
) {
464469
if (!isMismatchAllowed(el, MismatchTypes.TEXT)) {
465470
;(__DEV__ || __FEATURE_PROD_HYDRATION_MISMATCH_DETAILS__) &&
466471
warn(
467472
`Hydration text content mismatch on`,
468473
el,
469-
`\n - rendered on server: ${el.textContent}` +
470-
`\n - expected on client: ${vnode.children as string}`,
474+
`\n - rendered on server: ${textContent}` +
475+
`\n - expected on client: ${clientText}`,
471476
)
472477
logMismatchError()
473478
}
474-
475479
el.textContent = vnode.children as string
476480
}
477481
}

0 commit comments

Comments
 (0)