diff --git a/docs/concepts/data-fetching.md b/docs/concepts/data-fetching.md index 86e8c7f4..91ae8beb 100644 --- a/docs/concepts/data-fetching.md +++ b/docs/concepts/data-fetching.md @@ -1 +1,45 @@ -> TODO(lucacasonato): this page still needs to be written +--- +description: | + Data fetching in Fresh happens inside of route handler functions. These can pass route data to the page via page props. +--- + +Server side data fetching in Fresh is acomplished through asynchronous +[handler functions][route-handlers]. These handler functions can call a +`ctx.render()` function with the data to be rendered as an argument. This data +can then be retreived by the page component through the `data` property on the +`props`. + +Here is an example: + +```tsx +interface Project { + name: string; + stars: number; +} + +export const handler: Handlers = { + async GET(_req, ctx) { + const project = await db.projects.findOne({ id: ctx.params.id }); + if (!project) { + return new Response("Project not found", { status: 404 }); + } + return ctx.render(project); + }, +}; + +export default function ProjectPage(props: PageProps) { + return ( +
+

{props.data.name}

+

{props.data.stars} stars

+
+ ); +} +``` + +The type parameter on the `PageProps`, `Handlers`, `Handler`, and +`HandlerContext` can be used to enforce a TypeScript type to use for the render +data. Fresh enforces during type checking that the types in all of these fields +are compatible within a single page. + +[route-handlers]: ./route-handlers diff --git a/docs/concepts/error-pages.md b/docs/concepts/error-pages.md index 86e8c7f4..b33faee4 100644 --- a/docs/concepts/error-pages.md +++ b/docs/concepts/error-pages.md @@ -1 +1,37 @@ -> TODO(lucacasonato): this page still needs to be written +--- +description: | + Error pages can be used to customize the page that is shown when an error occurs in the application. +--- + +Fresh supports customizing the 404 Not Found, and the 500 Internal Server Error +pages. These are shown when a request is made but no matching route exists, and +when a middelware, route handler, or page component throws an error +respectively. + +The 404 page can be customized by creating a `_404.tsx` file in the `routes/` +folder. The file must have a default export that is a regular Preact component. +A props object of type `UnknownPageProps` is passed in as an argument. + +```tsx +/** @jsx h */ +import { h } from "preact"; +import { UnknownPageProps } from "$fresh/server.ts"; + +export default function NotFoundPage({ url }: UnknownPageProps) { + return

404 not found: {url.pathname}

; +} +``` + +The 500 page can be customized by creating a `_500.tsx` file in the `routes/` +folder. The file must have a default export that is a regular Preact component. +A props object of type `ErrorPageProps` is passed in as an argument. + +```tsx +/** @jsx h */ +import { h } from "preact"; +import { ErrorPageProps } from "$fresh/server.ts"; + +export default function Error500Page({ error }: ErrorPageProps) { + return

500 internal error: {(error as Error).message}

; +} +``` diff --git a/docs/concepts/islands.md b/docs/concepts/islands.md index 86e8c7f4..70decacb 100644 --- a/docs/concepts/islands.md +++ b/docs/concepts/islands.md @@ -1 +1,42 @@ -> TODO(lucacasonato): this page still needs to be written +--- +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 are rendered on the client. This is different from all other +components in Fresh, as they are ususally 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 name of the island. The file +must have a default export that is a regular preact component. + +```tsx +// islands/MyIsland.tsx + +/** @jsx h */ +import { h } from "preact"; +import { useState } from "preact/hooks"; + +export default function MyIsland() { + const [count, setCount] = useState(0); + + return ( +
+ Counter is at {count}.{" "} + +
+ ); +} +``` + +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 JSON +serializable. This means that you can only pass primitive types, plain objects, +and arrays. It is currently not possible to pass complex objects like `Date`, +custom classes, or functions. 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. diff --git a/docs/concepts/middleware.md b/docs/concepts/middleware.md index 8d1680e0..0aa3f23f 100644 --- a/docs/concepts/middleware.md +++ b/docs/concepts/middleware.md @@ -1,7 +1,6 @@ --- description: | - Add middleware routes to intercept requests or responses for analytics - purposes, access control, or anything else. + Add middleware routes to intercept requests or responses for analytics purposes, access control, or anything else. --- A middleware is defined in a `_middleware.ts` file. It will intercept the diff --git a/docs/concepts/route-handlers.md b/docs/concepts/route-handlers.md deleted file mode 100644 index 86e8c7f4..00000000 --- a/docs/concepts/route-handlers.md +++ /dev/null @@ -1 +0,0 @@ -> TODO(lucacasonato): this page still needs to be written diff --git a/docs/concepts/routes.md b/docs/concepts/routes.md index d6180caf..5eb6423a 100644 --- a/docs/concepts/routes.md +++ b/docs/concepts/routes.md @@ -1,3 +1,8 @@ +--- +description: | + Routes are the basic building block of fresh applications. They are used to define the behaviour the application when a given path is requested. +--- + At their core, routes describe how a request for a given path should be handled, and what the response should be. To do this, routes have two main parts: the handler, and the component. A route can have either one, or both, but never