Rendering and Hydration

Rendering and Hydration

How updates reach the DOM, and how server markup is adopted.

Updated: 2026-07-26

The reconciler

Updates diff against the DOM that is already on the page rather than replacing it. Element identity survives a re-render, and with it focus, text selection, scroll offsets inside nested containers, CSS transitions, and the value of an uncontrolled input.

Event handlers are attached once per element and event type, with the current handler held in a side table, so an inline arrow function does not cause listener churn.

Keys

key drives both child reconciliation and hook identity. A reordered list item moves its DOM node instead of being rebuilt, and keeps its own useState. Without a key, children reconcile by position.

tsx
{items.map((item) => (
  <Row key={item.id} item={item} />
))}

Hydration modes

full hydrates the whole page and suits interactive apps. islands hydrates only the components you mark, leaving the rest as static HTML. none ships no client JavaScript at all.

src/app.tsx
defineSite({
  pages,
  layout: Layout,
  hydrateMode: 'islands',
});

Islands

Mark a component with defineIsland and it is collected during the server render along with its props. On the client only those components hydrate, adopting their server markup; everything around them is untouched.

tsx
import { defineIsland } from '@rigbyhost/karui/ssr';
import { useState } from '@rigbyhost/karui/jsx';

export const Counter = defineIsland(() => {
  const [n, setN] = useState(0);
  return <button onClick={() => setN(n + 1)}>{n}</button>;
});

Rendering by hand

The JSX package exposes the primitives directly if you are not using the site conventions.

  • mount(root, node) โ€” render into root; later calls diff
  • hydrate(root, node) โ€” adopt server markup, then behave like mount
  • unmount(root) โ€” run effect cleanups, release refs, empty root
  • renderToString(node) โ€” HTML for SSR
  • renderToDom(node, doc) โ€” detached DOM nodes

Chunked responses

streamToNodeResponse writes the response in chunks. Note that the page is rendered in full before the first chunk is sent, so this is not progressive streaming and does not improve time to first byte. Real progressive flush is not implemented yet.