Skip to content

Send files directly between devices over the internet - no cloud storage, no size limits.

fix deep links crashing on cold start in mobile

Denis Lupookov860dec78f0a4
diff --git a/apps/mobile/app/+native-intent.ts b/apps/mobile/app/+native-intent.ts
index 20a1a65..0a0e842 100644
--- a/apps/mobile/app/+native-intent.ts
+++ b/apps/mobile/app/+native-intent.ts
@@ -1,12 +1,16 @@
+import { isPairUrl } from '@altersend/domain'
import { getShareExtensionKey } from 'expo-share-intent'
-const JOIN_PATH = /(?:^|\/)join\/[a-fA-F0-9]{64}/
+const JOIN_PATH = /(?:\/join\/|\/r[/#])[a-fA-F0-9]{64}/
-export function redirectSystemPath({ path }: { path: string; initial: boolean }) {
+export function redirectSystemPath({ path }: { path: string; initial: boolean }): string | null {
try {
if (path.includes(`dataUrl=${getShareExtensionKey()}`)) {
return '/'
}
+ if (isPairUrl(path)) {
+ return null
+ }
if (JOIN_PATH.test(path)) {
return '/receive'
}
diff --git a/apps/mobile/app/_layout.tsx b/apps/mobile/app/_layout.tsx
index 6703ca7..cc6f3c2 100644
--- a/apps/mobile/app/_layout.tsx
+++ b/apps/mobile/app/_layout.tsx
@@ -38,6 +38,7 @@ import { useAlterSendFonts } from '../src/theme/useAlterSendFonts'
import { startAppStateBridge } from '../src/lifecycle/appStateBridge'
import { startBackgroundTransferService } from '../src/lifecycle/backgroundTransferService'
import { startDeepLinkHandler } from '../src/lifecycle/deepLinkHandler'
+import { DeepLinkGate } from '../src/lifecycle/DeepLinkGate'
import { getSavedLocalePreference } from '../src/lifecycle/localePreferenceStorage'
import {
getSavedThemePreference,
@@ -234,6 +235,7 @@ function AppShell() {
<AccountProvider>
<ShareIntentHandler />
<ThemedStack />
+ <DeepLinkGate />
<UpdateBanner />
<PairRequestBanner />
<InviteBanner />
diff --git a/apps/mobile/src/lifecycle/DeepLinkGate.tsx b/apps/mobile/src/lifecycle/DeepLinkGate.tsx
new file mode 100644
index 0000000..0c9037f
--- /dev/null
+++ b/apps/mobile/src/lifecycle/DeepLinkGate.tsx
@@ -0,0 +1,20 @@
+import { useEffect } from 'react'
+import { useNavigationContainerRef } from 'expo-router'
+import { markRouterReady } from './deepLinkHandler'
+
+export function DeepLinkGate() {
+ const navigationRef = useNavigationContainerRef()
+
+ useEffect(() => {
+ const markWhenReady = () => {
+ if (navigationRef.isReady()) markRouterReady()
+ }
+
+ const unsubscribe = navigationRef.addListener('state', markWhenReady)
+ markWhenReady()
+
+ return unsubscribe
+ }, [navigationRef])
+
+ return null
+}
diff --git a/apps/mobile/src/lifecycle/deepLinkHandler.ts b/apps/mobile/src/lifecycle/deepLinkHandler.ts
index b3058dd..9be7537 100644
--- a/apps/mobile/src/lifecycle/deepLinkHandler.ts
+++ b/apps/mobile/src/lifecycle/deepLinkHandler.ts
@@ -10,20 +10,33 @@ import {
let started = false
let subscription: { remove(): void } | null = null
+let routerReady = false
+let pendingPairCode: string | null = null
export function startDeepLinkHandler(): () => void {
if (started) return teardown
started = true
- void Linking.getInitialURL().then((url) => {
- if (url) handleUrl(url)
- })
+ Linking.getInitialURL()
+ .then((url) => {
+ if (url) handleUrl(url)
+ })
+ .catch(() => {})
subscription = Linking.addEventListener('url', ({ url }) => handleUrl(url))
return teardown
}
+export function markRouterReady(): void {
+ if (routerReady) return
+ routerReady = true
+
+ const code = pendingPairCode
+ pendingPairCode = null
+ if (code) openPairing(code)
+}
+
const ALLOWED_SCHEMES = ['altersend://', 'com.altersend.mobile://']
const RECEIVE_LINK_PREFIX = `${webAppUrl}/r`
@@ -37,11 +50,7 @@ function handleUrl(url: string): void {
if (!code) return
if (isPairUrl(url)) {
- try {
- router.navigate({ pathname: '/devices', params: { pairCode: code } })
- } catch (err) {
- console.warn('deepLinkHandler: pair navigate failed', err)
- }
+ openPairing(code)
return
}
@@ -50,19 +59,29 @@ function handleUrl(url: string): void {
return
}
+ joinSession(code).catch((err) => {
+ console.warn('deepLinkHandler: joinSession failed', err)
+ })
+}
+
+function openPairing(code: string): void {
+ if (!routerReady) {
+ pendingPairCode = code
+ return
+ }
+
try {
- router.navigate('/receive')
+ router.navigate({ pathname: '/devices', params: { pairCode: code } })
} catch (err) {
- console.warn('deepLinkHandler: navigate failed', err)
+ console.warn('deepLinkHandler: pair navigate failed', err)
}
- void joinSession(code).catch((err) => {
- console.warn('deepLinkHandler: joinSession failed', err)
- })
}
function teardown(): void {
if (!started) return
started = false
+ routerReady = false
+ pendingPairCode = null
if (subscription) {
subscription.remove()
subscription = null