Files
sweet/docs/concepts/islands.md
T

1.9 KiB

description
description
Islands enable client side interactivity in Fresh. They are hydrated on the client in addition to being rendered on the server.

Islands enable client side interactivity in Fresh. Islands are isolated Preact components that are rendered on the client. This is different from all other components in Fresh, as they are usually just rendered on the server.

Islands are defined by creating a file in the islands/ folder in a Fresh project. The name of this file must be a PascalCase or kebab-case name of the island. The file must have a default export that is a regular Preact component.

// islands/MyIsland.tsx

import type { Signal } from "@preact/signals";

export default function MyIsland() {
  const count = useSignal(0);

  return (
    <div>
      Counter is at {count}. <button onClick={() => count.value += 1}>+</button>
    </div>
  );
}

An island can be used in a page like a regular Preact component. Fresh will take care of automatically re-hydrating the island on the client.

Passing props to islands is supported, but only if the props are serializable. Fresh can serialize the following types of values:

  • Primitive types string, boolean, and null
  • Most numbers (Infinity, -Infinity, and NaN are silently converted to null, and bigints are not supported)
  • Plain objects with string keys and serializable values
  • Arrays containing serializable values
  • Uint8Array
  • Preact Signals (if the inner value is serializable)

Circular references are supported. If an object or signal is referenced multiple times, it is only serialized once and the references are restored upon deserialization.

Passing complex objects like Date, custom classes, or functions is not supported. This means that it is not possible to pass children to an island, as children are VNodes, which are not serializable.

It is also not supported to nest islands within other islands.