This commit adds support for an advanced (de)serializer that is used for page state (island props and plugin state). This new serializer supports maintaining references (thus supporting circular objects, and multi-referenced objects). This is a pre-requisite for allowing to pass signals, JSX, Uint8Arrays, Date, and many other "advanced" values.
1.8 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 { useState } from "preact/hooks";
export default function MyIsland() {
const [count, setCount] = useState(0);
return (
<div>
Counter is at {count}.{" "}
<button onClick={() => setCount(count + 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, andnull - Most
numbers (Infinity,-Infinity, andNaNare silently converted tonull, andbigints are not supported) - Plain objects with string keys and serializable values
- Arrays containing serializable values
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.