Skip to content

v1.9.0

Latest

Choose a tag to compare

@gronxb gronxb released this 11 Nov 05:12
3e9037b

Highlights

delegate(): use React hooks in native methods

Native methods are declared at module scope, so until now they couldn't call useNavigation(), read a context value, or touch component state. delegate() declares the method on the bridge, and the screen that renders the WebView implements it with the delegate prop:

export const appBridge = bridge({
  goBack: delegate(),
  getUser: delegate({ input: z.object({ id: z.string() }), output: userSchema }),
});

function WebViewScreen() {
  const navigation = useNavigation();
  return (
    <WebView
      delegate={{
        goBack: async () => navigation.goBack(),
        getUser: async ({ id }) => fetchUser(id),
      }}
    />
  );
}
  • Per-WebView implementations. Each WebView runs its own implementations with its latest props, so a stack of WebView screens never calls the wrong screen's navigation.
  • Type-checked delegate prop. Every delegated method must be implemented, with a matching signature.
  • Missing implementations are hidden. A method that a WebView doesn't implement is not exposed to its page.

Guide: Using React Hooks in Native Methods

Standard Schema: types inferred, values validated

delegate({ input, output }) and postMessageSchema take Standard Schemas (Zod, Valibot, ArkType, ...) directly. Types are inferred from the schemas, and values are validated on native:

  • Direction of each type. The sender passes the schema's input type, and the receiver gets its output type, with transforms applied.
  • Validation errors. Invalid values fail with BridgeValidationError. Values that can't survive JSON (Date, BigInt, Map, Set, objects with methods, undefined array items) are rejected at compile time.
  • postMessageSchema events. Events no longer need a { validate } wrapper: postMessageSchema({ eventName: z.object({ message: z.string() }) }). Existing { validate } objects keep working.

Android: the bridge recovers when methods arrive late

On Android, injectedJavaScriptBeforeContentLoaded can run after the page's linkBridge(). Before this fix, native methods then stayed unknown, and calls fell back with "Method not found in native". Now:

  • The page asks native for its methods.
  • Calls made in the meantime are sent to native.
  • useBridgeStore and useBridgeStatus update when the methods arrive.

This fixes #93, #102 and #107.

Native methods that wait for the user are no longer cut off

A native method that waits for the user (a confirm dialog, a picker, an in-app browser) used to be cut off by linkBridge's timeout after 2 seconds. Native now acknowledges every call as soon as it starts running it. From then on the page waits for the real result.

Behaviour changes

  • timeout only waits for native to acknowledge a call, not for the method to finish. A slow native method that used to resolve to undefined or reject with Error("Timeout") now resolves with its real result. With native builds before 1.9.0, timeout still limits the whole call.
  • Packages are built with tsdown and target ES2015 instead of ES5. The bundles are about half the size. Every WebView React Native supports runs ES2015.
  • Zod 4 schemas passed straight to postMessageSchema are validated. Before, their unrelated validate() method made the page receive true/false instead of the data.

Fixes

  • postMessage keeps reaching mounted WebViews when WebViews unmount out of order (#119). Thanks @anxi01!
  • A throwing onFallback or fallback callback no longer changes or stalls the call that triggered it (#128). When those callbacks fire is unchanged and documented.
  • CommonJS consumers now resolve .d.cts types (#126). arethetypeswrong reports no problems for any package.

Docs

  • New guide for delegate(), with use cases (#123).
  • Standard Schema guides for delegate() and postMessage.
  • Complete linkBridge / createWebView references.
  • createLinkBridgeProvider hooks in the React guide.
  • An up-to-date CONTRIBUTING guide.

What's Changed

  • fix(react-native): remove own webview ref on unmount instead of popping the last one by @anxi01 in #119
  • fix: recover bridge hydration when injected methods arrive late by @gronxb in #121
  • feat(react-native): add delegate() for native methods that need React hooks by @gronxb in #122
  • docs: surface delegate() and add use-case guides by @gronxb in #123
  • fix: stop cutting off native methods that outlive linkBridge's timeout by @gronxb in #124
  • feat: infer delegate() and postMessage types from Standard Schemas by @gronxb in #125
  • build: switch from tsup to tsdown by @gronxb in #126
  • feat(react-native): take delegate() schemas as { input, output } by @gronxb in #127
  • fix: isolate onFallback/fallback exceptions and document fallback behaviour by @gronxb in #128
  • chore: update versions by @github-actions[bot] in #120

New Contributors

Full Changelog: v1.8.0...v1.9.0