diff --git a/packages/lib/src/components/Redirect/Redirect.test.tsx b/packages/lib/src/components/Redirect/Redirect.test.tsx index d9184477cd..0f1ac19e6b 100644 --- a/packages/lib/src/components/Redirect/Redirect.test.tsx +++ b/packages/lib/src/components/Redirect/Redirect.test.tsx @@ -4,7 +4,7 @@ import Redirect from './Redirect'; import RedirectShopper from './components/RedirectShopper'; import RedirectElement from './Redirect'; -jest.mock('../../utils/detectInIframeInAccessibleDomain', () => { +jest.mock('../../utils/detectInIframeInSameOrigin', () => { return jest.fn().mockImplementation(() => { return true; }); diff --git a/packages/lib/src/components/Redirect/components/RedirectShopper/RedirectShopper.tsx b/packages/lib/src/components/Redirect/components/RedirectShopper/RedirectShopper.tsx index 30b3727fd1..2748a556be 100644 --- a/packages/lib/src/components/Redirect/components/RedirectShopper/RedirectShopper.tsx +++ b/packages/lib/src/components/Redirect/components/RedirectShopper/RedirectShopper.tsx @@ -1,5 +1,5 @@ import { Component, h } from 'preact'; -import detectInIframeInAccessibleDomain from '../../../../utils/detectInIframeInAccessibleDomain'; +import detectInIframeInSameOrigin from '../../../../utils/detectInIframeInSameOrigin'; interface RedirectShopperProps { beforeRedirect: (resolve, reject, url) => Promise; @@ -21,7 +21,7 @@ class RedirectShopper extends Component { if (this.postForm) { this.postForm.submit(); } else { - if (this.props.redirectFromTopWhenInIframe && detectInIframeInAccessibleDomain()) { + if (this.props.redirectFromTopWhenInIframe && detectInIframeInSameOrigin()) { // if in an iframe and the config prop allows it - try to redirect from the top level window window.top.location.assign?.(this.props.url); } else { @@ -51,7 +51,7 @@ class RedirectShopper extends Component { ref={ref => { this.postForm = ref; }} - {...(this.props.redirectFromTopWhenInIframe && detectInIframeInAccessibleDomain() && { target: '_top' })} + {...(this.props.redirectFromTopWhenInIframe && detectInIframeInSameOrigin() && { target: '_top' })} > {Object.keys(data).map(key => ( diff --git a/packages/lib/src/utils/detectInIframeInAccessibleDomain.ts b/packages/lib/src/utils/detectInIframeInSameOrigin.ts similarity index 57% rename from packages/lib/src/utils/detectInIframeInAccessibleDomain.ts rename to packages/lib/src/utils/detectInIframeInSameOrigin.ts index 218010505f..dcb58ccec8 100644 --- a/packages/lib/src/utils/detectInIframeInAccessibleDomain.ts +++ b/packages/lib/src/utils/detectInIframeInSameOrigin.ts @@ -1,4 +1,8 @@ -// Returns true if the page is being run in an iframe (in a domain we have access to) +/** + * Returns true if the page is being run in an iframe with the same origin as the parent. + * In this scenario, if the merchant has set redirectFromTopWhenInIframe: true, then we can perform the redirect on the top level, parent, window; + * rather than on the iframe's window + */ export default () => { try { if (window.parent.location.href) {