bitstillery
← Back to the blog

Mithril, Bitstillery edition

Our frontends, Discover and Portal, run on Mithril, the small and fast virtual DOM library that Leo Horie started in 2014. We like it because there isn’t much of it: a component is an object with a view, the router fits in your head, and there’s no build magic between you and the browser. But there were things we kept missing. Components only redraw after an event handler, a request, a route change or an explicit m.redraw(). To keep the screen in step with the data, we had glued our own store onto Mithril from the outside: a proxy that called m.redraw() whenever the state changed and kept part of it in localStorage. It did the job, but the seams were starting to show. There was also no server-side rendering that hands its state over to the browser, and the types lived in a separate @types package, apart from the code.

The usual answer to that is a migration: months of rewriting everything into React, Vue or Solid, and we were heading that way. With AI, something else suddenly became doable for a team of three: instead of moving our code to another framework, we could just bring the framework up to date. So early this year we forked Mithril, moved it to Bun and TypeScript, and started building what we missed into Mithril itself, with AI writing much of the code. Some 320 commits later, our edition does what we need, and we don’t have to migrate anymore.

The biggest change is signals, which replace that proxy store. A component now redraws by itself when a signal it has read changes, and state() turns a plain object into reactive state, nested objects and arrays included, with its functions becoming computed values. A small example:

import m, {state, MithrilComponent} from '@bitstillery/mithril'

const $s = state({
    count: 0,
    total: () => $s.count * 2,
})

class Counter extends MithrilComponent {
    view() {
        return (
            <div>
                <p>
                    Count: {$s.count}, Total: {$s.total}
                </p>
                <button onclick={() => $s.count++}>Increment</button>
            </div>
        )
    }
}

m.mount(document.getElementById('app'), Counter)

There’s no m.redraw() and no .value in the template. Change $s.count from anywhere and the counter follows.

Because the library now knows about state, rendering on the server got a lot easier too. A component can load its data in an async oninit; on the server, the renderer waits for it and sends both the HTML and the state to the browser, which picks it up from there without fetching everything again. The store, the successor of our old localStorage proxy, builds on the same idea and lets you decide per value where it lives: in localStorage, in sessionStorage, in the server session, or in a cookie, so the server can already render a page with, say, the right language in place. And all of it is typed: the package ships its TypeScript source, there’s no build step, and the library has no any.

That this works is mostly down to the tests. 769 of them check the exact output: the HTML and state that server rendering produces, the router, the URL helpers. With those in place, an agent can change the inside and see straight away what broke. Since October, an agent writes most of the changes as pull requests, which we read before they’re merged. That also made room for a proper cleanup: dead code and leftovers from upstream went out, and in October alone some 15,500 lines were removed and 14,500 added. A benchmark suite for rendering, signals, state, the store, the router and server rendering keeps an eye on speed while that happens.

We let a few things go along the way. m.request and streams are gone, because fetch() does that job well these days; other than that, Mithril v2 apps run unchanged. And next to it a component library has grown: fields, dialogs, tabs, tables and a page layout, each taking its state as a single signal, so an app opens a dialog or switches a tab by writing a value. Where the browser already does something well, like the phone’s own picker for a select or the native dialog, the library simply uses that.

Want to try it? It’s MIT licensed and on npm: bun add @bitstillery/mithril. The source, the guides and the benchmarks are on GitHub. And thanks to Leo Horie for creating Mithril, and to everyone who has worked on it since.

Let's talk spirits & software

Curious what we do, or what we could do for you? We'd like to hear from you.