chore: release 1.3.0 (#1460)

This commit is contained in:
Marvin Hagemeister
2023-07-18 18:09:13 +02:00
committed by GitHub
parent b3c22384e0
commit 2575ca5cad
13 changed files with 98 additions and 631 deletions
+56 -2
View File
@@ -18,8 +18,8 @@ The component is the template for a page. It is a JSX element that is rendered
on the server. The page component gets passed props that can be used by it to
determine exactly what should be rendered. By default components receives props
consisting of: the request URL, the matching route (as a string), the matches
from the URL pattern match, and any data passed to the handler's `render`
function.
from the URL pattern match, any state set by middleware, and any data passed to
the handler's `render` function.
## Handler route
@@ -88,3 +88,57 @@ export default function Page(props: PageProps) {
return <div>You are on the page '{props.url.href}'.</div>;
}
```
## Async route components
Having a separate route handler and component function is nice, when you want to
test these in isolation, but can become a bit cumbersome to maintain. They
require some additional indirection of declaring an interface for the component
`Data` when you're passing it around through `ctx.render()`.
```tsx
interface Data {
foo: number;
}
export const handler: Handlers<Data> = {
async GET(req, ctx) {
const value = await loadFooValue();
ctx.render({ foo: value });
},
};
export default function MyPage(props: PageProps<Data>) {
return <p>foo is: {props.data.foo}</p>;
}
```
When a route has both a component and a `GET` handler, they are typically very
closely coupled. With async route components you can merge the two together and
avoid having to create the `Data` interface boilerplate.
```tsx
// Async route component
export default async function MyPage(req: Request, ctx: RouteContext) {
const value = await loadFooValue();
return <p>foo is: {value}</p>;
}
```
The code gets a little shorter with async route components. Conceptually, you
can think of async route components inlining the `GET` handler into the
component function. Note, that you can still add additional HTTP handlers in the
same file like before.
```tsx
export const handler: Handlers = {
async POST(req) {
// ... do something here
},
};
export default async function MyPage(req: Request, ctx: RouteContext) {
const value = await loadFooValue();
return <p>foo is: {value}</p>;
}
```