www: new fenced code block design (#1629)
This commit is contained in:
@@ -17,8 +17,7 @@ special [\_app](/docs/concepts/app-wrapper.md) wrapper and normal
|
||||
properties, e.g. `ctx.state.loggedIn = true`, but you can also replace the
|
||||
entire object like `ctx.state = { loggedIn = true }`.
|
||||
|
||||
```ts
|
||||
// routes/_middleware.ts
|
||||
```ts routes/_middleware.ts
|
||||
import { MiddlewareHandlerContext } from "$fresh/server.ts";
|
||||
|
||||
interface State {
|
||||
@@ -27,7 +26,7 @@ interface State {
|
||||
|
||||
export async function handler(
|
||||
req: Request,
|
||||
ctx: MiddlewareHandlerContext<State>,
|
||||
ctx: MiddlewareHandlerContext<State>
|
||||
) {
|
||||
ctx.state.data = "myData";
|
||||
const resp = await ctx.next();
|
||||
@@ -36,8 +35,7 @@ export async function handler(
|
||||
}
|
||||
```
|
||||
|
||||
```ts
|
||||
// routes/myHandler.ts
|
||||
```ts routes/myHandler.ts
|
||||
export const handler: Handlers<any, { data: string }> = {
|
||||
GET(_req, ctx) {
|
||||
return new Response(`middleware data is ${ctx.state.data}`);
|
||||
@@ -52,7 +50,7 @@ specific first).
|
||||
|
||||
For example, take a project with the following routes:
|
||||
|
||||
```
|
||||
```txt Project Structure
|
||||
└── routes
|
||||
├── _middleware.ts
|
||||
├── index.ts
|
||||
@@ -85,9 +83,7 @@ A single middleware file can also define multiple middlewares (all for the same
|
||||
route) by exporting an array of handlers instead of a single handler. For
|
||||
example:
|
||||
|
||||
```ts
|
||||
// routes/_middleware.ts
|
||||
|
||||
```ts routes/_middleware.ts
|
||||
export const handler = [
|
||||
async function middleware1(req, ctx) {
|
||||
// do something
|
||||
@@ -103,7 +99,7 @@ export const handler = [
|
||||
It should be noted that `middleware` has access to route parameters. If you're
|
||||
running a fictitious `routes/[tenant]/admin/_middleware.ts` like this:
|
||||
|
||||
```ts
|
||||
```ts routes/[tenant]/admin/_middleware.ts
|
||||
import { MiddlewareHandlerContext } from "$fresh/server.ts";
|
||||
|
||||
export async function handler(_req: Request, ctx: MiddlewareHandlerContext) {
|
||||
@@ -148,7 +144,7 @@ for a `route`, as opposed to something like `http://localhost:8001/favicon.ico`.
|
||||
Initiate a new Fresh project (`deno run -A -r https://fresh.deno.dev/`) and then
|
||||
create a `_middleware.ts` file in the `routes` folder like this:
|
||||
|
||||
```ts
|
||||
```ts routes/_middleware.ts
|
||||
import { MiddlewareHandlerContext } from "$fresh/server.ts";
|
||||
|
||||
export async function handler(req: Request, ctx: MiddlewareHandlerContext) {
|
||||
@@ -161,7 +157,7 @@ export async function handler(req: Request, ctx: MiddlewareHandlerContext) {
|
||||
|
||||
If you start up your server (`deno task start`) you'll see the following:
|
||||
|
||||
```
|
||||
```sh Terminal
|
||||
Task start deno run -A --watch=static/,routes/ dev.ts
|
||||
Watcher Process started.
|
||||
The manifest has been generated for 4 routes and 1 islands.
|
||||
|
||||
@@ -20,13 +20,13 @@ Let's illustrate that with an example:
|
||||
Without any way to group routes this is a problem because every route segment
|
||||
can only have one `_layout` file.
|
||||
|
||||
```txt
|
||||
/routes
|
||||
/_layout.tsx <-- applies to all routes here :(
|
||||
/about.tsx
|
||||
/career.tsx
|
||||
/archive.tsx
|
||||
/contact.tsx
|
||||
```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
|
||||
@@ -34,25 +34,25 @@ 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
|
||||
/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
|
||||
```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
|
||||
> /routes
|
||||
> /(group-1)
|
||||
> /about.tsx <-- Bad: Both routes map to the same `/about` url
|
||||
> /(group-2)
|
||||
> /about.tsx <-- Bad: Both routes map to the same `/about` url
|
||||
> ```txt Project structure
|
||||
> └── routes
|
||||
> ├── (group-1)
|
||||
> │ └── about.tsx # Bad: Maps to same `/about` url
|
||||
> └── (group-2)
|
||||
> └── about.tsx # Bad: Maps to same `/about` url
|
||||
> ```
|
||||
|
||||
@@ -7,16 +7,16 @@ An route layout is defined in a `_layout.tsx` file in any sub directory (at any
|
||||
level) under the `routes/` folder. It must contain a default export that is a
|
||||
regular Preact component. Only one such layout is allowed per sub directory.
|
||||
|
||||
```sh
|
||||
routes/
|
||||
_app.tsx
|
||||
_layout.tsx # will be applied to all routes
|
||||
/sub
|
||||
index.tsx
|
||||
page.tsx
|
||||
/other
|
||||
_layout.tsx # will be applied on top of `routes/_layout.tsx`
|
||||
page.tsx
|
||||
```txt Project structure
|
||||
└── routes
|
||||
├── sub
|
||||
│ ├── page.tsx
|
||||
│ └── index.tss
|
||||
├── other
|
||||
│ ├── _layout.tsx # will be applied on top of `routes/_layout.tsx`
|
||||
│ └── page.tsx
|
||||
├── _layout.tsx # will be applied to all routes
|
||||
└── _app.tsx
|
||||
```
|
||||
|
||||
The component to be wrapped is received via props, in addition to a few other
|
||||
@@ -24,9 +24,7 @@ things. This allows for the introduction of a global container functioning as a
|
||||
template which can be conditioned based on state and params. Note that any state
|
||||
set by middleware is available via `props.state`.
|
||||
|
||||
```tsx
|
||||
// routes/sub/_layout.tsx
|
||||
|
||||
```tsx routes/sub/_layout.tsx
|
||||
import { LayoutProps } from "$fresh/server.ts";
|
||||
|
||||
export default function Layout({ Component, state }: LayoutProps) {
|
||||
@@ -45,20 +43,19 @@ Sometimes you want to opt out of the layout inheritance mechanism for a
|
||||
particular route. This can be done via route configuration. Picture a directory
|
||||
structure like this:
|
||||
|
||||
```sh
|
||||
routes/
|
||||
_layout.tsx
|
||||
/sub
|
||||
_layout.tsx
|
||||
index.tsx
|
||||
special.tsx # should not inherit layouts
|
||||
```txt Project structure
|
||||
└── routes
|
||||
├── sub
|
||||
│ ├── _layout_.tsx
|
||||
│ ├── special.tsx # should not inherit layouts
|
||||
│ └── index.tss
|
||||
└── _layout.tsx
|
||||
```
|
||||
|
||||
To make `routes/sub/special.tsx` opt out of rendering layouts we can set
|
||||
`rootLayout: true`.
|
||||
|
||||
```tsx
|
||||
// routes/sub/special.tsx
|
||||
```tsx routes/sub/special.tsx
|
||||
import { RouteConfig } from "$fresh/server.ts";
|
||||
|
||||
export const config: RouteConfig = {
|
||||
|
||||
Reference in New Issue
Block a user