Skip to content

Commit

Permalink
test middleware to fix CORS issue with sentry
Browse files Browse the repository at this point in the history
  • Loading branch information
toddkao committed Dec 20, 2024
1 parent 1767a06 commit de6e580
Showing 1 changed file with 31 additions and 0 deletions.
31 changes: 31 additions & 0 deletions examples/nextjs/middleware.ts
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
}

0 comments on commit de6e580

Please sign in to comment.