Skip to content

Commit

Permalink
fix: iframe deeplink (#3403)
Browse files Browse the repository at this point in the history
  • Loading branch information
lukaisailovic authored Dec 6, 2024
1 parent 817314f commit d4352b0
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 3 deletions.
20 changes: 20 additions & 0 deletions .changeset/small-games-judge.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
---
'@reown/appkit-scaffold-ui': patch
'@reown/appkit-core': patch
'@reown/appkit-adapter-ethers': patch
'@reown/appkit-adapter-ethers5': patch
'@reown/appkit-adapter-solana': patch
'@reown/appkit-adapter-wagmi': patch
'@reown/appkit': patch
'@reown/appkit-utils': patch
'@reown/appkit-cdn': patch
'@reown/appkit-common': patch
'@reown/appkit-experimental': patch
'@reown/appkit-polyfills': patch
'@reown/appkit-siwe': patch
'@reown/appkit-siwx': patch
'@reown/appkit-ui': patch
'@reown/appkit-wallet': patch
---

iframe deeplink fix
13 changes: 11 additions & 2 deletions packages/core/src/utils/CoreHelperUtil.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import type { CaipAddress, CaipNetwork } from '@reown/appkit-common'
import type { AccountTypeMap, ChainAdapter, LinkingRecord, NamespaceTypeMap } from './TypeUtil.js'

type SDKFramework = 'html' | 'react' | 'vue'
type OpenTarget = '_blank' | '_self' | 'popupWindow' | '_top'

export const CoreHelperUtil = {
isMobile() {
Expand Down Expand Up @@ -50,6 +51,14 @@ export const CoreHelperUtil = {
navigator.clipboard.writeText(text)
},

isIframe() {
try {
return window.self !== window.top
} catch (e) {
return false
}
},

getPairingExpiry() {
return Date.now() + ConstantsUtil.FOUR_MINUTES_MS
},
Expand Down Expand Up @@ -136,11 +145,11 @@ export const CoreHelperUtil = {

return target
},
openHref(href: string, target: '_blank' | '_self' | 'popupWindow', features?: string) {
openHref(href: string, target: OpenTarget, features?: string) {
window.open(href, this.getOpenTargetForPlatform(target), features || 'noreferrer noopener')
},

returnOpenHref(href: string, target: '_blank' | '_self' | 'popupWindow', features?: string) {
returnOpenHref(href: string, target: OpenTarget, features?: string) {
return window.open(
href,
this.getOpenTargetForPlatform(target),
Expand Down
29 changes: 29 additions & 0 deletions packages/core/tests/utils/CoreHelperUtil.test.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
// @vitest-environment jsdom
import { describe, expect, it } from 'vitest'
import { CoreHelperUtil } from '../../src/utils/CoreHelperUtil.js'

Expand Down Expand Up @@ -28,4 +29,32 @@ describe('CoreHelperUtil', () => {
expect(CoreHelperUtil.isAddress(address, chain)).toBe(expected)
}
)

it('should return true when inside an iframe', () => {
const originalTop = global.window.top
const originalSelf = global.window.self
try {
;(global.window as any).top = { name: 'top' }
;(global.window as any).self = { name: 'self' }

expect(CoreHelperUtil.isIframe()).toBe(true)
} finally {
global.window.top = originalTop
global.window.self = originalSelf
}
})

it('should return false when not inside an iframe', () => {
const originalTop = global.window.top
const originalSelf = global.window.self

try {
global.window.top = global.window.self

expect(CoreHelperUtil.isIframe()).toBe(false)
} finally {
global.window.top = originalTop
global.window.self = originalSelf
}
})
})
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,8 @@ export class W3mConnectingWcMobile extends W3mConnectingWidget {
const { redirect, href } = CoreHelperUtil.formatNativeUrl(mobile_link, this.uri)
ConnectionController.setWcLinking({ name, href })
ConnectionController.setRecentWallet(this.wallet)
CoreHelperUtil.openHref(redirect, '_self')
const target = CoreHelperUtil.isIframe() ? '_top' : '_self'
CoreHelperUtil.openHref(redirect, target)
clearTimeout(this.labelTimeout)
this.secondaryLabel = ConstantsUtil.CONNECT_LABELS.MOBILE
} catch (e) {
Expand Down

0 comments on commit d4352b0

Please sign in to comment.