From 9b4f7a1ad0ce6dcf6de91bb628a2dbb6e9007972 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ond=C5=99ej=20Tu=C4=8Dek?= <70013396+Asqit@users.noreply.github.com> Date: Tue, 8 Aug 2023 14:30:37 +0200 Subject: [PATCH] www: rewriting "data fetching" code example (#1603) Co-authored-by: Asher Gomez --- docs/latest/getting-started/fetching-data.md | 53 +++++++++----------- 1 file changed, 23 insertions(+), 30 deletions(-) diff --git a/docs/latest/getting-started/fetching-data.md b/docs/latest/getting-started/fetching-data.md index 7716e6c7..29803c88 100644 --- a/docs/latest/getting-started/fetching-data.md +++ b/docs/latest/getting-started/fetching-data.md @@ -9,13 +9,10 @@ during rendering. In real projects, this is often different. In many cases you may need to read a file from disk (e.g. markdown for a blog post), or fetch some user data from an API or database. -These operations are all asynchronous. Rendering however, is always synchronous. -Instead of fetching data directly during rendering, it should be loaded in a -route's `handler` function and then passed to the page component via first -argument to `ctx.render()`. - -The data that is passed to `ctx.render()` can then be accessed via the -`props.data` field on the page component. +In order to fetch data, the route component must be asynchronous. The first +parameter contains the client's +[`Request`](https://developer.mozilla.org/en-US/docs/Web/API/Request) object. +The second `ctx` parameter is used to get the route parameters. Here is an example of a route that fetches user data from the GitHub API and renders it in a page component. @@ -23,42 +20,38 @@ renders it in a page component. ```tsx // routes/github/[username].tsx -import { Handlers, PageProps } from "$fresh/server.ts"; +import { RouteContext } from "$fresh/server.ts"; -interface User { +interface GitHubResponse { login: string; name: string; avatar_url: string; } -export const handler: Handlers = { - async GET(_, ctx) { - const { username } = ctx.params; - const resp = await fetch(`https://api.github.com/users/${username}`); - if (resp.status === 404) { - return ctx.render(null); - } - const user: User = await resp.json(); - return ctx.render(user); - }, -}; +export default async function Page(_req: Request, ctx: RouteContext) { + const resp = await fetch( + `https://api.github.com/users/${ctx.params.username}`, + ); -export default function Page({ data }: PageProps) { - if (!data) { - return

User not found

; + if (!resp.ok) { + return

An Error occurred

; } + const { login, name, avatar_url } = await resp.json() as GitHubResponse; + return (
- -

{data.name}

-

{data.login}

+ +

{name}

+

{login}

); } ``` -The data is first fetched inside of the handler by making an API call to GitHub. -If the API call succeeds, the data is passed to the page component. If the API -call fails, the page component is rendered with `null` as the data. The page -component grabs the data from the props and renders it. +The data is first fetched inside our page component. We check that the response +is returned successfully, by checking whether the +[`ok`](https://developer.mozilla.org/en-US/docs/Web/API/Response/ok) property is +true. If the API call was successful, we will see our div with the user's GitHub +image, name, and username. Otherwise, we should see a heading saying: "An Error +occurred."