-
-
Notifications
You must be signed in to change notification settings - Fork 2.3k
Expand file tree
/
Copy pathproxy.ts
More file actions
53 lines (43 loc) · 1.69 KB
/
proxy.ts
File metadata and controls
53 lines (43 loc) · 1.69 KB
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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
import { type NextRequest, NextResponse } from 'next/server'
import { updateSession } from '@/lib/supabase/middleware'
export async function proxy(request: NextRequest) {
// 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}`
// Create a response
let response: NextResponse
// Handle Supabase session if configured
const supabaseUrl = process.env.NEXT_PUBLIC_SUPABASE_URL
const supabaseAnonKey = process.env.NEXT_PUBLIC_SUPABASE_ANON_KEY
if (supabaseUrl && supabaseAnonKey) {
response = await updateSession(request)
} else {
// If Supabase is not configured, just pass the request through
response = NextResponse.next({
request
})
}
// 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
}
export const config = {
matcher: [
/*
* Match all request paths except for the ones starting with:
* - _next/static (static files)
* - _next/image (image optimization files)
* - favicon.ico (favicon file)
* Feel free to modify this pattern to include more paths.
*/
'/((?!_next/static|_next/image|favicon.ico|.*\\.(?:svg|png|jpg|jpeg|gif|webp)$).*)'
]
}