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.
|
||||
|
||||
Reference in New Issue
Block a user