# Hydration and adoption (/docs/concepts/adoption) **Hydration** is the moment your app's JavaScript takes over the snapshot screen. **Adoption** is how it does that: instead of mounting its first render as new views, Octapus matches it against the snapshot and hands the existing views to your app. The term comes from Octane's native renderer design, where a runtime's first commit is "accepted in adoption mode": it describes the screen that is already there rather than instructing the host to build it again. ## The sequence [#the-sequence] ```text launch ├─ draw the snapshot (no JS) phase: frame ├─ Hermes renders the first commit (in parallel) └─ the hydration strategy fires phase: hydrating ├─ match → views handed to your app phase: adopted (0 views created) │ held taps delivered └─ mismatch → snapshot dropped, normal render phase: remounted no snapshot → normal render phase: mounted ``` ## How the match works [#how-the-match-works] Octapus flattens both trees in the same order (parents before children, text nodes included) and compares them row by row: 1. Both trees must have the same number of rows. 2. Each row must have the same element type (`div`, `button`, text …) and the same parent. 3. Text inside a block of text must be identical. Props, styles and event handlers are not compared. Once the views belong to your app, its normal update path applies them, exactly as it would after any render. That keeps the match cheap and robust: a different class name or handler does not throw the snapshot away. When the trees match, each snapshot view is bound to the matching element of your app. No view is created, which is why `viewsCreatedOnAdopt` is expected to be `0`. ## When the trees differ [#when-the-trees-differ] The snapshot is dropped and your app's first commit is mounted from scratch. Nothing is lost except time, and the launch metrics record the first difference so you can fix the cause ([Troubleshooting](/docs/guides/troubleshooting#the-screen-rebuilds-on-launch-remounted)). ## Early input [#early-input] Because the snapshot is real native views, it can be tapped before JavaScript is ready. Octapus holds those events while the phase is `frame` or `hydrating`. After adoption it maps each one from the snapshot view to your app's element and delivers it if that element has a handler for the event, in the original order. Held events are dropped rather than delivered when the match failed or the snapshot came from a different bundle. ## Relation to Octane [#relation-to-octane] Octane hydrates server-rendered HTML with `` per boundary: the HTML is shown until the boundary's condition fires, then the boundary becomes interactive. Octapus applies the same idea to a native screen: the snapshot plays the server HTML, and `HydrationStrategy` plays `when` for the whole screen, with the same strategy names (`load`, `idle`, `interaction`, `never`). # Overview (/docs/concepts/overview) Native apps built on a JavaScript runtime usually start the same way: launch the app, start the JS engine, run the bundle, render, create every view, and only then show something. Octapus moves most of that work out of the launch. ## Three stages [#three-stages] ### 1. Build [#1-build] `oct build` compiles your app twice from the same source with Vite and Octane's compiler: - For **web**, a normal browser build that renders to the DOM. - For each **native** target, a single script that renders through Octane's universal renderer, compiled by `hermesc` into Hermes bytecode (`app.hbc`). Branches on `import.meta.env.OCT_TARGET` for other platforms are removed. ### 2. Bake [#2-bake] With `--bake`, `oct` runs your bytecode once in Hermes, takes the **first commit** (the element tree your app renders on its first pass), resolves its CSS for light and dark mode, and writes the result as the **[first-screen snapshot](/docs/concepts/snapshot)** (`app.ofrm`). The snapshot holds no positions or sizes, because those depend on the device. ### 3. Launch [#3-launch] When an Octapus view launches, two things happen at once: - **On the main thread**, the snapshot is laid out for the view's real size with the device's own fonts and drawn as native views. No JavaScript is involved, so this takes a few milliseconds. - **On a JS thread**, Hermes loads `app.hbc` and renders your app's first commit. When the [hydration strategy](/docs/guides/hydration) says so, Octapus compares Hermes's first commit with the snapshot. If they describe the same tree, it hands the views already on screen to your app instead of creating new ones ([adoption](/docs/concepts/adoption)). From then on, your app updates those views like any other render. ```text time ─▶ main thread │ draw snapshot │ │ adopt (0 views created) │ interactive JS thread │ start Hermes · run app.hbc · first render │ ``` ## Why it is fast [#why-it-is-fast] - **Nothing to compute at first paint.** Your CSS was resolved at build time, and drawing the snapshot is one forward pass over its rows. - **JavaScript is off the critical path.** It runs in parallel with the first paint instead of before it. - **No second build of the screen.** Adoption reuses the views that are already visible, so the takeover costs milliseconds and nothing flickers. ## Why it looks like the web [#why-it-looks-like-the-web] Native screens are not a re-interpretation of your app with native widgets. Octapus computes CSS, layout, text and painting the way a browser does, in one engine shared by every Apple platform, and the platform views only display the result. See [Rendering](/docs/concepts/rendering). # Rendering (/docs/concepts/rendering) Most cross-platform frameworks map your components onto platform widgets and let each platform lay them out. That is why the same app looks a little different on every platform. Octapus does the opposite: one shared engine computes everything that decides how a screen looks, and AppKit and UIKit only display the result. ## The shared engine [#the-shared-engine] | Step | What it does | | --- | --- | | **CSS** | A cascade modeled on WebKit: the same browser default stylesheet, selectors, specificity, inheritance, custom properties, media queries and computed values that Safari's `getComputedStyle` reports | | **Layout** | Browser-compatible block, flex and grid layout of an `html > body > #root` page | | **Text** | Line breaking, line height, alignment, letter spacing and decorations, built on CoreText. Measuring and drawing use the same code | | **Painting** | Backgrounds, borders, rounded corners, text and inline SVG, painted with CoreGraphics | The engine runs identically on macOS and iOS. A screen drawn from the snapshot and the same screen after your app takes it over go through the same painter, so they produce the same pixels. ## What the platform does [#what-the-platform-does] AppKit and UIKit views are thin hosts. For each element that needs a view, the renderer creates one (or reuses one), sets the frame the engine computed, and asks the engine's painter to draw into it. The platform contributes what only it can: real text fields, scrolling, input events, and the device's fonts and color scheme. Buttons and text fields start from one shared control stylesheet on every target, the browser included, so a button is the same box everywhere until your CSS says otherwise. ## Hermes and your app [#hermes-and-your-app] Your app runs on Hermes, compiled ahead of time to bytecode, so there is no parse or compile step at launch. Each Octapus view owns its own runtime, and JavaScript runs on a dedicated thread; your app's updates arrive on the main thread as batches of changes to the element tree, which the engine restyles, lays out and the renderer applies. The Swift modules you import talk to Hermes through a small C interface. That is why adding Octapus to an app does not require turning on Swift/C++ interop. # First-screen snapshot (/docs/concepts/snapshot) The **first-screen snapshot** (`app.ofrm`) is your app's first screen recorded at build time, so a device can show it before any JavaScript runs. It is the native counterpart of server-rendered HTML: content the user sees immediately, which your app's code takes over later. ## What it stores [#what-it-stores] | Stored | Why | | --- | --- | | Your app's first commit, as Octane produced it | The exact element tree and text your first render creates | | Scoped `