# How it works

What runs where, and the terms the rest of the docs use: the core, frames, origins, rules and the bridge.

![The app sends rules and settings to the native WebView core and gets state and events back; the core answers the page's navigations; the bridge links the app and allowed origins](<https://rasmuskard.github.io/react-native-minimal-webview/guides/how-it-works.svg>)

## The parts

| Part | What it is | What it does |
| --- | --- | --- |
| The page | Your site, in the platform's WebView: `WKWebView` on iOS, Android System WebView on Android | Runs in the WebView's own process, the renderer, apart from the app |
| The core | This package's Swift and Kotlin around that WebView | Decides navigations, keeps the navigation state, answers the page's requests, carries the bridge |
| Your app's JS | The `<WebView>` props, its ref and your callbacks | Hands the core its rules and settings, and hears what happened |
| A native hook | Your own Swift or Kotlin, optional | Gets the native WebView itself ([native hooks](<https://rasmuskard.github.io/react-native-minimal-webview/guides/native-hooks/index.md>)); its JS side goes in the [`extensions` prop](<https://rasmuskard.github.io/react-native-minimal-webview/typescript/react-native-minimal-webview/interfaces/WebViewProps/#extensions>), as the extensions package's hooks do |

The core decides natively, from what the props gave it, so a busy JS thread doesn't hold the page up. JS is asked only where a prop says so: an `ask` rule ([`onNavigationRequest`](<https://rasmuskard.github.io/react-native-minimal-webview/typescript/react-native-minimal-webview/interfaces/WebViewProps/#onnavigationrequest>)), an `external` navigation when you pass [`onOpenExternal`](<https://rasmuskard.github.io/react-native-minimal-webview/typescript/react-native-minimal-webview/interfaces/WebViewProps/#onopenexternal>), and the page's requests for files and permissions ([page requests](<https://rasmuskard.github.io/react-native-minimal-webview/guides/examples/index.md#page-requests>)).

The **bridge** carries calls between your app's JS and the page's: the page has `window.nativeBridge`, and the app has the object [`createBridge`](<https://rasmuskard.github.io/react-native-minimal-webview/typescript/react-native-minimal-webview/functions/createBridge/>) makes, passed as the [`bridge`](<https://rasmuskard.github.io/react-native-minimal-webview/typescript/react-native-minimal-webview/interfaces/WebViewProps/#bridge>) prop.

## Frames and origins

The **main frame** is the page's top-level document, the one whose URL the navigation state shows. Every `<iframe>` in it is a frame of its own.

An **origin** is a scheme, host and port, as the page's `location.origin` writes it: `https://example.com`. A page from an `html` source without `baseUrl`, a `data:` URL or a sandboxed iframe has an opaque origin, which only `'*'` matches.

The core gives the main frame more than it gives iframes, and trusts only the origins you name:

| Feature | Main frame | Iframes |
| --- | --- | --- |
| [Navigation state](<https://rasmuskard.github.io/react-native-minimal-webview/typescript/react-native-minimal-webview/type-aliases/NavigationState/>) | Its `url` is this frame's | Never change its `url` |
| [Navigation rules](<https://rasmuskard.github.io/react-native-minimal-webview/typescript/react-native-minimal-webview/type-aliases/NavigationRule/#mainframe>) | Decided | Decided on iOS; on Android only for non-http(s) URLs |
| [The bridge](<https://rasmuskard.github.io/react-native-minimal-webview/typescript/react-native-minimal-webview/type-aliases/BridgeOptions/#allowedorigins>) | On the bridge's `allowedOrigins` | Never |
| [Injected scripts](<https://rasmuskard.github.io/react-native-minimal-webview/typescript/react-native-minimal-webview/type-aliases/InjectedScript/>) | On the script's `origins` | Only with `allFrames`, each frame matched by its own origin |
| [`evaluate`](<https://rasmuskard.github.io/react-native-minimal-webview/typescript/react-native-minimal-webview/type-aliases/WebViewRef/#evaluate>) | Runs here | Never |
| [Camera and microphone](<https://rasmuskard.github.io/react-native-minimal-webview/typescript/react-native-minimal-webview/type-aliases/PermissionRequest/>) | Asks your `onPermissionRequest` | Denied without asking, except on Android one on the main frame's origin |

## How a navigation is decided

1. These load in place without the rules: the `source` load, [`load(url)`](<https://rasmuskard.github.io/react-native-minimal-webview/typescript/react-native-minimal-webview/type-aliases/WebViewRef/#load>), reloads, back and forward, form POSTs, and `about:`, `data:` and `javascript:` URLs unless they open a [new window](<https://rasmuskard.github.io/react-native-minimal-webview/typescript/react-native-minimal-webview/type-aliases/NavigationRule/#newwindow>). A redirect of the `source` load or a `load` goes to the rules; [`navigationRules`](<https://rasmuskard.github.io/react-native-minimal-webview/typescript/react-native-minimal-webview/interfaces/WebViewProps/#navigationrules>) lists each case per platform.
2. Any other navigation goes down [`navigationRules`](<https://rasmuskard.github.io/react-native-minimal-webview/typescript/react-native-minimal-webview/interfaces/WebViewProps/#navigationrules>), and the first rule that matches decides: `allow`, `block`, `external` or `ask`.
3. A navigation no rule matches is blocked.

Without the prop, the rules are [`defaultNavigationRules`](<https://rasmuskard.github.io/react-native-minimal-webview/typescript/react-native-minimal-webview/variables/defaultNavigationRules/>). [`onNavigation`](<https://rasmuskard.github.io/react-native-minimal-webview/typescript/react-native-minimal-webview/interfaces/WebViewProps/#onnavigation>) reports every decision with the index of the rule that made it.

## Which props recreate the WebView

Only two props recreate the native WebView when they change, which reloads the page: [`bridge`](<https://rasmuskard.github.io/react-native-minimal-webview/typescript/react-native-minimal-webview/interfaces/WebViewProps/#bridge>), compared by identity ([keep it stable](<https://rasmuskard.github.io/react-native-minimal-webview/guides/troubleshooting/index.md#the-page-reloads-or-the-navigation-state-starts-over>)), and [`ios.configuration`](<https://rasmuskard.github.io/react-native-minimal-webview/typescript/react-native-minimal-webview/type-aliases/IOSSettings/#configuration>), compared by value. Everything else, callbacks included, can be written inline.
