- Docs
- Sites
Static pages and islands
The build decides what the browser runs, based on what each component asks of it. There's no setting, you always get the smallest output.
What needs the browser
A component needs the browser when it:
- listens to an event, binds a value, takes a reference with
@refor@mount, or callsmountoreffect; - waits for a value with
@await, or keeps a statement that uses a browser API, a page value included; - reads a state that a
.ktfile declares at its top level or in anobject, or calls a top-level function of a.ktfile that does, directly or through others; - renders a name that isn't a
.lithecomponent of the build, a Kotlin function that can hold any state; - gives content, a function or attributes for a rest prop to a component that needs the browser, since those can't be written in the page as data.
Everything else only computes its markup from its props, so its output at build time says all it will ever show: a state nothing writes, a loop, an if, a call, a member or a string template are fine there.
What the page gets
What happens to the page depends on App:
App |
The build writes |
|---|---|
| Needs the browser, or renders a component that does and is one of those above | app.mjs, which hydrates the whole page. |
| Needs the browser nowhere below it | No app.mjs: no script, and the browser code isn't compiled, which skips the release link, most of the build time. The build log says when it happens. |
| Doesn't, but renders components that do | app.mjs with the code of those islands only: App and everything above them is never hydrated. |
An island is the outermost component of its tree that needs the browser. A component inside an island is part of it.
Props of an island
The page holds an island between two comments, with the props its parent gave, so every prop of an island crosses from the build to the browser as JSON:
<!--lithe:ui.Counter {"id":"a","start":2}-->...<!--/lithe-->
| Prop type | How it crosses | Cost |
|---|---|---|
A String, a number, a Boolean or a List of those |
Read as it is. | Nothing in the client. |
A class or an enum marked @Serializable, and lists, sets, maps and nullables of those and of the primitives |
kotlinx.serialization. | About 43 KB more in the client. |
| A generic class | serializer<T>(), which looks the serializer up when the page runs. |
About 75 KB more. |
| A function, markup with parameters or a rest prop | It can't cross. | The component that gives one needs the browser itself, and the island is that component then. |
When one of the last kind reaches the build anyway (a function in a variable, not a lambda), the render fails and names the prop.
The module that declares a @Serializable class enables the plugin, with $kotlin.serialization.json among its dependencies:
dependencies:
- $kotlin.serialization.json
settings:
kotlin:
serialization: enabled
The server writes the JSON with the library, and the client reads it with the serializers of the plugin and a decoder of Lithe, without the JSON of the library. That adds about 43 KB to the client of a page (78 KB measured on examples/features/islands, 35 KB with plain props only), so plain props are the cheap way.
Markup without parameters (@Markup () -> Unit, the children included) doesn't cross as JSON: it stays HTML of the page. The server writes it where the island renders it, or in a <template> after the island when it doesn't, and the browser takes it from there, so the code of the parent never reaches the client. It never changes, since the parent doesn't run in the browser. When the markup renders an island itself, the component that gives it needs the browser, since islands don't nest.
Sharing state
A state that an island changes isn't seen by a component rendered at build time, since that one never runs again. Two islands share state through a variable of a .kt file, which both read.
Limits
- A sealed or open class isn't read as a prop of an island, because it needs a class discriminator.
- A
Longabove 2^53 loses its low digits as a prop of an island, since the page parses the JSON into JavaScript numbers. - Telling that a component follows a state through a method isn't possible, because the compiler doesn't know the type of a value: a component reads a state of a
.ktfile by naming it or by calling a top-level function that reads it, directly or through others. A method of a class that reads one isn't seen, so a component that only calls it stays rendered at build time. - Giving an island a function or content with parameters from a component rendered at build time isn't possible, because they can't be written in the page. The component that gives them needs the browser itself.