Skip to content

fix(custom-element): preserve appContext during hmr reload #12455

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 8 commits into
base: main
Choose a base branch
from

Conversation

edison1105
Copy link
Member

@edison1105 edison1105 commented Nov 21, 2024

close #12453

Summary by CodeRabbit

  • Bug Fixes

    • Improved handling of application context in custom elements to ensure correct rendering behavior.
  • Tests

    • Added a new test case to verify custom element behavior during Hot Module Replacement (HMR) reloads.

Copy link

github-actions bot commented Nov 21, 2024

Size Report

Bundles

File Size Gzip Brotli
runtime-dom.global.prod.js 101 kB (+53 B) 38.2 kB (+11 B) 34.5 kB (+58 B)
vue.global.prod.js 159 kB (+53 B) 58.4 kB (+14 B) 52 kB (+88 B)

Usages

Name Size Gzip Brotli
createApp (CAPI only) 46.6 kB 18.2 kB 16.7 kB
createApp 54.5 kB 21.2 kB 19.4 kB
createSSRApp 58.7 kB 23 kB 20.9 kB
defineCustomElement 59.4 kB (+53 B) 22.8 kB (+11 B) 20.8 kB (+7 B)
overall 68.6 kB 26.4 kB 24 kB

Copy link

pkg-pr-new bot commented Nov 21, 2024

Open in StackBlitz

@vue/compiler-core

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

@vue/compiler-dom

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

@vue/compiler-ssr

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

@vue/compiler-sfc

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

@vue/reactivity

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

@vue/runtime-core

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

@vue/runtime-dom

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

@vue/server-renderer

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

@vue/shared

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

vue

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

@vue/compat

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

commit: c1e6e85

@edison1105 edison1105 changed the title fix(customElement): keep appContext during custom element update fix(customElement): preserve appContext during custom element update Nov 22, 2024
@edison1105 edison1105 added 🔨 p3-minor-bug Priority 3: this fixes a bug, but is an edge case that only affects very specific usage. scope: custom elements labels Nov 22, 2024
@edison1105 edison1105 changed the title fix(customElement): preserve appContext during custom element update fix(customElement): preserve appContext during hmr reload Nov 22, 2024
@edison1105 edison1105 requested a review from Copilot November 22, 2024 03:58
@edison1105 edison1105 added the ready to merge The PR is ready to be merged. label Nov 22, 2024
Copy link

@Copilot Copilot AI left a comment

Choose a reason for hiding this comment

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

Copilot reviewed 2 out of 2 changed files in this pull request and generated no suggestions.

Comments skipped due to low confidence (1)

packages/runtime-dom/src/apiCustomElement.ts:524

  • The new behavior introduced in the '_update' method should be covered by tests to ensure that 'vnode.appContext' is correctly set.
if (this._app && !this._instance) vnode.appContext = this._app._context

@edison1105 edison1105 changed the title fix(customElement): preserve appContext during hmr reload fix(custom-element): preserve appContext during hmr reload Jan 6, 2025
@danieldasilva-smake
Copy link

@edison1105 Can you tell me when this will be merged? Some colleagues and me would be very happy if the HMR works again for us 😅
But thanks a lot for your work ^^

@edison1105
Copy link
Member Author

@danieldasilva-smake
I'm also not sure exactly when this PR will be merged. Maybe you can try using it in your development environment with:

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

It includes the latest code from the main branch.

Copy link

coderabbitai bot commented May 14, 2025

"""

Walkthrough

A new test case was added to verify that the provide context and global properties configured via configureApp are preserved after a Hot Module Replacement (HMR) reload in custom elements. The _update method in VueElement was modified to explicitly set the vnode's appContext to the app's context when appropriate, ensuring context retention during HMR.

Changes

File(s) Change Summary
packages/runtime-dom/__tests__/customElement.spec.ts Added a test to check that provided context and global properties persist after HMR reload in custom elements.
packages/runtime-dom/src/apiCustomElement.ts Modified VueElement._update to assign appContext to vnode before rendering, ensuring context is retained on HMR.

Sequence Diagram(s)

sequenceDiagram
    participant TestRunner
    participant CustomElement
    participant VueApp
    participant VNode
    participant HMRRuntime

    TestRunner->>CustomElement: Mount custom element with configureApp
    CustomElement->>VueApp: Provide context and set global properties
    CustomElement->>VNode: Create VNode and assign appContext
    CustomElement->>VueApp: Render VNode
    TestRunner->>HMRRuntime: Trigger HMR reload
    HMRRuntime->>CustomElement: Reload component definition
    CustomElement->>VNode: Create new VNode and assign appContext
    CustomElement->>VueApp: Render new VNode
    TestRunner->>CustomElement: Verify context and globals persist
Loading

Assessment against linked issues

Objective (Issue #) Addressed Explanation
Preserve provide context and global properties configured by configureApp after HMR in custom elements (#12453)

Poem

In the warren of code, a context was lost,
But with careful update, it now stays embossed.
HMR may reload, but our context won’t flee—
The shadow DOM whispers, “You still have me!”
🐇✨

With a hop and a test, the bug’s put to rest!
"""


📜 Recent review details

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

📥 Commits

Reviewing files that changed from the base of the PR and between cb019c1 and c1e6e85.

📒 Files selected for processing (1)
  • packages/runtime-dom/src/apiCustomElement.ts (1 hunks)
🚧 Files skipped from review as they are similar to previous changes (1)
  • packages/runtime-dom/src/apiCustomElement.ts
⏰ Context from checks skipped due to timeout of 90000ms (1)
  • GitHub Check: continuous-release
✨ Finishing Touches
  • 📝 Generate Docstrings

🪧 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.
    • Explain this complex logic.
    • 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. Examples:
    • @coderabbitai explain this code block.
    • @coderabbitai modularize this function.
  • 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 src/utils.ts and explain its main purpose.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Support

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

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai generate docstrings to generate docstrings for this PR.
  • @coderabbitai generate sequence diagram to generate a sequence diagram of the changes in this PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai 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

Documentation and Community

  • 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.

@@ -520,7 +520,9 @@ export class VueElement
}

private _update() {
render(this._createVNode(), this._root)
const vnode = this._createVNode()
if (this._app) vnode.appContext = this._app._context
Copy link
Member Author

Choose a reason for hiding this comment

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

Whether it's an HMR update or not, the appContext needs to be preserved

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 scope: hmr
Projects
None yet
Development

Successfully merging this pull request may close these issues.

defineCustomElement looses provide context configured by configureApp on HMR
2 participants