Skip to content

Conversation

@edison1105
Copy link
Member

@edison1105 edison1105 commented Nov 24, 2025

close #14127

Summary by CodeRabbit

  • Bug Fixes
    • Improved Hot Module Replacement so cached static text nodes are correctly replaced during HMR, ensuring static text updates while dynamic interpolations continue to update.
  • Tests
    • Added an HMR test that verifies cached text updates and preservation of dynamic interpolations across consecutive HMR updates.

✏️ Tip: You can customize this high-level summary in your review settings.

@coderabbitai
Copy link

coderabbitai bot commented Nov 24, 2025

Walkthrough

Cached text vnode handling was changed: during DEV/HMR updates, cached text children store a per-node index and are replaced with newly created text elements instead of reusing cached DOM nodes. A test was added to verify cached text updates across HMR rerenders.

Changes

Cohort / File(s) Summary
Renderer HMR text handling
packages/runtime-core/src/renderer.ts
DEV/HMR branch replaces cached text vnodes by creating new text nodes and replacing old elements; cached text children receive a per-node index (__elIndex) to target replacements; traversal logic adjusted for Fragment offsets.
HMR test
packages/runtime-core/__tests__/hmr.spec.ts
New test "update cached text nodes": renders component with static cached text and a dynamic count, triggers state update and two HMR rerenders, asserts DOM updates static text while preserving dynamic count.

Sequence Diagram(s)

sequenceDiagram
    participant Dev as Dev (HMR)
    participant Runtime as Runtime Core
    participant DOM as Host DOM

    Note over Dev,Runtime: Normal runtime update (non-cached or no HMR)
    Dev->>Runtime: trigger update
    Runtime->>DOM: hostSetText(existingEl, newText)

    Note over Dev,Runtime: DEV/HMR update for cached Text vnode
    Dev->>Runtime: HMR rerender with PatchFlags.CACHED Text vnode
    Runtime->>Runtime: detect cached text & HMR active
    Runtime->>Runtime: compute per-node index (__elIndex)
    Runtime->>Runtime: create new text node (createText)
    Runtime->>DOM: replace old element with new text node
    Runtime->>DOM: continue patching dynamic children
Loading

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~20 minutes

  • Attention points:
    • packages/runtime-core/src/renderer.ts — correctness of __elIndex calculation and Fragment offset handling.
    • Ensure DEV/HMR-only path doesn't affect production behavior.
    • Test stability and edge cases for nested static lists / fragments.

Possibly related PRs

Suggested labels

ready to merge, scope: hmr, :hammer: p3-minor-bug

Suggested reviewers

  • Doctor-wu
  • LittleSound
  • KazariEX
  • baiwusanyu-c

Poem

🐇 I hopped through vnodes, soft and spry,
Cached texts clung tight, refusing to fly.
HMR knocked — I swapped the twig for new,
Fresh letters sprout where old ones grew.
A rabbit cheers for updates true.

Pre-merge checks and finishing touches

