Skip to content
Merged
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
20 changes: 15 additions & 5 deletions src/dom.js
Original file line number Diff line number Diff line change
Expand Up @@ -571,9 +571,18 @@ export function popup(
}

if (closeOnUnload) {
window.addEventListener("pagehide", () => win.close());
window.addEventListener("unload", () => win.close());
window.addEventListener("beforeunload", () => win.close());
window.addEventListener("beforeunload", () => {
Copy link
Contributor

Choose a reason for hiding this comment

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

Great to see beforeunload kept here. From previously testing, beforeunload is required to ensure the popup is closed when the user closes the merchant page.

win.close();
});
if ("onpagehide" in window) {
window.addEventListener("pagehide", () => {
win.close();
});
} else {
window.addEventListener("unload", () => {
win.close();
});
}
}

return win;
Expand Down Expand Up @@ -1035,6 +1044,7 @@ export function watchElementForClose(
handler: () => mixed
): CancelableType {
handler = once(handler);
const terminationEvent = "onpagehide" in window ? "pagehide" : "unload";

let cancelled = false;
const mutationObservers = [];
Expand All @@ -1054,7 +1064,7 @@ export function watchElementForClose(
}
if (sacrificialFrameWin) {
// eslint-disable-next-line no-use-before-define
sacrificialFrameWin.removeEventListener("unload", elementClosed);
sacrificialFrameWin.removeEventListener(terminationEvent, elementClosed);
}
if (sacrificialFrame) {
destroyElement(sacrificialFrame);
Expand Down Expand Up @@ -1097,7 +1107,7 @@ export function watchElementForClose(
sacrificialFrame.style.display = "none";
awaitFrameWindow(sacrificialFrame).then((frameWin) => {
sacrificialFrameWin = assertSameDomain(frameWin);
sacrificialFrameWin.addEventListener("unload", elementClosed);
sacrificialFrameWin.addEventListener(terminationEvent, elementClosed);
});
element.appendChild(sacrificialFrame);

Expand Down
15 changes: 9 additions & 6 deletions test/tests/dom/popup.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,15 @@ describe("popup", () => {
closeOnUnload: 1,
});

if (!listeners.unload) {
throw new Error(`Popup should have unload listener registered.`);
}

if (!listeners.pagehide) {
throw new Error(`Popup should have pagehide listener registered.`);
// Check that either pagehide or unload is registered (but not both)
if ("onpagehide" in window) {
if (!listeners.pagehide) {
throw new Error(`Popup should have pagehide listener registered.`);
}
} else {
if (!listeners.unload) {
throw new Error(`Popup should have unload listener registered.`);
}
}

if (!listeners.beforeunload) {
Expand Down
Loading