Skip to content

Commit

Permalink
Refactor middleware.ts to remove unnecessary code and fix header mani…
Browse files Browse the repository at this point in the history
…pulation
  • Loading branch information
marcinciarka committed Oct 21, 2024
1 parent 90f51fb commit e1e0900
Showing 1 changed file with 4 additions and 11 deletions.
15 changes: 4 additions & 11 deletions middleware.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,6 @@ import type { NextRequest } from 'next/server'
import { NextResponse } from 'next/server'
import { handleRewrite } from 'server/rewrites'

function getRandomishId() {
const chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789'
let result = ''
for (let i = 0; i < 10; i++) {
result += chars.charAt(Math.floor(Math.random() * chars.length))
}
return result
}

export function middleware(request: NextRequest) {
const allowedOrigins = process.env.ALLOWED_ORIGINS?.split(',') || []

Expand All @@ -22,8 +13,10 @@ export function middleware(request: NextRequest) {

const response = NextResponse.next()
const origin = request.headers.get('origin') || ''
const userAgentHeader = request.headers.get('user-agent') || ''
request.headers.set('user-agent', `${userAgentHeader} Summer.fi/${getRandomishId()}`)

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)) {
Expand Down

0 comments on commit e1e0900

Please sign in to comment.