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(custom-element): Use asynchronous custom element nesting to avoid errors #9351

Closed
wants to merge 19 commits into from
Closed
Show file tree
Hide file tree
Changes from 2 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
34 changes: 34 additions & 0 deletions packages/runtime-dom/__tests__/customElement.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {
h,
inject,
nextTick,
provide,
Ref,
ref,
renderSlot,
Expand Down Expand Up @@ -692,5 +693,38 @@ describe('defineCustomElement', () => {
`<div><slot><div>fallback</div></slot></div><div><slot name="named"></slot></div>`
)
})

test('async & nested custom elements', async () => {
let fooVal: string | undefined = ''
const E = defineCustomElement(
defineAsyncComponent(() => {
return Promise.resolve({
setup(props) {
provide('foo', 'foo')
},
render(this: any) {
return h('div', null, [renderSlot(this.$slots, 'default')])
}
})
})
)

const EChild = defineCustomElement({
setup(props) {
fooVal = inject('foo')
},
render(this: any) {
return h('div', null, 'child')
}
})
customElements.define('my-el-async-nested-ce', E)
customElements.define('slotted-child', EChild)
container.innerHTML = `<my-el-async-nested-ce><slotted-child></slotted-child></my-el-async-nested-ce>`

await new Promise(r => setTimeout(r))
const e = container.childNodes[0] as VueElement
expect(e.shadowRoot!.innerHTML).toBe(`<div><slot></slot></div>`)
expect(fooVal).toBe('foo')
})
})
})
24 changes: 22 additions & 2 deletions packages/runtime-dom/src/apiCustomElement.ts
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,7 @@ export class VueElement extends BaseClass {
private _numberProps: Record<string, true> | null = null
private _styles?: HTMLStyleElement[]
private _ob?: MutationObserver | null = null
private _childResolve: Array<() => void> = []
constructor(
private _def: InnerComponentDef,
private _props: Record<string, any> = {},
Expand Down Expand Up @@ -282,9 +283,28 @@ export class VueElement extends BaseClass {

const asyncDef = (this._def as ComponentOptions).__asyncLoader
if (asyncDef) {
asyncDef().then(def => resolve(def, true))
asyncDef().then(def => {
resolve(def, true)
if (this._childResolve.length > 0) {
this._childResolve.forEach((childResolveFn: () => void) => {
childResolveFn()
})
}
})
} else {
resolve(this._def)
// When parentNode is a custom element rendered by an asynchronous component,
// it is collected and waits for the asynchronous
// completion before executing the resolve function.
const parentNode = this.parentNode as typeof this
baiwusanyu-c marked this conversation as resolved.
Show resolved Hide resolved
if (
parentNode &&
parentNode._def &&
(parentNode._def as ComponentOptions).__asyncLoader
) {
parentNode._childResolve.push(() => resolve(this._def))
} else {
resolve(this._def)
}
}
}

Expand Down