www: Various documentation improvements (#1635)
Co-authored-by: Reed von Redwitz <reed+github@vonredwitz.com>
This commit is contained in:
co-authored by
Reed von Redwitz
parent
794c8ac10c
commit
b2362da281
@@ -165,3 +165,23 @@ export default async function MyPage(req: Request, ctx: RouteContext) {
|
||||
return <p>foo is: {value}</p>;
|
||||
}
|
||||
```
|
||||
|
||||
### Define helper
|
||||
|
||||
To make it a little quicker to write async routes, Fresh ships with a
|
||||
`defineRoute` helper which automatically infers the correct types for the
|
||||
function arguments.
|
||||
|
||||
```tsx
|
||||
import { defineRoute } from "$fresh/server.ts";
|
||||
|
||||
export default defineRoute(async (req, ctx) => {
|
||||
const data = await loadData();
|
||||
|
||||
return (
|
||||
<div class="page">
|
||||
<h1>Hello {data.name}</h1>
|
||||
</div>
|
||||
);
|
||||
});
|
||||
```
|
||||
|
||||
@@ -51,4 +51,60 @@ export const config: RouteConfig = {
|
||||
// ...
|
||||
```
|
||||
|
||||
## Route Groups
|
||||
|
||||
When working with [route layouts](/docs/canary/concepts/route-layout) or
|
||||
[route middlewares](/docs/canary/concepts/middleware), you'll sometimes come
|
||||
across a situation where you want your routes to inherit from a layout other
|
||||
than what's suggested by the URL segment.
|
||||
|
||||
Let's illustrate that with an example:
|
||||
|
||||
```txt
|
||||
/about -> layout A
|
||||
/career -> layout A
|
||||
/archive -> layout B
|
||||
/contact -> layout B
|
||||
```
|
||||
|
||||
Without any way to group routes this is a problem because every route segment
|
||||
can only have one `_layout` file.
|
||||
|
||||
```txt Project structure
|
||||
└── routes
|
||||
├── _layout.tsx # applies to all routes here :(
|
||||
├── about.tsx
|
||||
├── career.tsx
|
||||
├── archive.tsx
|
||||
└── contact.tsx
|
||||
```
|
||||
|
||||
We can solve this problem with route groups. A route group is a folder which has
|
||||
a name that is wrapped in braces. For example `(pages)` would be considered a
|
||||
route and so would be `(marketing)`. This enables us to group related routes in
|
||||
a folder and use a different `_layout` file for each group.
|
||||
|
||||
```txt Project structure
|
||||
└── routes
|
||||
├── (marketing)
|
||||
│ ├── _layout.tsx # only applies to about.tsx and career.tsx
|
||||
│ ├── about.tsx
|
||||
│ └── career.tsx
|
||||
└── (info)
|
||||
├── _layout.tsx # only applies to archive.tsx and contact.tsx
|
||||
├── archive.tsx
|
||||
└── contact.tsx
|
||||
```
|
||||
|
||||
> ℹ️ Be careful about routes in different groups which match to the same URL.
|
||||
> Such scenarios will lead to ambiguity as to which route file should be picked.
|
||||
>
|
||||
> ```txt Project structure
|
||||
> └── routes
|
||||
> ├── (group-1)
|
||||
> │ └── about.tsx # Bad: Maps to same `/about` url
|
||||
> └── (group-2)
|
||||
> └── about.tsx # Bad: Maps to same `/about` url
|
||||
> ```
|
||||
|
||||
[urlpattern]: https://developer.mozilla.org/en-US/docs/Web/API/URL_Pattern_API
|
||||
|
||||
Reference in New Issue
Block a user