Choosing when to hydrate
Decide when JavaScript takes over the first screen, and what happens to taps that arrive before it does.
The first screen is drawn from the snapshot without JavaScript. Hydration is the moment your app's JavaScript takes over that screen and it becomes interactive. You choose when that happens with HydrationStrategy, passed to any BakeNative view:
BakeNativeViewController(bundle: bundle, hydration: .idle)| Strategy | Takes over the screen | Good for |
|---|---|---|
.load (default) | Right after the snapshot is on screen | Most screens |
.idle | When the main thread is next idle | Screens shown during a busy transition |
.interaction | On the first tap or keystroke; that input is then delivered to your app | Screens users often only look at |
.never | Not automatically; call root.hydrate() yourself | Static screens, previews, your own trigger |
.after(milliseconds:) | After a fixed delay | Measuring and testing |
The names follow Octane's <Hydrate when={…}> strategies. Octane applies them to one part of a page; BakeNative applies them to the whole screen.
JavaScript starts after the first frame
After the host commits the baked frame, Hermes loads your bundle on the main thread. The strategy controls when your app takes over the existing views. With .never, Hermes starts when you explicitly request hydration after the first frame.
Taps before JavaScript is ready
Users can tap the snapshot before hydration. Those taps are not lost: BakeNative holds them, and once your app has taken over the screen it delivers each one to the element that was tapped, in order. With .interaction, the first tap is what starts hydration.
BakeNative drops held input instead of replaying it when it cannot be sure it lands on the same element:
- the snapshot was built from a different bundle,
- your app's first render did not match the snapshot (the screen is rebuilt), or
- hydration failed.
Measuring it
The launch metrics show the effect of each strategy:
if case .metrics(let m) = event {
print(m.phase, // .frame → .hydrating → .adopted
m.hydrationDelayMs, // snapshot on screen → takeover requested
m.jsReadyMs, // launch → first render ready
m.frameToInteractiveMs, // snapshot on screen → interactive
m.buffered, m.replayed) // taps held / delivered
}The Swift API reference lists the available strategies.
Testing that the takeover is invisible
The takeover must not change a single pixel: the snapshot and the adopted screen are supposed to be the same picture. Debug builds have two tools to prove it.
Hold hydration. Launch with the environment variable BAKE_HYDRATION=manual (or the launch argument -BakeHydration manual) and every root keeps its snapshot on screen until it receives the Darwin notification dev.bakenative.hydrate:
xcrun simctl spawn <udid> notifyutil -p dev.bakenative.hydrateAny strategy name works too (BAKE_HYDRATION=after:2000). Release builds ignore both.
Adoption check. Right after adoption, a Debug build compares every snapshot view with itself after the first live layout: position and size, opacity, visibility, the paint fields of its style (colors, borders, radii, fonts, line height, text runs) and the canvas color. It logs the first difference with its row, element and property:
[BakeNative] adoption check: 2 difference(s) in 33 rows; first: row 4 <h1> #12 fontWeight: snapshot 400 → live 700BakeNativeUIView.adoptionCheck holds the full report, so a test can fail on it.
The repository's regression test (tests/e2e/flicker, pnpm --filter @bake-native/e2e-tests flicker) uses both on the iOS simulator: for each example, snapshot source (on-device cache and build-time app.baked), light and dark, and default and large Dynamic Type, plus a switch to dark while the snapshot is shown and a launch at a larger text size than the snapshot was cached at, it captures the held snapshot, hydrates, captures the adopted screen, and fails when more than 0.01% of the pixels changed or the adoption check found a difference. Diff images land in tests/e2e/.e2e/flicker/<cell>/.
The same test on other hosts
Every host (Android, Windows, macOS) follows the same format so one test design covers them all:
- A debug-only override of the hydration strategy, read at root creation (
BAKE_HYDRATION/ the platform's launch arguments), withmanualmeaning "hold until told". - A platform signal that hydrates every held root (a Darwin notification on Apple platforms; a broadcast intent on Android; a named event on Windows).
- An adoption check run inside
adopt, comparing each snapshot view's frame and paint with its first live layout, logged with the first difference and exposed for tests. - A launch-state report the test can read (snapshot source and cache result, phase, views drawn and created, the adoption check), so a run that never showed a snapshot fails instead of comparing two live screens.
- The pixel test: held snapshot twice (noise floor), hydrate, adopted screen, diff with the same tolerance, over the same light/dark × text size × example matrix.