# Dev server

The dev server serves your app while you edit it. When a file changes, it rebuilds the app and tells every open tab to reload, swap a stylesheet, or show
the build error. From the root of the repository:

```shell
./kotlin run -m devserver -- --module examples/todo
```

It serves two kinds of modules:

- A [static site](/docs/sites/static-sites), which has a `src/App.lithe`: the page is rendered at build time and hydrated, like a production build.
- A Kotlin app without it, like `examples/hello`, a `js/app` module that only holds Kotlin. The server replaces `{{kotlin.scripts}}` in its
  `resources/index.html` with the script of the module.

## Options

| Option             | Default                      | Effect                                                                                           |
|--------------------|------------------------------|--------------------------------------------------------------------------------------------------|
| `--module <dir>`   |                              | Module to build and serve. Its name is the last path segment (`todo` for `examples/todo`).       |
| `--root <dir>`     | the output of the build      | Directory to serve, repeatable. The first root that has the file wins. Required without `--module`. |
| `--watch <dir>`    | the module and its dependencies, or the roots | Extra directory to watch, repeatable. With `--module`, the default also covers every module listed under `dependencies:` of a `module.yaml` by relative path, so editing `runtime` rebuilds the app. |
| `--project <dir>`  | current directory            | Project directory holding the `kotlin` wrapper. Relative paths of the other options are resolved against it. |
| `--toolchain-cache <dir>` | the toolchain's default location | Shared cache of the toolchain, where the library klibs and the Compose compiler plugin are. |
| `--host <host>`    | `127.0.0.1`                  | Address to bind. `0.0.0.0` and `::` expose the server to the network and turn the `Host` check off. |
| `--port <port>`    | `8080`                       | Port from 0 to 65535. `0` picks a free one.                                                      |
| `--debounce <ms>`  | `50`                         | Quiet time from 0 to 60000 before a batch of changes is handled. Editors save through a temp file and a rename, so one save is several events. |

Without `--module`, the files are served as they are and only the notifications are sent.

## What a change does

| Change                      | In the browser                                                      |
|-----------------------------|---------------------------------------------------------------------|
| A build starts              | The tab shows its loading state, like during a reload, and a small indicator at the top shows the time the build takes. The page stays usable. |
| Only `.css` files changed   | The matching stylesheet is swapped without navigation, so the page keeps its state. |
| Anything else               | The page reloads.                                                   |
| The build failed            | The [error panel](#error-panel) appears. The page is not reloaded.  |

On a site, editing the `<style>` block of a component swaps `app.css` and keeps the state of the page, unless the edit also changes the page or the client,
as a selector that gives elements the scope class or a runtime value does. A stylesheet of `resources/` needs no build at all.

### Edits that compile nothing

A development build reads the strings of the markup at runtime: the texts, the comments, the values of static attributes and of the quoted props of
components, and the HTML of the parts that never change. Edit only those (a word, a class, a static element added inside static markup) and the Kotlin
stays as it was, so **nothing compiles**: the page just reloads with the new strings. An edit that touches Kotlin, or that adds or removes something the
client builds on its own, like an expression, a block or a component, compiles as usual. [Performance](/docs/reference/performance#dev-server) has the time
of each kind of edit.

### Routes

A path without extension that a browser asks as a page and that no file matches is a [route](/docs/sites/routing) of the app, so it gets the page of the
app.

## Error panel

The panel lists the errors then the warnings of the build log, each with its file, line and column, and folds the full log. Escape or a click outside closes
it. Each entry has:

- **IDEA**: opens the file through the [JetBrains Toolbox](https://www.jetbrains.com/toolbox-app/), in the project named like the `--project` directory.
- **VS Code**: opens the file at that line and column.
- **Copy**: copies `file:line:column: message`, and **Copy all** every entry.

A static render that doesn't end within **30 seconds** fails the build, and the panel shows where the render is stuck.

## Limits

- Keeping the page state across edits of Kotlin, markup or script isn't possible, because the page reloads.
- Building a module the toolchain has never built isn't possible, because the library klibs and the Compose compiler plugin come from the toolchain cache:
  build it once with `./kotlin build -m <module>` first.
- Applying every setting of a `module.yaml` isn't possible, because the server reads three things from it: a `kmp/` product, `compose: enabled` and the
  relative `dependencies:`. Compiler options, other compiler plugins and other settings are ignored.
- Seeing a change of the `dependencies:` or settings of a module isn't possible without restarting the server, which reads the module graph once.
- Network access isn't possible by default, because the server binds to `127.0.0.1` and rejects any `Host` other than the loopback names and the bind
  address. `--host 0.0.0.0` lifts both, and exposes the served files and the event stream to the network.
- More than a few tabs on one origin isn't possible, because each tab holds one of the 6 HTTP/1.1 connections a browser opens per origin.
- Watching directories on macOS isn't possible without polling, because the JDK watcher polls there: changes show up with a delay.
- Copying from the error panel isn't possible on a page served over the network with plain HTTP, because browsers only give the clipboard to secure pages.
- Old browsers aren't supported, because the client targets Baseline early 2025 and has no fallback.
