-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmiddleware.ts
38 lines (30 loc) · 1.14 KB
/
middleware.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
import { NextRequest, NextResponse } from "next/server";
const ignorePathPrefix = ["/_next", "/assets", "/favicon.ico", "/vercel.svg"];
const isPathIgnorable = (path: string) =>
ignorePathPrefix.some((prefix) => path.startsWith(prefix));
export function middleware(req: NextRequest) {
if (!isPathIgnorable(req.nextUrl.pathname)) {
}
const response = NextResponse.next();
if (req.nextUrl.pathname.startsWith("/api")) {
response.headers.append(
"Access-Control-Allow-Origin",
req.headers.get("origin") || "*"
);
response.headers.append("Access-Control-Allow-Credentials", "true");
response.headers.append("Vary", "Origin");
response.headers.append(
"Access-Control-Allow-Methods",
"GET, POST, PUT, DELETE, OPTIONS"
);
response.headers.append(
"Access-Control-Allow-Headers",
"Authorization, X-CSRF-Token, X-Requested-With, Accept, Accept-Version, Content-Length, Content-MD5, Content-Type, Date, X-Api-Version, X-Requested-With"
);
if (req.method == "OPTIONS") {
return response;
}
response.headers.append("Content-Type", "application/json");
}
return response;
}