Skip to content

Commit

Permalink
Check if iframe is still in body before removing (#399)
Browse files Browse the repository at this point in the history
If the iframe is removed from the DOM prior to the timeout it would error on removeChild.

Error thrown: `Uncaught DOMException: Failed to execute 'removeChild' on 'Node': The node to be removed is not a child of this node.`

Bug introduced in #376

Co-authored-by: Steve Hobbs <[email protected]>
  • Loading branch information
paulfalgout and Steve Hobbs authored Apr 15, 2020
1 parent 55d479e commit 7b04165
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 2 deletions.
1 change: 1 addition & 0 deletions __tests__/utils.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -621,6 +621,7 @@ describe('utils', () => {
expect(type).toBe('iframe');
return iframe;
});
window.document.body.contains = () => true;
window.document.body.appendChild = jest.fn();
window.document.body.removeChild = jest.fn();
return { iframe, url, origin };
Expand Down
10 changes: 8 additions & 2 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,10 +55,16 @@ export const runIframe = (
iframe.setAttribute('width', '0');
iframe.setAttribute('height', '0');
iframe.style.display = 'none';

const removeIframe = () => {
if (window.document.body.contains(iframe)) {
window.document.body.removeChild(iframe);
}
};

const timeoutSetTimeoutId = setTimeout(() => {
rej(TIMEOUT_ERROR);
window.document.body.removeChild(iframe);
removeIframe();
}, timeoutInSeconds * 1000);

const iframeEventHandler = function(e: MessageEvent) {
Expand All @@ -71,7 +77,7 @@ export const runIframe = (
// Delay the removal of the iframe to prevent hanging loading status
// in Chrome: https://github.com/auth0/auth0-spa-js/issues/240
setTimeout(
() => window.document.body.removeChild(iframe),
removeIframe,
CLEANUP_IFRAME_TIMEOUT_IN_SECONDS * 1000
);
};
Expand Down

0 comments on commit 7b04165

Please sign in to comment.