# Rendering and hydration

A page is rendered twice: once on the JVM at build time, which writes its HTML, and once in the browser, which **adopts** the nodes of that HTML instead of
building them again. Both run the same components, so they agree on what the page holds.

## At build time

Every value is computed once with the state it reads at that moment, and nothing subscribes to anything: no effect, `mount` or listener runs. Concurrent
renders are independent.

- Text escapes `&` and `<`, attribute values, always double-quoted, escape `&` and `"`.
- Void elements (`area`, `base`, `br`, `col`, `embed`, `hr`, `img`, `input`, `link`, `meta`, `source`, `track`, `wbr`) are written `<br>`, without slash or
  end tag.
- `script` and `style` text is not escaped, so it can't contain its own end tag (`</script`, `</style`, any case): rendering fails.
- An attribute name, which a spread map can take from data, can't be empty or hold whitespace, a control character, `"`, `'`, `<`, `>`, `/` or `=`:
  rendering fails, since the name would start more markup.
- There is no whitespace between nodes, and the only markers are the comments `<!--[-->` and `<!--]-->` around the content of an `@await`, and those of
  [islands](/docs/sites/islands).

The server writes the HTML a person would write, and the client matches it by walking the tree: markers on every node would add bytes to every page.

## In the browser

The client walks the existing DOM in document order, and adopts each node that has the place and the kind the component builds there:

- An element is adopted when the next node is an element with the same tag. Otherwise it is created.
- A text is adopted when the next node is a text. The parser merges adjacent texts (`Count: ` and `0` are one node in the HTML), so a longer node is split,
  and a text whose content differs is rewritten.
- The parts that never change are adopted whole and never walked.
- The nodes nothing claimed are removed, and those that a `mount` block adds stay.

A branch, a list and the content of an `@await` are delimited by empty text nodes, which the browser adds while hydrating.

### When the two pages differ

Structure is always repaired, and texts and attributes that follow state are rewritten. Two things are **not** repaired: attributes present only in the server
HTML, and the content of a static chunk when another chunk with the same tag sits at the same position. Both mean that the server and the client rendered
different pages, like a page that reads the clock or the storage of the browser while it builds.

### When something throws

A text, an attribute or a block that throws while the page is hydrated doesn't stop it: the text is empty, the attribute is the one the server wrote, the
block holds what it built so far, each at its place, and the rest of the page is hydrated, listeners included. The first failure is thrown once the page is
up, and what failed runs again when the state it read changes. Code that throws outside of those, like the script of a component, stops the hydration
where it is. An [error boundary](/docs/components/async#error-boundaries) shows a fallback in place of what fails.

## HTML that parses back to the same tree

Hydration relies on the browser parsing the server HTML into the tree that was rendered, so the compiler keeps to markup where that holds:

- A `<table>` holds its rows through a `<tbody>`, the parser inserts one otherwise.
- A `<p>` is closed by a following block element, nested `<a>` and `<form>` elements are split by the parser.
- The children of `<noscript>` and `<template>` aren't part of the DOM tree, so they hold no dynamic content.
- The parser moves HTML elements like `<div>` or `<p>` out of `<svg>` and `<math>`, except in the elements that hold HTML.

[Compile errors and warnings](/docs/reference/errors#html-structure) lists what the compiler rejects.

## Lists

An item of a list has no node of its own: it is the range from its first to its last node, which stay the same since what can change inside it sits between
anchors. Code that replaces the first or last element of an item, like a library that swaps an element for an icon, breaks the list.
