feat: Added middleware for handling CORS

Added middleware for handling CORS and setting necessary headers
This commit is contained in:
Ayane Satomi 2023-10-21 14:55:25 +00:00
parent dd9b86af93
commit 3eaa783c15
2 changed files with 25 additions and 2 deletions

View file

@ -4,7 +4,8 @@
import * as $0 from "./routes/_404.tsx";
import * as $1 from "./routes/_app.tsx";
import * as $2 from "./routes/index.tsx";
import * as $2 from "./routes/_middleware.ts";
import * as $3 from "./routes/index.tsx";
import * as $$0 from "./islands/CounterCard.tsx";
import * as $$1 from "./islands/MarkdownContent.tsx";
@ -12,7 +13,8 @@ const manifest = {
routes: {
"./routes/_404.tsx": $0,
"./routes/_app.tsx": $1,
"./routes/index.tsx": $2,
"./routes/_middleware.ts": $2,
"./routes/index.tsx": $3,
},
islands: {
"./islands/CounterCard.tsx": $$0,

21
routes/_middleware.ts Normal file
View file

@ -0,0 +1,21 @@
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("Cache-Control", "public, max-age=14400, stale-while-revalidate=86400");
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",
);
return resp;
}