# How it works


1. The **compiler** reads `.lithe` files and writes plain Kotlin, one function per component, once for the JVM and once for the browser.
2. At build time, the components run on the **JVM** against an HTML writer, which writes the static pages.
3. In the browser, the same components run on **Kotlin/JS** against the DOM, and adopt the pre-rendered nodes (hydration) instead of building them again.

## A component runs once

A component is a function that runs **once** and builds its part of the page. Each part that can change (a text, an attribute, a block) is a small lambda,
and only that lambda runs again when the state it read changes. No virtual DOM to diff, nothing to memoize: [State and effects](/docs/components/state)
has the details.

## Two outputs per component

Event handlers, `mount`, `effect`, `@mount` and element references use browser types (`PointerEvent`, `window`, `HTMLCanvasElement`). The JVM has no
equivalent, because the typed DOM of `kotlin-browser` exists for JS only. So the compiler writes two outputs per component:

- The **browser** output holds everything.
- The **static rendering** output runs on the JVM and leaves out what never runs on the server: those parts, plus every statement of the script that uses
  a browser API. The compiler finds them on its own, no marker needed.

Props and callbacks between components stay in both. [Generated code](/docs/reference/generated-code) shows both outputs of a component.

## The browser runs only what can change

The compiler works out which parts of a page are interactive. A component that listens to nothing, mounts nothing and doesn't wait for a value is
rendered at build time only and never hydrated. There's no setting for it, you always get the smallest output:

- A page where nothing can change ships **no JavaScript**.
- A page where only some components can change hydrates those, as [islands](/docs/sites/islands).
- A page whose root needs the browser hydrates as a whole, see [Rendering and hydration](/docs/sites/hydration).

Every page is complete, semantic HTML before any JavaScript runs, so crawlers and users get the full content on the first response.

## Why it is built this way

[Optimizations](/docs/reference/optimizations) measures each of these choices and says what it asks of you.

- **Its own fine-grained runtime.** Reactive signals in the style of Svelte and Solid, where each dynamic part of the page updates on its own. On the `todo`
  example, a client on a runtime made with Compose is several times larger and hydrates about **4 times slower**, see [Performance](/docs/reference/performance).
  Compose Multiplatform UI draws on a canvas through Skia and Wasm, so its page has no real HTML and its download is several megabytes.
- **Still compatible with Compose.** The output is plain DOM, so a component and a Compose HTML composable [mount each other](/docs/sites/compose), and only
  the sites that use Compose ship it.
- **One component model, two platforms.** The runtime targets the JVM (an HTML writer) and JS (the DOM) behind one API. Compose HTML is JS only and renders
  into an empty root, so it can neither render on the JVM nor hydrate existing HTML.
- **Rendering on the JVM.** Pages are rendered at build time without a browser. Kobweb's own static export runs the app in a headless browser instead.
- **Kotlin/JS, not Wasm.** Kotlin/JS gives the smallest bundle and direct DOM access. Wasm needs a bigger first download and goes through interop for every
  DOM call.
- **A file format of its own.** Components live in `.lithe` files rather than `.kt` files, so the template syntax isn't limited by Kotlin's grammar, and the
  markup reads apart from the Kotlin around it.
- **Its own reload.** Compose Hot Reload only works on the JVM desktop target, so the [dev server](/docs/sites/dev-server) rebuilds a changed component and
  tells the browser. Edit only the texts and attribute values of the markup and nothing gets compiled.

## Browser target

Lithe targets browsers that support every web feature that reached [Baseline](https://web.dev/baseline) by **early 2025**. The runtime ships no polyfill and
no legacy fallback. The generated JS and CSS use modern syntax and APIs directly (ES modules, CSS nesting, `:has()`, container queries, `@layer`), and the
HTML uses modern loading primitives (`type="module"`, `modulepreload`, `fetchpriority`).

A feature that became Baseline later is only used when the page stays correct without it, like `moveBefore` for the rows of a list and the Navigation API
for links. Check their status on [webstatus.dev](https://webstatus.dev).
