diff --git a/docs/data-fetching.md b/docs/data-fetching.md new file mode 100644 index 00000000..f2aa11d4 --- /dev/null +++ b/docs/data-fetching.md @@ -0,0 +1,59 @@ +# Data fetching + +Not all pages can be server rendered synchronously - some pages are dynamic and +require fetching 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. + +## `useData` hook + +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. + +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. + +Here is a usage example of a `useData` hook where the fetcher fetches the URL +provided as the key as JSON: + +```tsx +import { h, PageProps, useData } from "../deps.ts"; + +export default function Page(_props: PageProps) { + const info = useData("https://cdn.deno.land/std/meta/versions.json", fetcher); + + if (info === null) { + return
+ The Deno standard library has {versions.length} versions, with {latest} + {" "} + being the most recent. +
+ ); +} + +interface ModuleInfo { + latest: string; + versions: string[]; +} + +async function fetcher(url: string): Promise