soda dev
soda dev builds your app, serves it, and watches it. When you save a page, a layout, a component
or an action, it patches the new code into the running app. Every open page re-renders in place,
without a reload.
Nothing the app holds is lost. On a server, its database and every session stay. Local-first, the data in the browser stays. In the browser, the address, the scroll and the form you're typing in stay.
What happens when you save#
Times are from saving the file to the open page showing it, measured on an M-series laptop.
| You change | What happens | Server (guestbook) | Local-first (template) |
|---|---|---|---|
| A page, layout, component or action | Patched in; open pages re-render | 0.6 to 0.8 s | 0.9 to 1.5 s |
| A new route file, or one removed | Patched in like an edit | 1.5 s | 1.2 to 1.7 s |
| A style, or a class in a view | Tailwind recompiles; the stylesheet is swapped in place | 0.3 s | 0.16 to 0.3 s |
The shell (web/app.html) | Pages reload into the new shell | 0.16 s | 0.9 s |
Another file in web/ | Copied in; pages reload | at once | at once |
| A struct, enum or union that already existed | Full rebuild and restart; pages reload | 4.9 s | 5.2 to 6.2 s |
Cargo.toml or build.rs | Full rebuild and restart; pages reload | as above | as above |
A static site runs as a server under soda dev, so the same rules apply. A page, layout or
component patches in 0.5 to 0.7 s. A change to what the site reads (the paths in its App::WATCH,
such as its Markdown) reloads that content and re-renders the open pages in about 0.3 s.
A patch takes as long as its crate takes to compile. In a bigger app it's slower: Soda IR (7,700 lines in one crate) patches its layout in 1.6 to 1.8 s, and 6.1 s the first time.
Why some changes rebuild#
A patch replaces code, not data. If you change the shape of a type the running app already holds,
the data in memory would be read with the old layout. So soda dev compares each saved file's type
definitions with the previous version. A changed type means a full rebuild; a new type is just new
code and patches normally.
After a full rebuild, a server starts again. Its database and sessions survive (sessions come back from their sealed cookies). Only what it kept in memory starts over. A local-first app rebuilds its state from its log when it starts, so it loses nothing.
Errors#
| Problem | What you see |
|---|---|
| A file doesn't parse | The error and its location over the page, in 0.2 s, until the file parses |
| It parses but doesn't compile | The compiler's message over the page, then its location and the code around it |
| A page panics (server) | The panic and where it happened, in place of the page. Fix it and the page re-renders |
| A panic (local-first) | The instance stops, and "The instance stopped" shows over the page with the panic. Fix it and save: a fresh instance starts |
| An action panics (server) | The action is refused with the panic's message |
The error stays until a build succeeds. Pages opened while it's showing see it too.
On a server in production, a panicking page only says that something went wrong. The details are for development.
What it needs#
soda dev fetches what it needs the first time:
- dx (Dioxus's CLI), pinned at
0.8.0-alpha.3byscripts/dx.sh. It's downloaded into~/.cache/sodaand checked against the release's SHA-256. - Tailwind, pinned the same way by
scripts/tailwind.sh.
If dx can't start, or you pass --no-hot, soda dev builds with Cargo and reloads the pages after
each change. Local-first, that's under a second for a one-line change (0.8 s measured), and nothing
is lost. On a server, the program restarts, so what it keeps in memory starts over.
soda dev keeps a list of your Rust files in .soda/files. The folder ignores itself in git, like
SvelteKit's .svelte-kit.
Local-first patching runs the app in a module service worker, which is untested in Safari. Release builds don't use one, so production is unaffected.
How it works#
Subsecond, from Dioxus, patches a running Rust program. Calls made through its jump table go to the newest version of a function. dx builds the program once, keeping every symbol. On each save it recompiles only the changed crate, links the changed functions into a patch, and sends the program a new jump table.
Soda calls the parts you edit through that jump table: on a server, rendering, actions and
downloads; local-first, the view, the routes, downloads and the shell. A release build has no
Subsecond in it at all, since only soda dev turns on the hot feature that brings it in.
What soda dev adds on top of dx:
- New files. dx only watches files it already compiled. Soda's router is generated from
src/routesas the crate compiles, sosoda devwrites.soda/filesand the router reads it. Adding or removing a file changes that list, and dx patches the router. - Type changes. It spots a changed type and rebuilds instead of patching (above).
- A fast first patch (local-first). dx compiles patches position-independent.
soda devbuilds the app's crate the same way from the start, so the first patch reuses the compiler's incremental cache instead of throwing it away. - Smaller builds. It builds without debug info. Panics still say where they happened, and a whole build takes about half as long.
- Errors in the page. dx skips a file that doesn't parse, so
soda devparses it itself and runscargo checkfor the compiler's full message. - Styles. Tailwind keeps watching, and the browser swaps the stylesheet with no Rust involved.
On a server, dx runs the program (dx serve --server --hot-patch). soda dev gives it a free
port (SODA_ADDR), the built site (SODA_DIST) and the shell to read from disk (SODA_SHELL).
The program connects to dx, loads each patch as a native library and re-renders every open page.
In front, soda dev proxies everything to the program, and waits while it builds or restarts.
Local-first, the app runs in a service worker, which Subsecond doesn't expect. In development
the worker is a module (web/soda-dev-worker.js ahead of sw.js) that loads dx's glue and applies
each patch to the running WebAssembly module. Browsers stop idle workers, and dx doesn't resend
patches to one that starts later. So soda dev keeps the latest patch, and a new worker applies
it before answering anything. One worker serves every open tab, so one patch reaches them all.
soda build renders everything from the code as it is, so there's nothing to patch there. The
SDK's npm run test:dev (local-first) and npm run test:dev-server (server) test most of these
cases end to end. Not covered: a static site, a web/ file on a server, and an action that panics.
This page is docs/hot-patching.md in the SDK, built into this site as it is.