forked from OasisDEX/oasis-borrow
-
Notifications
You must be signed in to change notification settings - Fork 0
/
middleware.ts
33 lines (25 loc) · 1014 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
32
33
import type { NextRequest } from 'next/server'
import { NextResponse } from 'next/server'
import { handleRewrite } from 'server/rewrites'
export function middleware(request: NextRequest) {
const allowedOrigins = process.env.ALLOWED_ORIGINS?.split(',') || []
const possibleRewrite = handleRewrite(request)
if (possibleRewrite) {
return possibleRewrite
}
const response = NextResponse.next()
const origin = request.headers.get('origin') || ''
if (request.headers.get('x-now-route-matches')) {
request.headers.delete('x-now-route-matches')
}
// If the origin is in the ALLOWED_ORIGINS env, add it to the Access-Control-Allow-Origin header
if (allowedOrigins.includes(origin)) {
response.headers.set('Access-Control-Allow-Origin', origin)
}
// Get `CloudFront-Viewer-Country` header if exists from request and set cookie
const country = request.headers.get('CloudFront-Viewer-Country')
if (country) {
response.cookies.set('country', country)
}
return response
}