-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test middleware to fix CORS issue with sentry
- Loading branch information
Showing
1 changed file
with
31 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
import { NextResponse } from 'next/server' | ||
import type { NextRequest } from 'next/server' | ||
|
||
export function middleware(request: NextRequest) { | ||
// Get the origin of the request | ||
const origin = request.headers.get('origin') || '' | ||
|
||
// Create response object | ||
const response = NextResponse.next() | ||
|
||
// Add CORS headers | ||
response.headers.set('Access-Control-Allow-Origin', origin) | ||
response.headers.set('Access-Control-Allow-Methods', 'GET, POST, PUT, DELETE, OPTIONS') | ||
response.headers.set('Access-Control-Allow-Headers', 'Content-Type, Authorization, sentry-trace') | ||
response.headers.set('Access-Control-Allow-Credentials', 'true') | ||
|
||
// Handle preflight requests | ||
if (request.method === 'OPTIONS') { | ||
return new NextResponse(null, { | ||
status: 200, | ||
headers: response.headers, | ||
}) | ||
} | ||
|
||
return response | ||
} | ||
|
||
// Configure which paths should be handled by the middleware | ||
export const config = { | ||
matcher: '/api/:path*', // Applies to all API routes | ||
} |