Skip to content

Instantly share code, notes, and snippets.

@aniltirola
Last active November 20, 2019 04:41
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save aniltirola/369e08971e18662772d8239bc101fe6f to your computer and use it in GitHub Desktop.
expo deeplink fix
/*
This gist is a workaround-fix for issue https://github.com/expo/expo/issues/2128
the bug affects only android-standalone-apps!
environment for testing:
- create a blank expo app
- add to app.json: "scheme": "expodeeplinktest"
- expo eject
- start expo development server: expo start
- start android-studio, open the folder 'android', and start your app from android studio
howto test:
adb shell am start -a android.intent.action.VIEW -d "expodeeplinktest://path/to/my/detailscreen"
*/
import React from 'react'
import { Text, View, Linking } from 'react-native'
const consolePrefix = function () {
const date = new Date()
return date.toLocaleTimeString() + ':' + date.getMilliseconds() + ':'
}
export default class App extends React.Component {
constructor(props) {
super(props)
global.timestampLastDeepLinkEvent = -1
}
async componentDidMount() {
Linking.addEventListener('url', this._linkingOnEvent)
this._linkingCheckInitialUrl()
}
componentWillUnmount() {
Linking.removeEventListener('url', this._linkingOnEvent);
}
_linkingOnEvent (event) {
console.log(consolePrefix(), 'deeplink event', event)
const now = Date.now()
const elapsed = now - global.timestampLastDeepLinkEvent
if (elapsed < 1000) {
console.log(consolePrefix(), 'WARNING: too many events inside 1 second, IGNORING event')
}
else {
global.timestampLastDeepLinkEvent = now
console.log(consolePrefix(), 'event is valid, lets do something with the url')
}
}
_linkingCheckInitialUrl = async () => {
const url = await Linking.getInitialURL()
console.log('initial url on appstart:', url)
const regexForDeeplink = /expodeeplinktest:\/\/[a-zA-Z]+/
if (regexForDeeplink.test(url)) {
console.log('app was started by deeplink')
this._linkingOnEvent({url: url})
}
else {
console.log('app was NOT started by deeplink')
}
}
render() {
return (
<View style={{flex: 1, alignItems: 'center', justifyContent: 'center'}}>
<Text>Deeplinktest app</Text>
</View>
)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment