diff --git a/src/core/vdom.ts b/src/core/vdom.ts index dd694ec93..8ccc53dbe 100644 --- a/src/core/vdom.ts +++ b/src/core/vdom.ts @@ -859,7 +859,11 @@ export const node = factory(({ id }) => { widgetMeta.nodeMap = widgetMeta.nodeMap || new Map(); const mountNode = widgetMeta.mountNode; const node = widgetMeta.nodeMap.get(key); - if (node && mountNode.contains(node)) { + if ( + node && + (mountNode.contains(node) || + (global.document.body !== mountNode && global.document.body.contains(node))) + ) { return node; } requestedDomNodes.add(`${widgetId}-${key}`); diff --git a/tests/core/unit/vdom.tsx b/tests/core/unit/vdom.tsx index bef7a638f..643efa150 100644 --- a/tests/core/unit/vdom.tsx +++ b/tests/core/unit/vdom.tsx @@ -3565,6 +3565,21 @@ jsdomDescribe('vdom', () => { assert.strictEqual(root.childNodes[0].childNodes[0], divNode); }); + it('should invalidate widget once body node is available', () => { + const createWidget = create({ node }); + let divNode: any; + const App = createWidget(({ middleware }) => { + divNode = middleware.node.get('div'); + return v('div', [undefined, v('body', [v('div', { key: 'div' })]), undefined]); + }); + const r = renderer(() => App({})); + const root = document.createElement('div'); + r.mount({ domNode: root }); + assert.isNull(divNode); + resolvers.resolve(); + assert.strictEqual(document.body.lastElementChild, divNode); + }); + it('should remove nodes from the map', () => { const createWidget = create({ node, invalidator }); let divNode: any;