feat: a new "island" based architechture (#97)
This commit is contained in:
+35
-32
@@ -4,35 +4,50 @@ Not all pages can be server rendered synchronously - some pages are dynamic and
|
||||
require fetching some data from an external source. This data fetching is often
|
||||
asynchronous.
|
||||
|
||||
Fresh supports asynchronous data fetch in server render using suspense. This
|
||||
allows render to be interupted to wait for a promise to resolve. Once the
|
||||
promise resolves, the render is retried.
|
||||
Fresh supports asynchronous data fetching in handlers. This is not done during
|
||||
rendering, but before rendering in the route
|
||||
handler<!-- TODO: link route handlers page -->.
|
||||
|
||||
## `useData` hook
|
||||
## Fetching data in a `handler`
|
||||
|
||||
To make using asynchronous data sources during server render super easy, fresh
|
||||
exposes the `useData` hook. This hook takes a key and a fetcher function that
|
||||
can return a promise. It is invoked once on the server. The server will wait
|
||||
until the promise resolves, at which point it will render the page with the new
|
||||
data.
|
||||
To fetch data, you must first define a custom `handler` function. This function
|
||||
is called when the route is requested. In there you can do some handling, and
|
||||
then return a `Response` object. If you want to render the page, you can call
|
||||
the `ctx.render` function. This returns a `Promise<Response>` that you can
|
||||
return from the handler.
|
||||
|
||||
The fetcher for the `useData` hook is never run on the client. Instead the data
|
||||
that was computed on the server during render will be used.
|
||||
The `ctx.render` function takes a single argument, which is some data that you
|
||||
want to pass to the page template. The data can be accessed in the template via
|
||||
the `props.data` property.
|
||||
|
||||
Here is a usage example of a `useData` hook where the fetcher fetches the URL
|
||||
provided as the key as JSON:
|
||||
Here is a usage example of a `handler` fetches a URL as JSON and provides the
|
||||
resulting data to the template:
|
||||
|
||||
```tsx
|
||||
import { h, PageProps, useData } from "../deps.ts";
|
||||
import { h, Handlers, PageProps, useData } from "../deps.ts";
|
||||
|
||||
export default function Page(_props: PageProps) {
|
||||
const info = useData("https://cdn.deno.land/std/meta/versions.json", fetcher);
|
||||
interface ModuleInfo {
|
||||
latest: string;
|
||||
versions: string[];
|
||||
}
|
||||
|
||||
if (info === null) {
|
||||
export const handler: Handlers<ModuleInfo> = {
|
||||
async GET(ctx) {
|
||||
const resp = await fetch("https://cdn.deno.land/std/meta/versions.json");
|
||||
let data = null;
|
||||
if (resp.status === 200) data = await resp.json();
|
||||
return ctx.render(data);
|
||||
},
|
||||
};
|
||||
|
||||
export default function Page(props: PageProps<ModuleInfo>) {
|
||||
const data = props.data;
|
||||
|
||||
if (data === null) {
|
||||
return <div>Data not available.</div>;
|
||||
}
|
||||
|
||||
const { latest, versions } = info;
|
||||
const { latest, versions } = data;
|
||||
return (
|
||||
<p>
|
||||
The Deno standard library has {versions.length} versions, with {latest}
|
||||
@@ -41,19 +56,7 @@ export default function Page(_props: PageProps) {
|
||||
</p>
|
||||
);
|
||||
}
|
||||
|
||||
interface ModuleInfo {
|
||||
latest: string;
|
||||
versions: string[];
|
||||
}
|
||||
|
||||
async function fetcher(url: string): Promise<ModuleInfo | null> {
|
||||
const resp = await fetch(url);
|
||||
if (resp.status === 200) return resp.json();
|
||||
return null;
|
||||
}
|
||||
```
|
||||
|
||||
The `useData` can not generate data client side. Calling `useData` on the client
|
||||
with a key that no data was generated for on the server will result in `useData`
|
||||
throwing an error.
|
||||
If you want to fetch data client side, inside of an island, you can use
|
||||
libraries like `"swr"`, or raw `useEffect` and `useState` as you usually would.
|
||||
|
||||
Reference in New Issue
Block a user