Skip to content

fix(hydration): avoid mismatch during hydrate text with newlines in interpolation - #9232

Merged
edison1105 merged 7 commits into
vuejs:mainfrom
edison1105:fix/9229
Nov 5, 2025
Merged

edison1105 merged 7 commits into
vuejs:mainfrom
edison1105:fix/9229

Conversation

@edison1105

@edison1105 edison1105 commented Sep 16, 2023 •

Copy link
Copy Markdown
Member

close #9229

image

image

@github-actions

github-actions Bot commented Sep 16, 2023 •

Copy link
Copy Markdown

Size Report

Bundles

File Size Gzip Brotli
runtime-dom.global.prod.js 100 kB (+41 B) 38.1 kB (+25 B) 34.3 kB (+25 B)
vue.global.prod.js 158 kB (+41 B) 58 kB (+25 B) 51.7 kB (+43 B)

Usages

Name Size Gzip Brotli
createApp (CAPI only) 46.5 kB 18.2 kB 16.7 kB
createApp 54.6 kB 21.3 kB 19.4 kB
createSSRApp 58.8 kB (+41 B) 23.1 kB (+26 B) 21 kB (+15 B)
defineCustomElement 59.4 kB 22.9 kB 20.8 kB
overall 68.6 kB 26.4 kB 24 kB

Comment thread packages/compiler-core/src/parse.ts Resolved

@skirtles-code skirtles-code left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Any reason not to include test cases for the change to hydration.ts?

Comment thread packages/compiler-core/src/parser.ts Resolved
@edison1105

Copy link
Copy Markdown
Member Author

/ecosystem-ci run

@edison1105

edison1105 commented Jun 22, 2024 •

Copy link
Copy Markdown
Member Author

Any reason not to include test cases for the change to hydration.ts?

It seems that jsdom's innerHTML does not normalize \r\n into \n.
I did not figure out a way to test it.

@vue-bot

vue-bot commented Jun 22, 2024

Copy link
Copy Markdown
Contributor

📝 Ran ecosystem CI: Open

suite result latest scheduled
language-tools ✅ success ❌ failure
nuxt ✅ success ✅ success
pinia ✅ success ✅ success
primevue ✅ success ✅ success
quasar ✅ success ✅ success
radix-vue ✅ success ✅ success
router ✅ success ✅ success
test-utils ✅ success ✅ success
vant ✅ success ✅ success
vite-plugin-vue ✅ success ✅ success
vitepress ✅ success ✅ success
vue-i18n ✅ success ✅ success
vue-macros ✅ success ✅ success
vuetify ✅ success ✅ success
vueuse ✅ success ✅ success
vue-simple-compiler ✅ success ✅ success

@skirtles-code

Copy link
Copy Markdown
Contributor

Any reason not to include test cases for the change to hydration.ts?

It seems that jsdom's innerHTML does not normalize \r\n into \n. I did not figure out a way to test it.

But can't you create a node that has the relevant content directly, rather than relying on jsdom to normalize it?

@edison1105

Copy link
Copy Markdown
Member Author

Any reason not to include test cases for the change to hydration.ts?

It seems that jsdom's innerHTML does not normalize \r\n into \n. I did not figure out a way to test it.

But can't you create a node that has the relevant content directly, rather than relying on jsdom to normalize it?

Done

@edison1105 edison1105 changed the title fix(compiler-core): normalize newlines in interpolation fix(hydration): avoid mismatch during hydrate text with newlines in interpolation Jun 23, 2024
@edison1105 edison1105 added 🐞 bug Something isn't working scope: ssr ready for review This PR requires more reviews labels Aug 16, 2024
@pkg-pr-new

pkg-pr-new Bot commented Feb 18, 2025

Copy link
Copy Markdown

Open in Stackblitz

@vue/compiler-core

npm i https://pkg.pr.new/@vue/compiler-core@9232

@vue/compiler-dom

npm i https://pkg.pr.new/@vue/compiler-dom@9232

@vue/compiler-sfc

npm i https://pkg.pr.new/@vue/compiler-sfc@9232

@vue/compiler-ssr

npm i https://pkg.pr.new/@vue/compiler-ssr@9232

@vue/reactivity

npm i https://pkg.pr.new/@vue/reactivity@9232

@vue/runtime-core

npm i https://pkg.pr.new/@vue/runtime-core@9232

@vue/runtime-dom

npm i https://pkg.pr.new/@vue/runtime-dom@9232

@vue/server-renderer

npm i https://pkg.pr.new/@vue/server-renderer@9232

@vue/shared

npm i https://pkg.pr.new/@vue/shared@9232

vue

npm i https://pkg.pr.new/vue@9232

@vue/compat

npm i https://pkg.pr.new/@vue/compat@9232

commit: 5d368bc

@edison1105
edison1105 merged commit 6cbdf78 into vuejs:main Nov 5, 2025
zhoubiao188 added a commit to zhoubiao188/core that referenced this pull request Nov 4, 2026
Server rendered text and the client value may legitimately differ because of
how the HTML parser handles the markup:

- `\r\n` / `\r` are normalized into `\n` (vuejs#9232)
- the first newline right after a `<pre>` / `<textarea>` start tag is dropped
  (vuejs#11873)

Both are compensated for in the text children branch of `hydrateElement`, but
the branch that hydrates a single text vnode compares the raw values, so the
same input is considered equivalent in one place and a mismatch in the other.
It is reached whenever the text is not the only child of an element, e.g.
`<div>{{ code }}<CopyButton /></div>`, `<span>{{ a }} {{ b }}</span>`, a
component whose root is a text node, or a slot returning text.

As a consequence `data-allow-mismatch="text"` is ignored for those nodes, and
in production (where `logMismatchError` is not dev-only) correct SSR output
still logs `Hydration completed but contains mismatches.`

Move the normalization into a shared helper used by both branches. The two
steps are also applied in the order the parser applies them (normalize line
endings first, then drop the leading newline), which fixes `<pre>` text that
starts with `\r\n` as well.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

🐞 bug Something isn't working ready for review This PR requires more reviews scope: ssr

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Hydration error with \r in text #23234

4 participants