www: make islands.md examples run without errors (#1614)
Co-authored-by: Marvin Hagemeister <marvinhagemeister50@gmail.com>
This commit is contained in:
co-authored by
Marvin Hagemeister
parent
4a921d91b2
commit
7679e278cc
+101
-40
@@ -12,7 +12,7 @@ project. The name of this file must be a PascalCase or kebab-case name of the
|
|||||||
island.
|
island.
|
||||||
|
|
||||||
```tsx
|
```tsx
|
||||||
// islands/MyIsland.tsx
|
// islands/my-island.tsx
|
||||||
|
|
||||||
import { useSignal } from "@preact/signals";
|
import { useSignal } from "@preact/signals";
|
||||||
|
|
||||||
@@ -31,6 +31,60 @@ export default function MyIsland() {
|
|||||||
An island can be used in a page like a regular Preact component. Fresh will take
|
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.
|
care of automatically re-hydrating the island on the client.
|
||||||
|
|
||||||
|
```tsx
|
||||||
|
// route/index.tsx
|
||||||
|
import MyIsland from "../islands/my-island.tsx";
|
||||||
|
|
||||||
|
export default function Home() {
|
||||||
|
return <MyIsland />;
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
## Passing JSX to islands
|
||||||
|
|
||||||
|
Islands support passing JSX elements via the `children` property.
|
||||||
|
|
||||||
|
```tsx
|
||||||
|
// islands/my-island.tsx
|
||||||
|
|
||||||
|
import { useSignal } from "@preact/signals";
|
||||||
|
import { ComponentChildren } from "preact";
|
||||||
|
|
||||||
|
interface Props {
|
||||||
|
children: ComponentChildren;
|
||||||
|
}
|
||||||
|
|
||||||
|
export default function MyIsland({ children }: Props) {
|
||||||
|
const count = useSignal(0);
|
||||||
|
|
||||||
|
return (
|
||||||
|
<div>
|
||||||
|
Counter is at {count}.{" "}
|
||||||
|
<button onClick={() => (count.value += 1)}>+</button>
|
||||||
|
{children}
|
||||||
|
</div>
|
||||||
|
);
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
This allows you to pass static content rendered by the server to an island in
|
||||||
|
the browser.
|
||||||
|
|
||||||
|
```tsx
|
||||||
|
// routes/index.tsx
|
||||||
|
import MyIsland from "../islands/my-island.tsx";
|
||||||
|
|
||||||
|
export default function Home() {
|
||||||
|
return (
|
||||||
|
<MyIsland>
|
||||||
|
<p>This text is rendered on the server</p>
|
||||||
|
</MyIsland>
|
||||||
|
);
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
## Passing other props to islands
|
||||||
|
|
||||||
Passing props to islands is supported, but only if the props are serializable.
|
Passing props to islands is supported, but only if the props are serializable.
|
||||||
Fresh can serialize the following types of values:
|
Fresh can serialize the following types of values:
|
||||||
|
|
||||||
@@ -48,47 +102,56 @@ times, it is only serialized once and the references are restored upon
|
|||||||
deserialization. Passing complex objects like `Date`, custom classes, or
|
deserialization. Passing complex objects like `Date`, custom classes, or
|
||||||
functions is not supported.
|
functions is not supported.
|
||||||
|
|
||||||
## Passing JSX to islands
|
During server side rendering, Fresh annotates the HTML with special comments
|
||||||
|
that indicate where each island will go. This gives the code sent to the client
|
||||||
|
enough information to put the islands where they are supposed to go without
|
||||||
|
requiring hydration for the static children of interactive islands. No
|
||||||
|
Javascript is sent to the client when no interactivity is needed.
|
||||||
|
|
||||||
Islands support passing JSX elements via the `children` property. This allows
|
```HTML
|
||||||
you to pass static content rendered by the server to an island in the browser.
|
<!--frsh-myisland_default:default:0-->
|
||||||
|
<div>
|
||||||
```jsx
|
Counter is at 0.
|
||||||
// route/index.tsx
|
<button>+</button>
|
||||||
import MyIsland from "../islands/my-island.tsx";
|
<!--frsh-slot-myisland_default:children-->
|
||||||
|
<p>This text is rendered on the server</p>
|
||||||
export default function Home() {
|
<!--/frsh-slot-myisland_default:children-->
|
||||||
return (
|
</div>
|
||||||
<MyIsland>
|
<!--/frsh-myisland_default:default:0-->
|
||||||
<p>This text is rendered on the server</p>
|
|
||||||
</MyIsland>
|
|
||||||
);
|
|
||||||
}
|
|
||||||
```
|
```
|
||||||
|
|
||||||
We can deduce which parts were rendered by the server and which parts where
|
|
||||||
rendered by an island from the HTML alone. It contains all the information we
|
|
||||||
need, which allows us to skip the work of having to send a serialized version of
|
|
||||||
`props.children` to the browser.
|
|
||||||
|
|
||||||
### Nesting islands
|
### Nesting islands
|
||||||
|
|
||||||
Islands can be nested within other islands as well. In that scenario they act
|
Islands can be nested within other islands as well. In that scenario they act
|
||||||
like a normal Preact component, but still receive the serialized props if any
|
like a normal Preact component, but still receive the serialized props if any
|
||||||
were present.
|
were present.
|
||||||
|
|
||||||
```jsx
|
```tsx
|
||||||
// route/index.tsx
|
// islands/other-island.tsx
|
||||||
import MyIsland from "../islands/my-island.tsx";
|
import { useSignal } from "@preact/signals";
|
||||||
import OtherIsland from "../islands/other-island.tsx";
|
import { ComponentChildren } from "preact";
|
||||||
|
|
||||||
|
interface Props {
|
||||||
|
children: ComponentChildren;
|
||||||
|
foo: string;
|
||||||
|
}
|
||||||
|
|
||||||
|
function randomNumber() {
|
||||||
|
return Math.floor(Math.random() * 100);
|
||||||
|
}
|
||||||
|
|
||||||
|
export default function MyIsland({ children, foo }: Props) {
|
||||||
|
const number = useSignal(randomNumber());
|
||||||
|
|
||||||
export default function Home() {
|
|
||||||
return (
|
return (
|
||||||
<MyIsland>
|
<div>
|
||||||
<OtherIsland foo="this prop will be serialized">
|
<p>String from props: {foo}</p>
|
||||||
<p>This text is rendered on the server</p>
|
<p>
|
||||||
</OtherIsland>
|
<button onClick={() => number.value = randomNumber()}>Random</button>
|
||||||
</MyIsland>
|
{" "}
|
||||||
|
number is: {number}.
|
||||||
|
</p>
|
||||||
|
</div>
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
@@ -97,21 +160,19 @@ In essence, Fresh allows you to mix static and interactive parts in your app in
|
|||||||
a way that's most optimal for your use app. We'll keep sending only the
|
a way that's most optimal for your use app. We'll keep sending only the
|
||||||
JavaScript that is needed for the islands to the browser.
|
JavaScript that is needed for the islands to the browser.
|
||||||
|
|
||||||
```jsx
|
```tsx
|
||||||
// route/index.tsx
|
// route/index.tsx
|
||||||
import MyIsland from "../islands/my-island.tsx";
|
import MyIsland from "../islands/my-island.tsx";
|
||||||
import OtherIsland from "../islands/other-island.tsx";
|
import OtherIsland from "../islands/other-island.tsx";
|
||||||
|
|
||||||
export default function Home() {
|
export default function Home() {
|
||||||
return (
|
return (
|
||||||
<MyIsland>
|
<div>
|
||||||
<div>
|
<MyIsland>
|
||||||
<h1>Rendered by server</h1>
|
<OtherIsland foo="this prop will be serialized" />
|
||||||
<OtherIsland>
|
</MyIsland>
|
||||||
<p>also rendered by server</p>
|
<p>Some more server rendered text</p>
|
||||||
</OtherIsland>
|
</div>
|
||||||
</div>
|
|
||||||
</MyIsland>
|
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|||||||
Reference in New Issue
Block a user