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
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 35 additions & 0 deletions packages/runtime-dom/__tests__/customElement.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1330,6 +1330,41 @@ describe('defineCustomElement', () => {

expect(e.shadowRoot?.innerHTML).toBe('<div>app-injected</div>')
})

test('with hmr reload', async () => {
const __hmrId = '__hmrWithApp'
const def = defineComponent({
__hmrId,
setup() {
const msg = inject('msg')
return { msg }
},
render(this: any) {
return h('div', [h('span', this.msg), h('span', this.$foo)])
},
})
const E = defineCustomElement(def, {
configureApp(app) {
app.provide('msg', 'app-injected')
app.config.globalProperties.$foo = 'foo'
},
})
customElements.define('my-element-with-app-hmr', E)

container.innerHTML = `<my-element-with-app-hmr></my-element-with-app-hmr>`
const el = container.childNodes[0] as VueElement
expect(el.shadowRoot?.innerHTML).toBe(
`<div><span>app-injected</span><span>foo</span></div>`,
)

// hmr
__VUE_HMR_RUNTIME__.reload(__hmrId, def as any)

await nextTick()
expect(el.shadowRoot?.innerHTML).toBe(
`<div><span>app-injected</span><span>foo</span></div>`,
)
})
})

// #9885
Expand Down
4 changes: 3 additions & 1 deletion packages/runtime-dom/src/apiCustomElement.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.

render(vnode, this._root)
}

private _createVNode(): VNode<any, any> {
Expand Down