Skip to content

Conversation

alex-snezhko
Copy link
Contributor

@alex-snezhko alex-snezhko commented Jun 15, 2025

close #12619

Applies prop updates on custom elements in batch before rendering instead of one at a time serially, resolving discrepancies in behavior compared to normal components.

Summary by CodeRabbit

  • New Features

    • Custom elements now batch prop updates during patch operations and defer reactions until patching finishes, improving update efficiency and stability.
  • Bug Fixes

    • Watchers on custom element props fire correctly and only on real changes, including when multiple props are updated together; lifecycle patch boundaries ensure consistent behavior.
  • Tests

    • Added tests validating batched prop updates and watcher behavior for both sync and async custom elements.

Copy link

github-actions bot commented Jun 15, 2025

Size Report

Bundles

File Size Gzip Brotli
runtime-dom.global.prod.js 101 kB (+276 B) 38.5 kB (+89 B) 34.7 kB (+103 B)
vue.global.prod.js 159 kB (+276 B) 58.6 kB (+91 B) 52.1 kB (+67 B)

Usages

Name Size Gzip Brotli
createApp (CAPI only) 46.6 kB (+94 B) 18.2 kB (+44 B) 16.7 kB (+40 B)
createApp 54.6 kB (+94 B) 21.2 kB (+36 B) 19.4 kB (+43 B)
createSSRApp 58.8 kB (+94 B) 23 kB (+37 B) 21 kB (+48 B)
defineCustomElement 59.7 kB (+276 B) 22.9 kB (+94 B) 20.9 kB (+72 B)
overall 68.6 kB (+94 B) 26.4 kB (+37 B) 24.1 kB (+85 B)

Copy link

pkg-pr-new bot commented Jun 15, 2025

Open in StackBlitz

@vue/compiler-core

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

@vue/compiler-dom

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

@vue/compiler-sfc

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

@vue/compiler-ssr

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

@vue/reactivity

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

@vue/runtime-core

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

@vue/runtime-dom

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

@vue/server-renderer

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

@vue/shared

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

vue

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

@vue/compat

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

commit: fb4e8c7

Copy link

coderabbitai bot commented Jun 15, 2025

Walkthrough

Adds begin/end patch lifecycle hooks for Vue custom elements, defers and batches prop updates during a patch, updates renderer to call the hooks around element patching, and adds tests validating batched prop updates and watcher behavior.

Changes

Cohort / File(s) Change Summary
Core interface
packages/runtime-core/src/component.ts
Added internal methods _beginPatch(): void and _endPatch(): void to ComponentCustomElementInterface.
Renderer
packages/runtime-core/src/renderer.ts
Wraps element patching with _beginPatch() and _endPatch() calls when the existing element is a Vue custom element (_isVueCE), ensuring _endPatch() runs in finally.
Custom element runtime
packages/runtime-dom/src/apiCustomElement.ts
Introduces _patching and _dirty flags on VueElement, defers immediate updates by passing !this._patching to _setProp, marks _dirty on prop change, and adds _beginPatch() / _endPatch() to batch and flush updates after patching.
Tests
packages/runtime-dom/__tests__/customElement.spec.ts
Adds synchronous and async tests verifying that multiple props patched together trigger watchers correctly and that rendered output reflects batched updates.

Sequence Diagram(s)

sequenceDiagram
    participant Renderer
    participant VueElement
    participant ComponentInstance

    Renderer->>VueElement: detect _isVueCE on n1.el
    Renderer->>VueElement: _beginPatch()         %% color: #DDEBF7 (begin)
    Note right of VueElement: _patching = true\n_dirty = false
    Renderer->>VueElement: set multiple props   %% props set with shouldUpdate = false
    VueElement->>VueElement: mark _dirty for changed props
    Renderer->>Renderer: patchElement()          %% actual VNode patch
    Renderer->>VueElement: _endPatch()           %% color: #E2F0D9 (end)
    Note right of VueElement: _patching = false\nif _dirty -> trigger update on instance
    VueElement->>ComponentInstance: trigger update (if _dirty)
