docs: update canary docs for Fresh 2 (#3108)

There are still some rough edges here and there, but it's in a much
better shape than what we have today.
This commit is contained in:
Marvin Hagemeister
2025-07-18 15:18:38 +02:00
committed by GitHub
parent a66e0df08d
commit a703e79181
29 changed files with 2094 additions and 62 deletions
+28
View File
@@ -0,0 +1,28 @@
---
description: "Set CORS HTTP headers with the cors middleware"
---
The `cors()` middleware can be used to add
[Cross-Origin-Resource-Sharing headers](https://developer.mozilla.org/en-US/docs/Web/HTTP/Guides/CORS)
to HTTP requests. These allow the server to indicate which origins (domains,
scheme or port) other than its own is permitted to load resources from.
```ts
import { cors } from "fresh";
const app = new App()
.use(cors({
origin: "http://example.com",
allowHeaders: ["X-Custom-Header", "Upgrade-Insecure-Requests"],
allowMethods: ["POST", "GET", "OPTIONS"],
exposeHeaders: ["Content-Length", "X-Kuma-Revision"],
maxAge: 600,
credentials: true,
}))
.get("/", () => new Response("hello"));
```
## Options
See the [API docs](https://jsr.io/@fresh/core/doc/~/cors) for a list of all
supported options
+25
View File
@@ -0,0 +1,25 @@
---
description: "Extend fresh with plugins"
---
Fresh itself can be extended through the methods available on the
[`App`](/docs/canary/concepts/app) class or on the `Builder` class. Most of the
features in Fresh itself are built using these APIs.
## Custom middlewares
If you need to modify requests, add HTTP headers or pass additional data to
other middlewares via `ctx.state`, then going with a middleware is the way to
go.
```ts
const addXFreshHeader = define.middleware(async (ctx) => {
const res = await ctx.next();
res.headers.set("X-Fresh", "served by Fresh");
return res;
});
```
Learn more about [middlewares](/docs/canary/concepts/middleware).
TODO: Show more ways
+25
View File
@@ -0,0 +1,25 @@
---
description: "Ensure URLs always end or never end with trailing slashes"
---
The `trailingSlashes()` middleware can be used to ensure URL pathnames always
end with a slash character or will never end with one. It redirects the user's
request respectively.
```ts
import { trailingSlashes } from "fresh";
const app = new App()
.use(trailingSlashes("never"))
.get("/", () => new Response("hello"));
```
Always append a trailing slash:
```ts
import { trailingSlashes } from "fresh";
const app = new App()
.use(trailingSlashes("always"))
.get("/", () => new Response("hello"));
```