diff --git a/docs/canary/concepts/app-wrapper.md b/docs/canary/concepts/app-wrapper.md deleted file mode 100644 index 47a96c34..00000000 --- a/docs/canary/concepts/app-wrapper.md +++ /dev/null @@ -1,104 +0,0 @@ ---- -description: | - Add a global app wrapper to provide common meta tags or context for application routes. ---- - -An app wrapper is defined in an `_app.tsx` file in `routes/` folder and is -typically used to create the outer structure of an HTML document. It must -contain a default export that is a regular Preact component. Only one such -wrapper is allowed per application. - -The component to be wrapped is received via props, in addition to a few other -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/_app.tsx -import { AppProps } from "$fresh/server.ts"; - -export default function App({ Component, state }: AppProps) { - // do something with state here - return ( - - - - - My Fresh app - - - - - - ); -} -``` - -## Async app wrapper - -Similar to routes and layouts, the app wrapper can be made asynchronous. This -changes the function signature so that the first argument is the `Request` -instsance and the second one is the `AppContext`. - -```tsx routes/_app.tsx -import { AppContext } from "$fresh/server.ts"; - -export default async function App(req: Request, ctx: AppContext) { - const data = await loadData(); - - return ( - - - - - My Fresh app - - -

Hello {data.name}

- - - - ); -} -``` - -### Define helper - -To make it quicker to type the async app wrapper, Fresh includes a `defineApp` -helper which already infers the correct types for you. - -```tsx routes/_app.tsx -import { defineApp } from "$fresh/server.ts"; - -export default defineApp(async (req, ctx) => { - const data = await loadData(); - - return ( - - - - - My Fresh app - - -

Hello {data.name}

- - - - ); -}); -``` - -## Disabling the app wrapper - -Rendering the app wrapper can be skipped on a route or layout basis. To do that, -set `skipAppWrapper: true` to the layout or route config. - -```tsx routes/my-special-route.tsx -export const config: RouteConfig { - skipAppWrapper: true; // Skip the app wrapper during rendering -} - -export default function Page() { - // ... -} -``` diff --git a/docs/canary/concepts/middleware.md b/docs/canary/concepts/middleware.md deleted file mode 100644 index e510c3e1..00000000 --- a/docs/canary/concepts/middleware.md +++ /dev/null @@ -1,197 +0,0 @@ ---- -description: | - 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 -request in order for you to perform custom logic before or after the route -handler. This allows modifying or checking requests and responses. Common -use-cases for this are logging, authentication, and performance monitoring. - -Each middleware gets passed a `next` function in the context argument that is -used to trigger child handlers. The `ctx` also has a `state` property that can -be used to pass arbitrary data to downstream (or upstream) handlers. This -`state` is included in `PageProps` by default, which is available to both the -special [\_app](/docs/concepts/app-wrapper.md) wrapper and normal -[routes](/docs/concepts/routes.md). `ctx.state` is normally set by modifying its -properties, e.g. `ctx.state.loggedIn = true`, but you can also replace the -entire object like `ctx.state = { loggedIn = true }`. - -```ts routes/_middleware.ts -import { MiddlewareHandlerContext } from "$fresh/server.ts"; - -interface State { - data: string; -} - -export async function handler( - req: Request, - ctx: MiddlewareHandlerContext -) { - ctx.state.data = "myData"; - const resp = await ctx.next(); - resp.headers.set("server", "fresh server"); - return resp; -} -``` - -```ts routes/myHandler.ts -export const handler: Handlers = { - GET(_req, ctx) { - return new Response(`middleware data is ${ctx.state.data}`); - }, -}; -``` - -Middlewares are scoped and can be layered. This means a project can have -multiple middlewares, each covering a different set of routes. If multiple -middlewares cover a route, they will all be run, in order of specificity (least -specific first). - -For example, take a project with the following routes: - -```txt Project Structure -└── routes -    ├── _middleware.ts -    ├── index.ts -    └── admin -       ├── _middleware.ts -       └── index.ts -       └── signin.ts -``` - -For a request to `/` the request will flow like this: - -1. The `routes/_middleware.ts` middleware is invoked. -2. Calling `ctx.next()` will invoke the `routes/index.ts` handler. - -For a request to `/admin` the request flows like this: - -1. The `routes/_middleware.ts` middleware is invoked. -2. Calling `ctx.next()` will invoke the `routes/admin/_middleware.ts` - middleware. -3. Calling `ctx.next()` will invoke the `routes/admin/index.ts` handler. - -For a request to `/admin/signin` the request flows like this: - -1. The `routes/_middleware.ts` middleware is invoked. -2. Calling `ctx.next()` will invoke the `routes/admin/_middleware.ts` - middleware. -3. Calling `ctx.next()` will invoke the `routes/admin/signin.ts` handler. - -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 -export const handler = [ - async function middleware1(req, ctx) { - // do something - return ctx.next(); - }, - async function middleware2(req, ctx) { - // do something - return ctx.next(); - }, -]; -``` - -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 routes/[tenant]/admin/_middleware.ts -import { MiddlewareHandlerContext } from "$fresh/server.ts"; - -export async function handler(_req: Request, ctx: MiddlewareHandlerContext) { - const currentTenant = ctx.params.tenant; - //do something with the tenant - const resp = await ctx.next(); - return resp; -} -``` - -and the request is to `mysaas.com/acme/admin/`, then `currentTenant` will have -the value of `acme` in your middleware. - -## Middleware Destination - -To set the stage for this section, `MiddlewareHandlerContext` looks like this: - -```ts -export interface MiddlewareHandlerContext> { - next: () => Promise; - state: State; - destination: router.DestinationKind; - remoteAddr: { - transport: "tcp" | "udp"; - hostname: string; - port: number; - }; -} -``` - -and `router.DestinationKind` is defined like this: - -```ts -export type DestinationKind = "internal" | "static" | "route" | "notFound"; -``` - -This is useful if you want your middleware to only run when a request is headed -for a `route`, as opposed to something like `http://localhost:8001/favicon.ico`. - -### Example - -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 routes/_middleware.ts -import { MiddlewareHandlerContext } from "$fresh/server.ts"; - -export async function handler(req: Request, ctx: MiddlewareHandlerContext) { - console.log(ctx.destination); - console.log(req.url); - const resp = await ctx.next(); - return resp; -} -``` - -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. - - 🍋 Fresh ready - Local: http://localhost:8000/ - -route -http://localhost:8000/ -internal -http://localhost:8000/_frsh/js/3c7400558fc00915df88cb181036c0dbf73ab7f5/deserializer.js -internal -http://localhost:8000/_frsh/js/3c7400558fc00915df88cb181036c0dbf73ab7f5/signals.js -internal -http://localhost:8000/_frsh/js/3c7400558fc00915df88cb181036c0dbf73ab7f5/plugin-twind-main.js -internal -http://localhost:8000/_frsh/js/3c7400558fc00915df88cb181036c0dbf73ab7f5/main.js -internal -http://localhost:8000/_frsh/js/3c7400558fc00915df88cb181036c0dbf73ab7f5/island-counter.js -internal -http://localhost:8000/_frsh/refresh.js -static -http://localhost:8000/logo.svg?__frsh_c=3c7400558fc00915df88cb181036c0dbf73ab7f5 -internal -http://localhost:8000/_frsh/alive -internal -http://localhost:8000/_frsh/js/3c7400558fc00915df88cb181036c0dbf73ab7f5/chunk-PDMKJVJ5.js -internal -http://localhost:8000/_frsh/js/3c7400558fc00915df88cb181036c0dbf73ab7f5/chunk-UGFDDSOV.js -internal -http://localhost:8000/_frsh/js/3c7400558fc00915df88cb181036c0dbf73ab7f5/chunk-RCK7U3UF.js -``` - -That first `route` request is for when `Fresh` responds with the root level -`index.tsx` route. The rest, as you can see, are either `internal` or `static` -requests. You can use `ctx.destination` to filter these out if your middleware -is only supposed to deal with routes. diff --git a/docs/canary/concepts/ahead-of-time-builds.md b/docs/latest/concepts/ahead-of-time-builds.md similarity index 100% rename from docs/canary/concepts/ahead-of-time-builds.md rename to docs/latest/concepts/ahead-of-time-builds.md diff --git a/docs/latest/concepts/app-wrapper.md b/docs/latest/concepts/app-wrapper.md index a346d25e..47a96c34 100644 --- a/docs/latest/concepts/app-wrapper.md +++ b/docs/latest/concepts/app-wrapper.md @@ -1,10 +1,11 @@ --- description: | - Add a global application wrapper to provide common meta tags or context for application routes. + Add a global app wrapper to provide common meta tags or context for application routes. --- -An application wrapper is defined in an `_app.tsx` file in `routes/` folder. It -must contain a default export that is a regular Preact component. Only one such +An app wrapper is defined in an `_app.tsx` file in `routes/` folder and is +typically used to create the outer structure of an HTML document. It must +contain a default export that is a regular Preact component. Only one such wrapper is allowed per application. The component to be wrapped is received via props, in addition to a few other @@ -16,11 +17,88 @@ set by middleware is available via `props.state`. import { AppProps } from "$fresh/server.ts"; export default function App({ Component, state }: AppProps) { - //do something with state here + // do something with state here return ( -
- -
+ + + + + My Fresh app + + + + + ); } ``` + +## Async app wrapper + +Similar to routes and layouts, the app wrapper can be made asynchronous. This +changes the function signature so that the first argument is the `Request` +instsance and the second one is the `AppContext`. + +```tsx routes/_app.tsx +import { AppContext } from "$fresh/server.ts"; + +export default async function App(req: Request, ctx: AppContext) { + const data = await loadData(); + + return ( + + + + + My Fresh app + + +

Hello {data.name}

+ + + + ); +} +``` + +### Define helper + +To make it quicker to type the async app wrapper, Fresh includes a `defineApp` +helper which already infers the correct types for you. + +```tsx routes/_app.tsx +import { defineApp } from "$fresh/server.ts"; + +export default defineApp(async (req, ctx) => { + const data = await loadData(); + + return ( + + + + + My Fresh app + + +

Hello {data.name}

+ + + + ); +}); +``` + +## Disabling the app wrapper + +Rendering the app wrapper can be skipped on a route or layout basis. To do that, +set `skipAppWrapper: true` to the layout or route config. + +```tsx routes/my-special-route.tsx +export const config: RouteConfig { + skipAppWrapper: true; // Skip the app wrapper during rendering +} + +export default function Page() { + // ... +} +``` diff --git a/docs/canary/concepts/layouts.md b/docs/latest/concepts/layouts.md similarity index 100% rename from docs/canary/concepts/layouts.md rename to docs/latest/concepts/layouts.md diff --git a/docs/latest/concepts/middleware.md b/docs/latest/concepts/middleware.md index 55d13908..f232ac39 100644 --- a/docs/latest/concepts/middleware.md +++ b/docs/latest/concepts/middleware.md @@ -13,7 +13,9 @@ used to trigger child handlers. The `ctx` also has a `state` property that can be used to pass arbitrary data to downstream (or upstream) handlers. This `state` is included in `PageProps` by default, which is available to both the special [\_app](/docs/concepts/app-wrapper.md) wrapper and normal -[routes](/docs/concepts/routes.md). +[routes](/docs/concepts/routes.md). `ctx.state` is normally set by modifying its +properties, e.g. `ctx.state.loggedIn = true`, but you can also replace the +entire object like `ctx.state = { loggedIn = true }`. ```ts routes/_middleware.ts import { MiddlewareHandlerContext } from "$fresh/server.ts"; @@ -207,11 +209,12 @@ export function handler(req: Request): Response { `307` stands for temporary redirect. You can also use `301` for permanent redirect. You can also redirect to a relative path by doing: -```ts +````ts export function handler(req: Request): Response { return new Response("", { status: 307, headers: { Location: "/my/new/relative/path" }, }); } -``` +```**** +```` diff --git a/docs/toc.ts b/docs/toc.ts index d261f37d..65090aa5 100644 --- a/docs/toc.ts +++ b/docs/toc.ts @@ -48,15 +48,15 @@ const toc: RawTableOfContents = { ["server-components", "Server Components", "link:latest"], ["routing", "Routing", "link:latest"], ["routes", "Routes", "link:latest"], - ["app-wrapper", "App wrapper", "link:canary"], - ["layouts", "Layouts", "link:canary"], + ["app-wrapper", "App wrapper", "link:latest"], + ["layouts", "Layouts", "link:latest"], ["forms", "Forms", "link:latest"], ["islands", "Interactive islands", "link:latest"], ["static-files", "Static files", "link:latest"], - ["middleware", "Middlewares", "link:canary"], + ["middleware", "Middlewares", "link:latest"], ["error-pages", "Error pages", "link:latest"], ["data-fetching", "Data fetching", "link:latest"], - ["ahead-of-time-builds", "Ahead-of-time Builds", "link:canary"], + ["ahead-of-time-builds", "Ahead-of-time Builds", "link:latest"], ["deployment", "Deployment", "link:latest"], ["plugins", "Plugins", "link:latest"], ["updating", "Updating Fresh", "link:latest"], @@ -125,18 +125,20 @@ const toc: RawTableOfContents = { pages: [ ["architecture", "Architecture"], ["server-components", "Server Components"], + ["routing", "Routing"], ["routes", "Routes"], + ["app-wrapper", "App wrapper"], + ["layouts", "Layouts"], ["forms", "Forms"], ["islands", "Interactive islands"], ["static-files", "Static files"], - ["middleware", "Route middleware"], + ["middleware", "Middlewares"], ["error-pages", "Error pages"], - ["routing", "Routing"], ["data-fetching", "Data fetching"], + ["ahead-of-time-builds", "Ahead-of-time Builds"], ["deployment", "Deployment"], ["plugins", "Plugins"], ["updating", "Updating Fresh"], - ["app-wrapper", "Application wrapper"], ["server-configuration", "Server configuration"], ], }, @@ -147,7 +149,6 @@ const toc: RawTableOfContents = { title: "Examples", pages: [ ["modifying-the-head", "Modifying the "], - ["setting-the-language", "Setting the language"], ["writing-tests", "Writing tests"], ["changing-the-src-dir", "Changing the source directory"], ["using-twind-v1", "Using Twind v1"], diff --git a/versions.json b/versions.json index d8eb84c2..2a6082d1 100644 --- a/versions.json +++ b/versions.json @@ -1,4 +1,5 @@ [ + "1.4.0", "1.3.1", "1.3.0", "1.2.0",