Loading

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~20 minutes

Assessment against linked issues

Objective Addressed Explanation
Ensure watchers in a custom element see the latest values of other props when multiple props are updated together. (#12619)

Out-of-scope changes

No out-of-scope functional code changes detected relative to the objectives in the linked issue.

Possibly related PRs

Poem

🐇
I nibble bugs beneath the moonlit keys,
Batch the props and hush the watcher pleas.
Begin the patch, end it with a cheer,
Dirty flags cleared, fresh values appear.
Hoppity hop — the element's clear!


📜 Recent review details

Configuration used: CodeRabbit UI

Review profile: CHILL

Plan: Pro

💡 Knowledge Base configuration:

  • MCP integration is disabled by default for public repositories
  • Jira integration is disabled by default for public repositories
  • Linear integration is disabled by default for public repositories

You can enable these sources in your CodeRabbit configuration.

📥 Commits

Reviewing files that changed from the base of the PR and between 1354a62 and fb4e8c7.

📒 Files selected for processing (3)
  • packages/runtime-core/src/component.ts (1 hunks)
  • packages/runtime-core/src/renderer.ts (1 hunks)
  • packages/runtime-dom/__tests__/customElement.spec.ts (1 hunks)
🚧 Files skipped from review as they are similar to previous changes (3)
  • packages/runtime-core/src/component.ts
  • packages/runtime-dom/tests/customElement.spec.ts
  • packages/runtime-core/src/renderer.ts
✨ Finishing Touches
  • 📝 Generate Docstrings
🧪 Generate unit tests
  • Create PR with unit tests
  • Post copyable unit tests in a comment

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share
🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.

Support

Need help? Create a ticket on our support page for assistance with any issues or questions.

CodeRabbit Commands (Invoked using PR/Issue comments)

Type @coderabbitai help to get the list of available commands.

Other keywords and placeholders

  • Add @coderabbitai ignore or @coderabbit ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Status, Documentation and Community

  • Visit our Status Page to check the current availability of CodeRabbit.
  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

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

🧹 Nitpick comments (2)
packages/runtime-dom/src/apiCustomElement.ts (2)

231-233: Consider clarifying lifecycle flags & guarding against stale _dirty

_patching + _dirty are clear, but after an out-of-patch _update() call (e.g. prop changed via DOM API), _dirty stays true.
While harmless, a permanently-true flag is misleading when inspecting instances and invites future misuse.

private _update() {
  const vnode = this._createVNode()
  if (this._app) vnode.appContext = this._app._context
  render(vnode, this._root)
+  // reset bookkeeping when we are outside a batched patch cycle
+  if (!this._patching) this._dirty = false
}

A one-liner reset keeps semantics tight.


494-502: Minor: avoid redundant _dirty = true when immediate update fires

When shouldUpdate is true and _update() is called synchronously, there is no benefit in keeping _dirty flagged.

if (val !== this._props[key]) {
-  this._dirty = true
+  // mark dirty only when the render will be deferred
+  if (!shouldUpdate || this._patching) this._dirty = true

Keeps the flag strictly associated with deferred work.

📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between c91afec and d54f6c8.

📒 Files selected for processing (4)
  • packages/runtime-core/src/component.ts (1 hunks)
  • packages/runtime-core/src/renderer.ts (1 hunks)
  • packages/runtime-dom/__tests__/customElement.spec.ts (1 hunks)
  • packages/runtime-dom/src/apiCustomElement.ts (4 hunks)
🧰 Additional context used
🧬 Code Graph Analysis (1)
packages/runtime-core/src/renderer.ts (1)
packages/runtime-dom/src/apiCustomElement.ts (1)
  • VueElement (202-710)
🔇 Additional comments (4)
packages/runtime-core/src/component.ts (1)

1273-1280: Custom-element patch-lifecycle hooks exposed – looks good

The addition of _beginPatch() / _endPatch() in the ComponentCustomElementInterface aligns with the renderer changes and the concrete implementation in VueElement.
No issues spotted – signature and visibility (@internal) are appropriate.

packages/runtime-dom/src/apiCustomElement.ts (2)

460-468: shouldUpdate computation may skip updates if instance not yet mounted

Inside the generated setter we pass !this._patching to _setProp.
During pre-mount cycles (e.g. attributes parsed before connectedCallback), this._instance is still null, so _setProp will:

  1. mark _dirty,
  2. evaluate shouldUpdate === true,
  3. not call _update() because this._instance is falsy,
  4. leave _dirty set to true.

If the element mounts without going through a renderer patch cycle (_beginPatch/_endPatch), the first render happens with stale props.

Please double-check that all early attribute/property writes are followed by either _beginPatch/_endPatch or a later setter invocation so an update is eventually fired.


676-693: Great addition – batching hook integrates cleanly with renderer

The _beginPatch / _endPatch pair is straightforward and the finally{} invocation on the renderer side guarantees symmetry even on thrown errors – well done.

packages/runtime-dom/__tests__/customElement.spec.ts (1)

477-565: Solid coverage for batched prop patching

The two new test blocks exercise both sync and async scenarios and verify:

• DOM output
• watcher invocation counts
• no redundant updates on identical prop sets

This should reliably catch regressions in the new batching logic. 👍

Also applies to: 566-660

@edison1105 edison1105 added ready to merge The PR is ready to be merged. 🔨 p3-minor-bug Priority 3: this fixes a bug, but is an edge case that only affects very specific usage. scope: custom elements labels Jun 16, 2025
@alex-snezhko alex-snezhko force-pushed the custom-element-batch-prop-patch branch from f695bc3 to 1354a62 Compare June 16, 2025 03:13
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: 0

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

624-627: Remove redundant !! and duplicate casts for clearer intent

customElement is derived solely to hold either the element reference or null; the double-negation adds no value and forces an unnecessary boolean conversion.
Inlining the element reference also eliminates the repeated as VueElement cast.

-      const customElement = !!(n1.el && (n1.el as VueElement)._isVueCE)
-        ? (n1.el as VueElement)
-        : null
+      const el = n1.el as VueElement | null
+      const customElement = el && el._isVueCE ? el : null

This aligns with the Biome lint recommendation and slightly improves readability.

🧰 Tools
🪛 Biome (1.9.4)

[error] 624-624: Avoid redundant double-negation.

It is not necessary to use double-negation when a value will already be coerced to a boolean.
Unsafe fix: Remove redundant double-negation

(lint/complexity/noExtraBooleanCast)

📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between f695bc3 and 1354a62.

📒 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-dom/src/apiCustomElement.ts (1)
  • VueElement (202-710)
🪛 Biome (1.9.4)
packages/runtime-core/src/renderer.ts

[error] 624-624: Avoid redundant double-negation.

It is not necessary to use double-negation when a value will already be coerced to a boolean.
Unsafe fix: Remove redundant double-negation

(lint/complexity/noExtraBooleanCast)

⏰ Context from checks skipped due to timeout of 90000ms (4)
  • GitHub Check: Redirect rules
  • GitHub Check: Header rules
  • GitHub Check: Pages changed
  • GitHub Check: test / unit-test-windows

@edison1105
Copy link
Member

/ecosystem-ci run

@vuejs vuejs deleted a comment from edison1105 Sep 1, 2025
@vue-bot
Copy link
Contributor

vue-bot commented Sep 1, 2025

📝 Ran ecosystem CI: Open

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

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. ready to merge The PR is ready to be merged. scope: custom elements
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Using watch to observe a prop in a vue custom element, the other props accessed in the listener are the old values.
3 participants