docs: finish up concepts docs (#292)

This commit is contained in:
Luca Casonato
2022-06-28 15:47:56 +02:00
committed by GitHub
parent 27b67b47e1
commit 4d5f7a38c0
6 changed files with 130 additions and 6 deletions
+45 -1
View File
@@ -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<Project> = {
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<Project>) {
return (
<div>
<h1>{props.data.name}</h1>
<p>{props.data.stars} stars</p>
</div>
);
}
```
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
+37 -1
View File
@@ -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 <p>404 not found: {url.pathname}</p>;
}
```
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 <p>500 internal error: {(error as Error).message}</p>;
}
```
+42 -1
View File
@@ -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 (
<div>
Counter is at {count}.{" "}
<button onClick={() => setCount(count + 1)}>+</button>
</div>
);
}
```
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.
+1 -2
View File
@@ -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
-1
View File
@@ -1 +0,0 @@
> TODO(lucacasonato): this page still needs to be written
+5
View File
@@ -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