✅ Passed checks (5 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Linked Issues check ✅ Passed The PR addresses the linked issue #14127 by fixing HMR text node updates through changes to handle cached text nodes and enable targeted replacements via per-node indexing.
Out of Scope Changes check ✅ Passed All changes are scoped to HMR text node handling: modifications to renderer.ts for text node replacement logic and a test case validating the fix.
Docstring Coverage ✅ Passed Docstring coverage is 100.00% which is sufficient. The required threshold is 80.00%.
Title check ✅ Passed The title 'fix(hmr): handle cached text node update' directly addresses the main change in the PR, which is fixing HMR behavior for cached text nodes by implementing proper text node replacement logic.
✨ Finishing touches
  • 📝 Generate docstrings
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment
  • Commit unit tests in branch edison/fix/14127

Comment @coderabbitai help to get the list of available commands and usage tips.

@github-actions
Copy link

github-actions bot commented Nov 24, 2025

Size Report

Bundles

File Size Gzip Brotli
runtime-dom.global.prod.js 103 kB (+29 B) 39 kB (+21 B) 35.1 kB (+23 B)
vue.global.prod.js 161 kB (+29 B) 59 kB (+21 B) 52.5 kB (-119 B)

Usages

Name Size Gzip Brotli
createApp (CAPI only) 47 kB (+29 B) 18.3 kB (+15 B) 16.8 kB (+19 B)
createApp 55.2 kB (+29 B) 21.4 kB (+18 B) 19.6 kB (+12 B)
createSSRApp 59.4 kB (+29 B) 23.2 kB (+20 B) 21.1 kB (+20 B)
defineCustomElement 60.8 kB (+29 B) 23.2 kB (+21 B) 21.1 kB (+13 B)
overall 69.4 kB (+29 B) 26.6 kB (+18 B) 24.3 kB (-33 B)

@pkg-pr-new
Copy link

pkg-pr-new bot commented Nov 24, 2025

Open in StackBlitz

@vue/compiler-core

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

@vue/compiler-dom

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

@vue/compiler-sfc

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

@vue/compiler-ssr

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

@vue/reactivity

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

@vue/runtime-core

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

@vue/runtime-dom

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

@vue/server-renderer

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

@vue/shared

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

vue

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

@vue/compat

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

commit: 6c11c4d

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 1

📜 Review details

Configuration used: CodeRabbit UI

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 25ebe3a and c70913b.

📒 Files selected for processing (1)
  • packages/runtime-core/src/renderer.ts (1 hunks)
🧰 Additional context used
🧬 Code graph analysis (1)
packages/runtime-core/src/renderer.ts (1)
packages/runtime-core/src/hmr.ts (1)
  • isHmrUpdating (15-15)
🔇 Additional comments (1)
packages/runtime-core/src/renderer.ts (1)

501-501: LGTM: const to let change enables reassignment.

The change from const to let is necessary to allow reassignment of el in the HMR conditional branch below.

@edison1105 edison1105 marked this pull request as draft November 24, 2025 13:46
@edison1105 edison1105 marked this pull request as ready for review November 25, 2025 01:19
@edison1105 edison1105 changed the title fix(hmr): handle text node creation during HMR updates for cached nodes fix(hmr): handle cached text node update Nov 25, 2025
@edison1105 edison1105 added scope: hmr 🔨 p3-minor-bug Priority 3: this fixes a bug, but is an edge case that only affects very specific usage. labels Nov 25, 2025
Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 1

📜 Review details

Configuration used: CodeRabbit UI

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 97861cd and 6c11c4d.

📒 Files selected for processing (2)
  • packages/runtime-core/__tests__/hmr.spec.ts (1 hunks)
  • packages/runtime-core/src/renderer.ts (2 hunks)
🧰 Additional context used
🧬 Code graph analysis (1)
packages/runtime-core/src/renderer.ts (3)
packages/runtime-core/src/hmr.ts (1)
  • isHmrUpdating (15-15)
packages/runtime-core/src/vnode.ts (2)
  • Text (69-69)
  • Fragment (63-68)
packages/runtime-core/src/index.ts (2)
  • Text (113-113)
  • Fragment (113-113)
🔇 Additional comments (2)
packages/runtime-core/__tests__/hmr.spec.ts (1)

1044-1095: LGTM! Comprehensive test coverage for cached text node HMR updates.

The test effectively validates the fix for issue #14127 by:

  • Verifying initial render and state updates work correctly
  • Performing two consecutive HMR updates to ensure static text changes are reflected
  • Confirming dynamic state (count) is preserved across HMR updates

The test structure follows existing patterns and thoroughly exercises the interaction between HMR and cached text nodes.

packages/runtime-core/src/renderer.ts (1)

2519-2530: Well-designed approach to prevent detached DOM node references.

The logic correctly addresses the issue:

  • Non-cached text nodes inherit el from the previous node (preserving existing behavior)
  • Cached text nodes store __elIndex instead to avoid retaining references to detached DOM nodes
  • The Fragment offset (+1) correctly accounts for the fragment start anchor at index 0

This index-based approach elegantly avoids stale DOM references while enabling HMR to locate and replace cached text nodes by their position in the parent's child list.

Comment on lines +503 to +523
// We don't inherit el for cached text nodes in `traverseStaticChildren`
// to avoid retaining detached DOM nodes. However, the text node may be
// changed during HMR. In this case we need to replace the old text node
// with the new one.
if (
__DEV__ &&
isHmrUpdating &&
n2.patchFlag === PatchFlags.CACHED &&
'__elIndex' in n1
) {
const childNodes = __TEST__
? container.children
: container.childNodes
const newChild = hostCreateText(n2.children as string)
const oldChild =
childNodes[((n2 as any).__elIndex = (n1 as any).__elIndex)]
hostInsert(newChild, container, oldChild)
hostRemove(oldChild)
} else {
hostSetText(el, n2.children as string)
}
Copy link

Choose a reason for hiding this comment

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

⚠️ Potential issue | 🔴 Critical

Critical: n2.el is not updated to reference the new text node.

After creating and inserting newChild (line 516-519), then removing oldChild (line 520), n2.el still points to the old element that was set on line 501. This means n2.el references a detached DOM node after the replacement.

Apply this diff to update n2.el to reference the new text node:

         const newChild = hostCreateText(n2.children as string)
         const oldChild =
           childNodes[((n2 as any).__elIndex = (n1 as any).__elIndex)]
+        n2.el = newChild
         hostInsert(newChild, container, oldChild)
         hostRemove(oldChild)

This follows the pattern used in patchStaticNode (lines 574-584), which properly updates n2.el and n2.anchor after replacing static content during HMR.

📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
// We don't inherit el for cached text nodes in `traverseStaticChildren`
// to avoid retaining detached DOM nodes. However, the text node may be
// changed during HMR. In this case we need to replace the old text node
// with the new one.
if (
__DEV__ &&
isHmrUpdating &&
n2.patchFlag === PatchFlags.CACHED &&
'__elIndex' in n1
) {
const childNodes = __TEST__
? container.children
: container.childNodes
const newChild = hostCreateText(n2.children as string)
const oldChild =
childNodes[((n2 as any).__elIndex = (n1 as any).__elIndex)]
hostInsert(newChild, container, oldChild)
hostRemove(oldChild)
} else {
hostSetText(el, n2.children as string)
}
// We don't inherit el for cached text nodes in `traverseStaticChildren`
// to avoid retaining detached DOM nodes. However, the text node may be
// changed during HMR. In this case we need to replace the old text node
// with the new one.
if (
__DEV__ &&
isHmrUpdating &&
n2.patchFlag === PatchFlags.CACHED &&
'__elIndex' in n1
) {
const childNodes = __TEST__
? container.children
: container.childNodes
const newChild = hostCreateText(n2.children as string)
const oldChild =
childNodes[((n2 as any).__elIndex = (n1 as any).__elIndex)]
n2.el = newChild
hostInsert(newChild, container, oldChild)
hostRemove(oldChild)
} else {
hostSetText(el, n2.children as string)
}
🤖 Prompt for AI Agents
In packages/runtime-core/src/renderer.ts around lines 503 to 523, after creating
and inserting newChild and removing oldChild during HMR replace logic, n2.el is
still pointing at the detached old element; update n2 to reference the newly
inserted node (assign n2.el = newChild) and also update n2.anchor if the node
uses an anchor (follow the same assignment pattern as patchStaticNode lines
~574-584) so the vnode now points to the live DOM node.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

🔨 p3-minor-bug Priority 3: this fixes a bug, but is an edge case that only affects very specific usage. scope: hmr

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Hot module reload causing error or not updating.

2 participants