Skip to content
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

fix(customElement): preserve appContext during hmr reload #12455

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

Conversation

edison1105
Copy link
Member

close #12453

Copy link

github-actions bot commented Nov 21, 2024

Size Report

Bundles

File Size Gzip Brotli
runtime-dom.global.prod.js 100 kB (+70 B) 38 kB (+18 B) 34.2 kB (-22 B)
vue.global.prod.js 158 kB (+70 B) 57.8 kB (+17 B) 51.5 kB (+30 B)

Usages

Name Size Gzip Brotli
createApp (CAPI only) 47.2 kB 18.3 kB 16.8 kB
createApp 55.2 kB 21.3 kB 19.5 kB
createSSRApp 59.3 kB 23.1 kB 21 kB
defineCustomElement 60.1 kB (+72 B) 22.9 kB (+14 B) 20.9 kB (+17 B)
overall 69.1 kB 26.5 kB 24.1 kB

Copy link

pkg-pr-new bot commented Nov 21, 2024

Open in Stackblitz

@vue/compiler-core

pnpm add https://pkg.pr.new/@vue/compiler-core@12455

@vue/compiler-sfc

pnpm add https://pkg.pr.new/@vue/compiler-sfc@12455

@vue/compiler-dom

pnpm add https://pkg.pr.new/@vue/compiler-dom@12455

@vue/compiler-ssr

pnpm add https://pkg.pr.new/@vue/compiler-ssr@12455

@vue/runtime-core

pnpm add https://pkg.pr.new/@vue/runtime-core@12455

@vue/runtime-dom

pnpm add https://pkg.pr.new/@vue/runtime-dom@12455

@vue/reactivity

pnpm add https://pkg.pr.new/@vue/reactivity@12455

@vue/server-renderer

pnpm add https://pkg.pr.new/@vue/server-renderer@12455

@vue/shared

pnpm add https://pkg.pr.new/@vue/shared@12455

vue

pnpm add https://pkg.pr.new/vue@12455

@vue/compat

pnpm add https://pkg.pr.new/@vue/compat@12455

commit: b07d1db

@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
@@ -520,7 +520,9 @@ export class VueElement
}

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

@edison1105 edison1105 Nov 22, 2024

Choose a reason for hiding this comment

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

The reason for checking this._app here is that the test case will not pass.

test('remove then insert again', async () => {
container.innerHTML = `<my-element></my-element>`
const e = container.childNodes[0] as VueElement
container.removeChild(e)
await nextTick()
expect(e._instance).toBe(null)
expect(e.shadowRoot!.innerHTML).toBe('')
container.appendChild(e)
expect(e._instance).toBeTruthy()
expect(e.shadowRoot!.innerHTML).toBe('<div>hello</div>')
})

When re-inserting, this._app is null because we cleared it in disconnectedCallback.
For custom elements that remove and then re-insert (this._resolved = true), just calling _update is not enough.
if (this._resolved) {
this._setParent()
this._update()
} else {

We must also reinitialize this._ob because it is cleared in disconnectedCallback.
For more edge cases refer to #12412. Maybe once #12413 is merged, the check for this._app can be removed.

@edison1105 edison1105 added the ready to merge The PR is ready to be merged. label Nov 22, 2024

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
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
1 participant