Add a web client
The web shell is a library, mobiler-web. It drives your core and
renders its widget tree to the DOM with Leptos, compiled to WebAssembly. Your web client is a small Trunk
project with one line of Rust.
Move the app into its own crate
Section titled “Move the app into its own crate”mobiler new puts the app in shared/, which also carries the native bindings. To share it with the web,
move the app (Model, Msg, MobilerApp, App) into a plain library crate, here app-core, and make
shared re-export it:
-
Move
shared/src/app.rstoapp-core/src/lib.rs, and give the crate a manifest:app-core/Cargo.toml [package]name = "app-core"version = "0.1.0"edition.workspace = true[dependencies]crux_core.workspace = trueserde = { workspace = true, features = ["derive"] }mobiler-core = "0.49" -
Add it to the workspace in the root
Cargo.toml:members = ["shared", "app-core"]. -
Point
sharedat it. Inshared/Cargo.toml, addapp-core = { path = "../app-core" }under[dependencies]. Inshared/src/lib.rs, replacemod app;andpub use app::*;with:pub use app_core::*;
The mobile app builds as before: shared still provides the bindings, now over app-core. Keep
app-core’s mobiler-core version equal to shared’s; mobiler upgrade moves the one in shared and
lists the other for you to change.
The barbershop demo is laid out like this.
The web crate
Section titled “The web crate”Keep it in its own Cargo workspace (it builds for wasm32-unknown-unknown):
[package]name = "myapp-web"version = "0.1.0"edition = "2021"
[workspace]
[dependencies]mobiler-web = "0.52"app-core = { path = "../app-core" }fn main() { mobiler_web::run::<app_core::App>();}<!DOCTYPE html><html lang="en"> <head> <meta charset="utf-8" /> <meta name="viewport" content="width=device-width, initial-scale=1" /> <title>My App</title> <link data-trunk rel="rust" /> </head> <body></body></html>Then cd web && trunk serve, and open the address it prints.
One thing to know about this layout: mobiler watch follows shared/src, not app-core/src. After
moving the app, run mobiler dev again after a change, or keep trunk serve open for the web.
What to know
Section titled “What to know”- No CSS needed. The shell injects its own stylesheet, first in
<head>, so your own stylesheet can override any widget class. - Every widget renders, including charts (SVG), video, maps (MapLibre GL), PDF and web views.
- Built-in capabilities work through browser APIs, with limits a page can’t get past: a copied text
can’t be marked sensitive or expire, and the clipboard needs a secure context (HTTPS or localhost);
cx.device_infoanswersNone; and haptics do nothing where the browser has no vibration (iOS Safari). - Plugins:
websocket’s message stream,transfer,sharefileandsaveimagework on the web.sharefileuses the browser’s share sheet where it can share files and otherwise downloads the file (ok: true, "downloaded");saveimagealways downloads, since a browser has no photo library. Both fetch thesourcefirst, so a cross-origin URL needs CORS headers on its server, or the reply isfetch_failed. Any other plugin request answersok: false(“not available”), and a stream from one never sends an event; handle that inupdate. - Fonts and the launch screen: when the app has
web/index.html,mobiler buildanddevalso write the[fonts]and[splash]options there. A web-only workflow runsmobiler fonts syncandmobiler splash syncitself. - Tabs: the web keeps each tab’s scroll position and switches at once, like the native shells.
For a product with a server too, see One core for mobile, web and server.