forked from miurla/morphic
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmiddleware.ts
26 lines (20 loc) · 918 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
import type { NextRequest } from 'next/server'
import { NextResponse } from 'next/server'
export function middleware(request: NextRequest) {
// Create a response
const response = NextResponse.next()
// Get the protocol from X-Forwarded-Proto header or request protocol
const protocol =
request.headers.get('x-forwarded-proto') || request.nextUrl.protocol
// Get the host from X-Forwarded-Host header or request host
const host =
request.headers.get('x-forwarded-host') || request.headers.get('host') || ''
// Construct the base URL - ensure protocol has :// format
const baseUrl = `${protocol}${protocol.endsWith(':') ? '//' : '://'}${host}`
// Add request information to response headers
response.headers.set('x-url', request.url)
response.headers.set('x-host', host)
response.headers.set('x-protocol', protocol)
response.headers.set('x-base-url', baseUrl)
return response
}