-
Notifications
You must be signed in to change notification settings - Fork 5
/
middleware.ts
31 lines (26 loc) · 916 Bytes
/
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
import { NextRequest, NextResponse } from "next/server";
export function middleware(request: NextRequest) {
const response = NextResponse.next();
// Apply CORS headers to all API routes for GET requests
if (request.nextUrl.pathname.startsWith("/api") && request.method === "GET") {
response.headers.set("Access-Control-Allow-Origin", "*");
response.headers.set("Access-Control-Allow-Methods", "GET");
response.headers.set("Access-Control-Allow-Headers", "Content-Type");
}
// Handle preflight requests
if (request.method === "OPTIONS") {
const headers = {
"Access-Control-Allow-Origin": "*",
"Access-Control-Allow-Methods": "GET",
"Access-Control-Allow-Headers": "Content-Type",
};
return new Response(null, {
headers,
status: 204,
});
}
return response;
}
export const config = {
matcher: "/api/:path*", // Apply to all API routes
};