# How I Imagined Lithe, a Svelte-Like Web Framework for Kotlin

> Why I built [Lithe](https://lithe.ayfri.com), single-file components mixing HTML, Kotlin and scoped CSS, and how a framework that started on the Compose runtime ended up with its own.

Published 2026-12-04, updated 2026-12-04. Canonical: https://ayfri.com/articles/imagining-lithe/

![How I Imagined Lithe, a Svelte-Like Web Framework for Kotlin](https://ayfri.com/images/articles/imagining-lithe.webp)

I have been writing websites in [Kotlin](https://kotlinlang.org/) for years, [this one included](https://ayfri.com/articles/building-this-portfolio/), and most of my other web projects are written with [Svelte](https://svelte.dev/). Every time I went back from a `.svelte` file to Compose HTML, I missed the same thing: writing the HTML as HTML, with the logic right next to it and the styles scoped to the component.

So I built it. [Lithe](https://lithe.ayfri.com) is a web frontend framework for Kotlin: you write `.lithe` files with HTML, Kotlin and scoped CSS in one place, your pages are rendered to HTML at build time, and Kotlin/JS only handles what changes.

![The Lithe website, with the headline "HTML first, Kotlin right next to it"](https://ayfri.com/images/projects/lithe.avif)

It started as a test to see if the idea would even work. Today it has a compiler, a runtime, a dev server, a language server, a VS Code extension, an IntelliJ plugin, a documentation website written with Lithe itself, and a 0.1.0 on Maven Central. This article is about how I imagined it, why it looks the way it does, and the one decision that changed everything early on.

## What a component looks like

If you know Svelte, you will feel at home:

```lithe
<script kotlin>
	prop val step: Int = 1

	var count by state(0)
	val doubled by derived { count * 2 }
</script>

<div class="demo">
	<button class="counter" type="button" @click={count += step}>{count} × 2 = {doubled}</button>
	@if (count > 10) {
		<p>That's a lot of clicks.</p>
	}
</div>

<style>
	.counter {
		padding: 0.6rem 1.2rem;
		border: 0;
		border-radius: 999px;
		cursor: pointer;
	}
</style>
```

- `<script kotlin>` holds the Kotlin of the component. `prop val` declares a prop, which becomes a parameter of the component function.
- `state` and `derived` are reactive values, used as Kotlin delegates.
- The markup is real HTML. `{expr}` inserts any Kotlin expression, `@click={...}` listens to an event, and `@if`, `@for`, `@when` or `@await` are blocks.
- `<style>` is scoped to the component, and it can read Kotlin values too.

A tag starting with an uppercase letter is another component, a lowercase one is an HTML element. The file name is the component name, and its folder is its package, like a Kotlin file.

## Why not just Compose HTML

My portfolio runs on [Kobweb](https://kobweb.varabyte.com/), which is built on Compose HTML. I like Kobweb a lot, but two things kept bothering me.

**The weight.** Compose HTML brings the Compose runtime, its HTML library and kotlinx.coroutines into the browser. A small to-do page written with Compose HTML ships **137.5 KB** of JavaScript once compressed with Brotli. The same page in Svelte is 20.7 KB.

**The way it renders.** Compose HTML renders into an empty element, in the browser only. It can't render a page on the JVM, and it can't adopt HTML that is already there. Kobweb works around it for its static export by running the whole site in a headless browser, then the client composes the full page again on load.

And then there is the syntax. Writing `Div({ classes("card") }) { Text(...) }` all day is fine, but it is not HTML, and copying a snippet from MDN or a designer means translating it by hand.

I wanted the opposite: HTML first, complete before any JavaScript runs, and Kotlin right next to it.

## Design before code

Before writing any runtime, I wrote the design down: the browser target, the SEO goals, and the full syntax of a component, almost 500 lines of it. The syntax kept moving after that, and it went through many rounds before it looked like the snippet above.

A few rules came out of that night and never moved:

- **Every page is complete HTML before any JavaScript runs.** Crawlers and users get the full content on the first response.
- **A part of the page that can't change ships no JavaScript.** And the compiler decides that by itself, there is no setting.
- **A file format of its own.** Components live in `.lithe` files, not `.kt` files, so the template syntax isn't limited by Kotlin's grammar, and you can read the markup apart from the Kotlin around it.
- **Kotlin/JS, not Wasm.** Kotlin/JS gives the smallest bundle and direct DOM access, where Wasm needs a bigger first download and goes through interop for every DOM call.
- **Modern browsers only.** Lithe targets [Baseline](https://web.dev/baseline) early 2025: ES modules, CSS nesting, `:has()`, container queries, no polyfill and no fallback.

## The first version ran on Compose

Here is the funny part. My first design was not a new runtime at all. The component block was called `<script compose>`, and the compiler turned each file into a `@Composable` function:

```kotlin
@Composable
fun Counter(start: Int = 0, step: Int = 1) {
	var count by state(start)
	val doubled = count * 2
	Element("button", { on("click") { count += step } }) { Text("$count × 2 = $doubled") }
}
```

The idea was to keep only the Compose **runtime**, the compiler plugin, recomposition and state, with my own appliers: an HTML writer on the JVM for the static pages, and a DOM applier in the browser that adopts the pre-rendered nodes instead of building them again. Compose HTML can't do that, so this was already a step up.

That version worked. Then I measured it.

## The pivot

On the to-do page, the client built on the Compose runtime weighed **119.7 KB** with Brotli, against **20.2 KB** for a version on a small runtime of signals, and it hydrated 3 to 4 times slower. Compose recomposes whole functions, and all of its machinery has to come with it into the browser. No amount of tuning would close a 6x gap.

So I replaced Compose with my own fine-grained runtime, in the style of Svelte 5 and [Solid](https://www.solidjs.com/).

The difference is in how a component runs. With Compose, a component function runs again every time a state it read changes, and Compose compares what it got with what it had. With Lithe, **a component runs once**. Each text, attribute or block that can change is a small lambda subscribed to the state it reads, and only that lambda runs again. No virtual DOM, nothing to diff, nothing to memoize.

This one decision reshaped the syntax:

- The block became `<script kotlin>`, since it wasn't Compose anymore.
- `val doubled = count * 2` stopped working. With Compose, the function ran again, so the `val` was recomputed. With a script that runs once, it keeps its first value forever. That's why derived values are written `val doubled by derived { count * 2 }`, and why the compiler warns when a plain `val` reads state.
- The generated code went from `if (count > 0) Element(...)` inside a recomposing function to explicit reactive nodes, `Text { count.toString() }` or `Choose({ if (count > 0) 0 else -1 }) { ... }`.

Lithe still works with Compose, it just doesn't run on it. The output is plain DOM, so a Lithe component and a Compose HTML composable can mount each other, and only the sites that use Compose ship it.

## One file, two outputs

A Lithe page has to render on the JVM at build time and run in the browser. The problem is that browser code uses browser types, `PointerEvent`, `window`, `HTMLCanvasElement`, and the typed DOM of [`kotlin-browser`](https://github.com/JetBrains/kotlin-wrappers) only exists for JS and Wasm.

So the compiler writes two Kotlin outputs per component:

- The **browser** output holds everything.
- The **JVM** output builds an HTML string and leaves out what never runs on a server: event handlers, `mount`, `effect`, element references, and every statement of the script that uses a browser API. The compiler finds them on its own, you never mark them.

The JVM output is fast: a page of 1,000 table rows, about 187,000 characters of HTML, renders in **1.28 ms**, without any browser.

In the browser, the same component adopts the nodes of that HTML. The server writes no marker on them, the client matches the tree by walking it.

## Nothing ships for what can't change

This is the rule I care about most. The compiler reads what each component asks of the browser: an event, a binding, `mount`, `effect`, `@await`, a browser API. A component that asks for none is rendered at build time and never reaches the client.

![What a static page, a page with islands and a page that hydrates whole ship](https://ayfri.com/images/lithe/what-ships.svg)

- **A static page** ships no JavaScript at all. The browser code isn't even compiled, which also skips most of the build time.
- **A page where only some components are interactive** ships those, as islands.
- **A page that needs the browser at its root** hydrates whole: **17.0 KB** of JavaScript with Brotli for the to-do page.

## Making it fast

Once the runtime was mine, I could tune it for what a web page actually does. A few tricks I'm proud of:

**Linked lists instead of arrays.** Each signal links to its readers through linked lists, the way [alien-signals](https://github.com/stackblitz/alien-signals) does, so a run that reads what the last one read allocates nothing. On a table of 10,000 rows, selecting a row went from **179 ms** to **20 ms** with that change alone.

**The selector.** Selecting a row in a list is the classic benchmark. In a keyed `@for`, a comparison like `selected == row.id` runs once per row in Svelte and Solid. Lithe's compiler spots it and writes it as a selector declared once before the loop, so a new selection only updates **two rows**, the old one and the new one:

```lithe
@for (row in rows, key = row.id) {
	<tr class={"danger" on (selected == row.id)} @click={selected = row.id}>
		<td>{row.label}</td>
	</tr>
}
```

**Moving the fewest rows.** A reordered list keeps the rows that didn't move and only moves the ones outside the longest run still in order, with [`moveBefore`](https://developer.mozilla.org/docs/Web/API/Element/moveBefore) where the browser has it. Reversing 1,000 rows takes 2.4 ms, against 31.9 ms for Svelte.

**A client that only carries what it uses.** The link drops `@for`, `@await` and the rest when nothing uses them, and the runtime avoids the parts of the Kotlin standard library that are huge for what they do, like `Regex` and function references, which bring reflection. The link targets ES2020, where a `Long` is a native `bigint`, so the classes emulating 64-bit integers are gone too.

On [Svelte's own reactivity benchmarks](https://lithe.ayfri.com/docs/reference/optimizations), written with each library's public API, Lithe has the lowest time on all ten graphs, at least 1.2 times ahead of the faster of Svelte and Solid.

![Reactivity graphs of Lithe, Svelte and Solid](https://ayfri.com/images/lithe/reactivity.svg)

And here is the to-do page against Svelte, Solid and Compose, with the CPU slowed 4 times:

![Load and click times of the todo page on a CPU slowed 4 times](https://ayfri.com/images/lithe/todo-load.svg)

| To-do page                   | JavaScript, Brotli | Hydrate  | Click   |
|------------------------------|-------------------:|---------:|--------:|
| Lithe                        |            17.0 KB |  42.9 ms |  8.8 ms |
| Svelte                       |            20.7 KB |  43.7 ms | 10.7 ms |
| Solid                        |            20.5 KB |  44.5 ms |  9.5 ms |
| Compose HTML                 |           137.5 KB | 220.5 ms | 61.1 ms |
| Compose runtime, a reference |           119.7 KB | 176.1 ms | 56.1 ms |

Lithe, Svelte and Solid are close: Lithe's client is the smallest, and the hydration times overlap. Compose HTML is about 8 times larger and 5 times slower. To be fair to Compose, it composes the page in the browser where the others hydrate pre-rendered HTML, so it is not exactly the same job. These figures are medians from my machine, in headless Chrome, measured on December 3 and 4, 2026, and they move by 10 to 20% between sessions. The [performance page](https://lithe.ayfri.com/docs/reference/performance) has the method and every number.

## The tooling, because a syntax without an editor is useless

A custom file format means no editor understands it. So the next big piece was a language server: diagnostics, completion, hover, go to definition, renames, inlay hints. It answers errors after a change in about 42 ms and completion in 2.7 ms. On top of it, there is a VS Code extension and an IntelliJ IDEA plugin, both on their marketplaces.

![A Lithe component open in IntelliJ IDEA with the Lithe plugin](https://ayfri.com/images/lithe/intellij-overview.webp)

Then the dev server, since Compose Hot Reload only works on the JVM desktop target. It keeps a warm, incremental Kotlin/JS compiler, and it avoids compiling when it can: in a development build, the generated Kotlin reads the texts and static attributes of the markup from tables, so editing them only runs the Lithe compiler. Saving a text change reaches the browser in **170 ms**, against **2.46 s** for an edit that needs Kotlin to compile. A change in the `<style>` block swaps the stylesheet without even reloading the page.

## Kobweb, Compose and Ktor

I didn't want Lithe to be an island of its own (pun intended). It plugs into what Kotlin web developers already use:

- **Kobweb.** A `.lithe` file under `pages` is a page, and a component is a composable that Markdown pages can render. The Lithe website itself is a Kobweb site written with Lithe.
- **Compose HTML.** Components and composables mount each other.
- **Ktor.** `call.respondLithe { ... }` renders a page on each request, and the browser hydrates its islands only. The Ktor module is in the repository and comes with the release after 0.1.0.
- **Gradle.** A plugin compiles `.lithe` files in a Compose project. Its server mode, for a Ktor server and its client, comes with that same release.

## Questions I got

When I shared it, the same questions came back, so here are the answers.

**Does it compile Kotlin back to JS?** Kind of. A `.lithe` file compiles to plain Kotlin, twice: once for the JVM, to render the static pages, and once for the browser, which Kotlin/JS compiles to JavaScript. The generated code is short and readable, not a black box.

**How does it avoid Compose-level sizes?** It doesn't use Compose at all, so none of its runtime ships. Only the parts of the page that change are hydrated, and the link drops every feature of the runtime the page doesn't call. I ended up reimplementing a few high-level ideas of Compose, like states, derived values and effects, on a much smaller core.

**Is it Kotlin Multiplatform?** It targets the JVM, for rendering, and JS, for the browser. The same component code runs on both. There is no Wasm build, for the reasons above.

## The limits

It is experimental, version 0.1.0, and I'd rather say what it doesn't do well:

- **Creating many rows is slower than Svelte.** 9.7 ms for 1,000 rows against 6.7 ms, and hydrating a table of 1,000 rows takes 18.0 ms against 10.1 ms.
- **Islands don't nest**, and a function or markup can't be passed to one as a prop.
- **Props reach an island as JSON.** Strings, numbers and booleans cost nothing, any other type brings kotlinx.serialization, about 13 KB more with Brotli.
- **A reload loses the state** of the page, unless the change was only in a stylesheet.
- **The syntax isn't final.** It already changed a lot: `@head` blocks became real `<head>` elements, `@this` became `@mount`, special window tags became listener functions. While Lithe is in 0.x, it can still move.
- **There's no online playground.** It would need a server running the compiler for every visitor, and my small server would not enjoy that.

## What's next

I'm very happy with the result. It started as "let's see if this could work", and it works much better than I expected. Now I want feedback from people who aren't me: on the syntax, on the docs, on what breaks in a real project.

If you want to try it:

- The website and docs: [lithe.ayfri.com](https://lithe.ayfri.com), with [How it works](https://lithe.ayfri.com/docs/how-it-works) and the [Optimizations](https://lithe.ayfri.com/docs/reference/optimizations) page, which explains every choice with its cost.
- The code: [github.com/Ayfri/lithe](https://github.com/Ayfri/lithe), open source under Apache 2.0, with a to-do example, a full task app, and one small example per feature.

And if you build something with it, tell me, I'll be its second user.
