Brownfield
Put Octane screens into an existing native app - as a whole screen or as one section of a native screen - and let the two sides talk both ways.
A brownfield app is an app that already exists, written in UIKit, SwiftUI, Android views or Win32. You add Octane where it helps and keep the rest native. This page uses iOS for the examples; the same building blocks exist on Android, macOS and Windows.
There are two shapes:
- A whole screen. An Octane screen pushed on the app's navigation stack, presented, or shown in a tab.
- A section of a native screen. An Octane view in the middle of a native screen, for example a card in a table cell, sized to its content. The native screen scrolls as one.
Setup (adding BakeNativeKit and the bundle to the app) is on
Embed in an iOS app. For Android, macOS and Windows see
Android, macOS and Windows.
A whole screen
import BakeNativeKit
let screen = BakeNativeViewController(bundle: bundle)
navigationController?.pushViewController(screen, animated: true)Pass the screen's native modules as one registry, from the generated
BakeNativeBundleModules.root(...) (the modules the generated code creates plus the screen's own):
BakeNativeViewController(bundle: bundle, modules: BakeNativeBundleModules.root(...))
(Module registry).
To open the screen without a delay, prepare it while the previous screen is up: Prepare a screen before it opens.
A section of a native screen
Open the view with fitsContent. It then has no scrolling of its own and is as tall as its page,
so the native screen around it scrolls.
/// An Octane section for a table cell or a stack view: as tall as its page.
@MainActor final class OctaneSection: UIView {
let bakenative: BakeNativeView
/// Called when the page height changes, so the host can resize the cell.
var onHeight: ((CGFloat) -> Void)?
init(bundle: BakeNativeBundle, modules: BakeNativeModuleRegistry? = nil) {
bakenative = BakeNativeView(bundle: bundle, options: BakeNativeView.Options(fitsContent: true),
modules: modules)
super.init(frame: CGRect(x: 0, y: 0, width: 0, height: 1))
bakenative.onContentHeight = { [weak self] h in
self?.invalidateIntrinsicContentSize()
self?.onHeight?(h)
}
addSubview(bakenative)
bakenative.launch()
}
required init?(coder: NSCoder) { fatalError() }
override var intrinsicContentSize: CGSize { bakenative.intrinsicContentSize }
override func layoutSubviews() {
super.layoutSubviews()
bakenative.frame = bounds
}
}Give the page a transparent background so the native screen shows around the card. In this mode
vh units, position: fixed and host.on('scroll') have no useful meaning, because the page is
laid out at its full height. Details for every platform:
Module registry: embedded views.
Talk both ways
| Direction | How | When |
|---|---|---|
| Native to JS, at start | Props: props: [...] on the view, host.props in JS | Values the first render needs |
| Native to JS, later | Host events: sendEvent(_:json:) on the view, host.on in JS | A native control changed |
| JS to native | A native module of the screen: native.<Module>.<function>() | The Octane view changes something native |
JS to native always goes through a module, never through an app-wide event channel. The module takes the native screen as a constructor argument. The generated registration includes a factory that takes such modules as required parameters, so a screen that forgets one does not compile (Module registry).
Props are installed before the bundle runs, so the first render sees them. The build-time snapshot is baked without props: a first screen that depends on props mounts from JS instead of adopting the snapshot (Native modules).
Example: an order screen
A UIKit order screen shows a native order summary pinned at the top, a native segmented control for the delivery speed, and an Octane delivery card in a table cell. The card's tip buttons change the native order total. The segmented control changes the card's arrival time.
1. The module
The module needs the order screen. It takes the screen as a constructor argument and holds it
weakly, so it never keeps the screen alive. setTip runs on the JS thread and hops to the main
thread before it touches UIKit.
Its spec, native/order-screen/OrderScreen.native.ts in the Octane app:
import type { Int, NativeModule } from '@bake-native/octane';
export interface OrderScreen extends NativeModule {
/** The tip in dollars (0: none); the summary's total follows. */
setTip(dollars: Int): void;
}bake build generates OrderScreenSpec; the host app implements it:
import BakeNativeAppModules
import BakeNativeKit
final class OrderScreenModule: OrderScreenSpec {
private weak var screen: OrderViewController?
init(screen: OrderViewController) { self.screen = screen }
func setTip(dollars: Int) {
DispatchQueue.main.async { [weak self] in MainActor.assumeIsolated { self?.screen?.setTip(dollars) } }
}
}2. The registration
bake build sees that the card calls native.OrderScreen and writes the registration, with a
factory that takes OrderScreenModule as a parameter (optional here, since the card checks
hasNative before it calls; a module the bundle calls unguarded is a required parameter). It
returns the registry a screen passes as modules:.
// Generated (.bake/native/ios/BakeNativeBundleModules.swift; add it to the app target once).
enum BakeNativeBundleModules {
static var entries: [BakeNativeModuleEntry] { BakeNativeAppModules.entries }
static func root(orderScreen: (any OrderScreenSpec)? = nil) -> BakeNativeModuleRegistry {
let registry = BakeNativeModuleRegistry(entries: entries)
registry.register(contentsOf: [orderScreen].compactMap { $0 })
return registry
}
}3. The screen passes its module
The order screen creates its OrderScreenModule with itself and passes it to the generated
factory when it opens the section. It also forwards the segmented control to the card as a host
event:
final class OrderViewController: UIViewController {
private var tip = 0
private lazy var section = OctaneSection(bundle: bundle,
modules: BakeNativeBundleModules.root(orderScreen: OrderScreenModule(screen: self)))
/// Called by OrderScreenModule, on the main thread.
func setTip(_ dollars: Int) {
tip = max(0, dollars)
updateSummary()
}
private func observeSpeed(_ control: UISegmentedControl) {
control.addAction(UIAction { [unowned self, unowned control] _ in
let speed = control.selectedSegmentIndex == 1 ? "express" : "standard"
section.bakenative.sendEvent("speed", json: "\"\(speed)\"")
}, for: .valueChanged)
}
}4. The card
The card listens for the host event and calls the module from its handlers:
import { useEffect, useState } from 'octane';
import { hasNative, native, host, Text, View } from '@bake-native/octane';
const TIPS = [0, 2, 5];
export function DeliveryCard() @{
const [speed, setSpeed] = useState('standard');
const [tip, setTip] = useState(0);
useEffect(() => host.on('speed', (s) => setSpeed(s === 'express' ? 'express' : 'standard')), []);
<View class="card">
<Text class="big">{speed === 'express' ? 'Arriving 1:55 PM' : 'Arriving 2:40 PM'}</Text>
<div class="tips" role="group" aria-label="Tip">
@for (const t of TIPS; key t) {
<button class={t === tip ? 'tip on' : 'tip'} onClick={() => {
setTip(t);
if (hasNative('OrderScreen')) native.OrderScreen.setTip(t);
}}>
{t === 0 ? 'No tip' : '$' + t}
</button>
}
</div>
</View>
}- Native calls happen in handlers, never during render: the build-time snapshot has no native modules.
- The same bundle also serves a SwiftUI screen that has no order screen, so the call is guarded
with
hasNative('OrderScreen'). That makes the module optional: a screen without it still starts. Unguarded, the boot check would refuse to start a screen that does not register it.
The complete app is examples/ios-brownfield in the repository, with the card in
examples/brownfield-embed.
Several apps in one host
A host can show screens from more than one React or Octane app. Two ways:
- Bundles in the app. Each app is built with
bake buildand copied into the host. One app-layer registry covers the modules of every bundle:bake native linktakes several--bundleoptions and writes the registration of their union (System modules). - App packages. Each app is its own native part with its own modules, on one shared runtime. The host adds the parts and calls their entry points: Micro-native packages.
Cost
What one Octane screen adds to an existing app (time to the first frame, binary size, memory) is measured on Embedding cost.
Embed in a Windows app
Build BakeNative on Windows, show a bundle in an existing Win32 app, and ship updates.
Embedding cost
What one BakeNative screen adds to an existing iOS or macOS app: time to the first frame, time to interactive, binary size and memory, next to the same screen written in UIKit, AppKit and SwiftUI.