docs: add "Dealing with CORS" example (#1347)

This commit is contained in:
Reed von Redwitz
2023-06-24 11:10:12 +00:00
committed by GitHub
parent 30349db939
commit 0c187c145a
4 changed files with 105 additions and 2 deletions
+1 -1
View File
@@ -58,7 +58,7 @@ export interface RouterOptions {
## Plugins
See the [docs](./docs/concepts/plugins) on this topic for more detail. But for
See the [docs](/docs/concepts/plugins) on this topic for more detail. But for
completion, you can do something like this to load plugins:
```ts
+101
View File
@@ -0,0 +1,101 @@
---
description: |
CORS enabling routes in your Fresh project.
---
So you've encountered some CORS problems and are on the hunt for the solution?
You're in the right spot.
Here's a good [resource](https://developer.mozilla.org/en-US/docs/Web/HTTP/CORS)
talking about CORS in general, in case you don't fully understand what's wrong.
## Simple CORS -- Middleware
As per the above link, "simple" requests involve `GET`, `HEAD`, or `POST`
requests. You can CORS enable all the routes affected by some `middleware` by
doing the following:
```ts
import { MiddlewareHandlerContext } from "$fresh/server.ts";
export async function handler(
req: Request,
ctx: MiddlewareHandlerContext,
) {
const origin = req.headers.get("Origin") || "*";
const resp = await ctx.next();
const headers = resp.headers;
headers.set("Access-Control-Allow-Origin", origin);
headers.set("Access-Control-Allow-Credentials", "true");
headers.set(
"Access-Control-Allow-Headers",
"Content-Type, Content-Length, Accept-Encoding, X-CSRF-Token, Authorization, accept, origin, Cache-Control, X-Requested-With",
);
headers.set(
"Access-Control-Allow-Methods",
"POST, OPTIONS, GET, PUT, DELETE",
);
return resp;
}
```
## Complex CORS -- Middleware
What about for one of the other HTTP methods? Then you'll need to be able to
deal with "preflight requests". Let's imagine you're trying to support a
`DELETE` route. Then you'd need to do something like this:
```ts
import { MiddlewareHandlerContext } from "$fresh/server.ts";
export async function handler(
_req: Request,
ctx: MiddlewareHandlerContext,
) {
if (_req.method == "OPTIONS") {
const resp = new Response(null, {
status: 204,
});
const origin = _req.headers.get("Origin") || "*";
const headers = resp.headers;
headers.set("Access-Control-Allow-Origin", origin);
headers.set(
"Access-Control-Allow-Methods",
"DELETE",
);
return resp;
}
const origin = _req.headers.get("Origin") || "*";
const resp = await ctx.next();
const headers = resp.headers;
headers.set("Access-Control-Allow-Origin", origin);
headers.set("Access-Control-Allow-Credentials", "true");
headers.set(
"Access-Control-Allow-Headers",
"Content-Type, Content-Length, Accept-Encoding, X-CSRF-Token, Authorization, accept, origin, Cache-Control, X-Requested-With",
);
headers.set(
"Access-Control-Allow-Methods",
"POST, OPTIONS, GET, PUT, DELETE",
);
return resp;
}
```
These complex results require a two step process:
1. the browser makes an `OPTIONS` request to find out about the allowed methods
2. the browser makes the actual request
So you can see the middleware has some special handling to deal with `OPTIONS`
requests.
## CORS in Routes
Of course there's no reason why you need to use middleware in order to solve
this. The headers can be set directly in the
[handler](/docs/getting-started/custom-handlers) as well.
+1
View File
@@ -14,3 +14,4 @@ like to see here, please open
- [Changing the source directory](./examples/changing-the-src-dir)
- [Initializing the server](./examples/init-the-server)
- [Using Fresh canary version](./examples/using-fresh-canary-version)
- [Dealing with CORS](./examples/dealing-with-cors)
+2 -1
View File
@@ -47,7 +47,8 @@
["changing-the-src-dir", "Changing the source directory"],
["using-twind-v1", "Using Twind v1"],
["init-the-server", "Initializing the server"],
["using-fresh-canary-version", "Using Fresh canary version"]
["using-fresh-canary-version", "Using Fresh canary version"],
["dealing-with-cors", "Dealing with CORS"]
]
}
}