Skip to content

Islands

An island is a small interactive component inside a page that is otherwise static. The rest of the page is sent as plain HTML and never hydrated. The island's own JavaScript loads separately, and only the island is hydrated. Islands let you keep a content-heavy page light while still having a counter, a search box, or a menu that works.

Creating an island

Put the directive "use island" on the first line of a component file under app/:

tsx
// app/components/AddToCart.tsx
"use island";

import { useState } from "react";

export default function AddToCart({ productId, price }: { productId: string; price: number }) {
  const [count, setCount] = useState(0);
  return (
    <button onClick={() => setCount(count + 1)}>
      Add to cart ({count}) · ${price}
    </button>
  );
}

Comments may come before the directive. Single or double quotes are allowed. The directive must be the first statement in the file.

Using it in a page

Import the island and render it like any component. Pass props that can be turned into JSON:

tsx
// app/pages/products/[id].server.tsx
import AddToCart from "../../components/AddToCart";

export async function loader({ params }: { params: Record<string, string> }) {
  return { id: params.id, price: 19 };
}

export default function Product({ data }: { data: { id: string; price: number } }) {
  return (
    <article>
      <h1>Product {data.id}</h1>
      <p>A long description that needs no JavaScript.</p>
      <AddToCart productId={data.id} price={data.price} />
    </article>
  );
}

The page is a .server page, so only the island hydrates. The description ships as HTML and no page JavaScript loads.

What happens at build time

Kiln treats the island file in two places:

  1. On the server, the component is wrapped. The wrapper renders a container with the island's id and the props serialized into a data-props attribute. The server records the island's id, so it knows the page needs the client script.
  2. In the browser, the client loads the island's code and hydrates the container with the same props.

The id is the file's path relative to the working directory, with forward slashes. You do not write the id yourself.

Props

Props must be JSON-serializable, because they are written into the page as text. This means:

  • Strings, numbers, booleans, null, arrays, and plain objects are fine.
  • Functions cannot be passed. Define handlers inside the island.
  • Date objects become strings after the round trip. Convert them yourself.
  • Children are not supported as props. Pass data instead.

Do not pass secrets as props. They are visible in the page source.

Islands on universal pages

A universal page that contains islands does not hydrate the whole page. Only the islands hydrate, and the rest of the page is static. Visitors see the same HTML as before, and only the islands respond to clicks. If you need interactivity across the whole page, use a universal page with no islands, or a .client page.

Rules

  • An island must be a file under the app folder. A file outside it with the directive is not an island.
  • An island file must have a default export. A file without one is not wrapped.
  • Islands cannot be .server files, because they need client code.
  • An island cannot be imported as a server-only module from a client file. The usual import rules apply.
  • Each island is hydrated on its own root. Islands do not share React state or context. Use the URL, the browser's storage, or a custom event to share state between islands.

Hydration of multiple islands

A page can contain many islands. Each one loads its own code and hydrates separately. Islands that never receive a click still load, so keep island count reasonable.

Errors

If an island's code fails to load, the browser logs island … failed to load and the server-rendered HTML stays on screen. If the server render of an island throws, the whole page request fails like any other render error.

If the server HTML and the client render differ, React reports a hydration mismatch in the console and the island re-renders. Keep island components deterministic on their first render. Use useIsClient for values that only exist in the browser.

Islands and the router

Islands are not routes. When you move between pages with Link, a universal target page is rendered by the browser, so its islands work as ordinary components. Moving to a .server page is a full page load, and that page's islands hydrate on arrival.