Skip to content

Commit 45e85da

Browse files
lukaw3dlukaw3d
authored andcommitted
Fallback to transak button if embedded doesn't initialize after 5sec
Extensions don't send origin/referrer header when loading iframe and transak now responds with `Referrer-Policy: same-origin; X-Frame-Options: sameorigin;`. We embed transak based on window size, so transak could get embedded and fail if user opens the extension in a large tab. This mitigates a failure in 5 seconds.
1 parent 46e966c commit 45e85da

2 files changed

Lines changed: 21 additions & 1 deletion

File tree

.changelog/2154.bugfix.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Fallback to transak button if embedded doesn't initialize after 5sec

src/app/pages/FiatOnrampPage/index.tsx

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,9 @@ export function FiatOnramp() {
5555
const thirdPartyAcknowledged = useSelector(selectThirdPartyAcknowledged)
5656
// Intentionally not responsive. If it initializes with embedded iframe, user
5757
// inputs some data, then resizes: do not lose user's inputs!
58-
const [shouldOpenTransakInNewTab] = useState(window.innerWidth <= 768 || window.innerHeight <= 700)
58+
const [shouldOpenTransakInNewTab, setShouldOpenTransakInNewTab] = useState(
59+
window.innerWidth <= 768 || window.innerHeight <= 700,
60+
)
5961

6062
// Ignore refreshing account balance. Don't destroy and re-create iframe if balance changes and account balance is loading again.
6163
const [isInitialLoading, setInitialLoading] = useState(true)
@@ -164,6 +166,23 @@ export function FiatOnramp() {
164166
// 'allow-top-navigation-by-user-activation',
165167
].join(' ')}
166168
src={transakUrl}
169+
onLoad={event => {
170+
// Fallback to button if embedding fails.
171+
// Note: onError isn't called on X-Frame-Options errors, and onLoad is called even on error. So rely on Transak's postMessage.
172+
const onMessage = (event: MessageEvent) => {
173+
if (event?.data?.event_id === 'TRANSAK_WIDGET_INITIALISED') {
174+
// Success
175+
clearTimeout(failureTimeoutId)
176+
window.removeEventListener('message', onMessage)
177+
}
178+
}
179+
window.addEventListener('message', onMessage)
180+
const failureTimeoutId = window.setTimeout(() => {
181+
// Error (assumed - after 5 seconds of not initializing)
182+
setShouldOpenTransakInNewTab(true)
183+
window.removeEventListener('message', onMessage)
184+
}, 5_000)
185+
}}
167186
style={{
168187
display: 'block',
169188
width: '100%',

0 commit comments

Comments
 (0)