Skip to content

feat(lynx): deep linking - #9

Merged
upupming merged 1 commit into
mainfrom
feat/linking
Sep 7, 2026
Merged

upupming merged 1 commit into
mainfrom
feat/linking

Conversation

@upupming

Copy link
Copy Markdown
Contributor

Adds deep linking to @react-navigation/lynx.

What it does

React Navigation splits linking in two. @react-navigation/core turns a path into navigation state and back — getStateFromPath, getPathFromState, getActionFromState — and none of it touches a platform. The platform package answers two questions, and both are already injectable as getInitialURL / subscribe:

  • which URL launched us
  • how do later ones arrive

So this PR only fills those in. Everything downstream is reused untouched.

How the host hands a route over

A Lynx card is not the process that receives the link — the host app is — so the host has to pass it along. Two ways, both supported:

initData.__navigation — set at loadTemplate, updated later via updateMetaData. Read synchronously at startup, so unlike React Native's promise-based Linking.getInitialURL() there is nothing to await and the first render already lands on the right route.

initData is state rather than an event, so navigating twice to the same route would leave the value untouched and be dropped. Hence nonce:

TemplateData.fromMap(mapOf(
  "__navigation" to mapOf("route" to route, "nonce" to System.currentTimeMillis())
))

A reactnavigation.url global event — for hosts that would rather push than update data.

lynxView.sendGlobalEvent("reactnavigation.url", JavaOnlyArray.of(
  JavaOnlyMap.from(mapOf<String, Any>("url" to route))
))

The name is namespaced deliberately. React Native can afford the bare url because RCTLinkingManager is its own event emitter and subscription goes through Linking.addEventListener, so the name is scoped to that module. Lynx's GlobalEventEmitter is a single namespace shared by the whole card and its host, where url is short enough to collide with something unrelated. The bare url event is still accepted, so a host already speaking React Native's protocol needs no second code path.

Usage

<Navigation
  linking={{
    config: {
      screens: {
        Home: '',
        Depth: {
          path: 'depth/:level',
          parse: { level: Number },
          stringify: { level: String },
        },
      },
    },
  }}
/>

Routes that start with / skip prefix stripping entirely, so a host that hands over a path rather than a full schema needs no prefixes at all.

One ReactLynx difference worth flagging

The container reads its imperative handle from NavigationContainerRefContext instead of a ref. Under ReactLynx a ref on BaseNavigationContainer yields the Preact component instance — props, render, base, … — not the handle, so ref.current.getRootState() throws. Reading it from inside the tree sidesteps the difference.

Verification

11 unit tests cover reading the launch route, nonce deduping, both event names, unsubscribe, and a host with no emitter.

Verified end to end on a Pixel 4 XL through LynxExample:

  • cold start with -e route "/depth/3" lands on Level 3, with parse having turned "3" into a number
  • with the card already on Level 3, sending /events navigates to it

The example app in examples/stack now passes a linking config, and LynxExample's DebugActivity reads a route intent extra and forwards it both ways — that lives in the lynx-screens repo and is a separate PR.

@changeset-bot

changeset-bot Bot commented Aug 27, 2026 •

Copy link
Copy Markdown

🦋 Changeset detected

Latest commit: 9016738

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package
Name Type
@react-navigation/lynx Minor

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@pkg-pr-new

pkg-pr-new Bot commented Aug 27, 2026 •

Copy link
Copy Markdown
pnpm add https://pkg.pr.new/@react-navigation/lynx@9016738

commit: 9016738

React Navigation splits linking in two: `@react-navigation/core` turns a
path into navigation state and back, and the platform package answers
which URL launched the app and how later ones arrive. Only those two
were missing on Lynx, so core is untouched.

A card is not the process that receives the URL - the host is - so the
host hands it over through `initData.__navigation` for a cold start and
a `reactnavigation.url` global event for later ones. The event name is
namespaced because `GlobalEventEmitter` is one namespace shared with the
host, and initData carries a nonce because it is state, not an event:
without one, the same route twice would be dropped.

Unlike React Native there is nothing to await - initData is already in
hand when the card starts - so the first render lands on the right route
rather than on the fallback.

`extractPathFromURL` is taken from `@react-navigation/native` unchanged,
along with its 15 test cases, so prefix stripping behaves identically.
@upupming
upupming merged commit e87b888 into main Sep 7, 2026
6 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant