Handling Incoming Links

Handling an Incoming Link in Your App

App Lifecycle's listener turned booknest://lab/ into /lab/ with a string replace, which breaks on an https link. BookNest now converts every link with one function:

One function for every kind of link, in src/js/lab.jsJavaScript
// booknest://books/3/ and https://booknest.example.com/books/3/ both become /books/3/
export function linkToRoute(url) {
  const { protocol, host, pathname, search } = new URL(url);
  if (protocol === 'booknest:') return `/${host}${pathname}${search}`; // custom scheme
  return pathname + search; // an App Link or Universal Link on the site's own domain
}

The listener now calls router().navigate(linkToRoute(url)). The URL parser reads the first segment after booknest:// as the host, so the custom-scheme branch puts it back. An App Link started the stopped app, and a custom-scheme link followed:

A cold start from an App Link, then a custom-scheme linkShell
adb -s emulator-5558 shell am force-stop com.example.booknest
adb -s emulator-5558 shell am start -a android.intent.action.VIEW \
  -d https://booknest.example.com/books/3/
adb -s emulator-5558 shell am start -a android.intent.action.VIEW -d booknest://books/5/
Output
LAB appUrlOpen {"url":"https://booknest.example.com/books/3/"}
routeChange  / -> /books/3/ [book]
...
LAB appUrlOpen {"url":"booknest://books/5/"}
routeChange  /books/3/ -> /books/5/ [book]

appUrlOpen fired on the cold start too, so one listener covers both cases. Treat links as user input: an unknown path should reach the 404 route, and no link should buy anything unconfirmed.