-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathproxy.ts
More file actions
38 lines (33 loc) · 1015 Bytes
/
Copy pathproxy.ts
File metadata and controls
38 lines (33 loc) · 1015 Bytes
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
import { NextResponse, type NextRequest } from 'next/server'
import { LAST_CHANNEL_COOKIE } from '@/features/user/session'
export function proxy(request: NextRequest) {
const { pathname } = request.nextUrl
if (pathname === '/') {
const channelId = request.cookies.get(LAST_CHANNEL_COOKIE)?.value
return channelId
? NextResponse.redirect(
new URL(`/channel/${encodeURIComponent(channelId)}`, request.url),
)
: NextResponse.next()
}
const channelId = pathname.match(/^\/channel\/([^/]+)$/)?.[1]
if (!channelId) return NextResponse.next()
const response = NextResponse.next()
response.cookies.set(LAST_CHANNEL_COOKIE, decodeURIComponent(channelId), {
path: '/',
sameSite: 'lax',
})
return response
}
export const config = {
matcher: [
'/',
{
missing: [
{ key: 'next-router-prefetch', type: 'header' },
{ key: 'purpose', type: 'header', value: 'prefetch' },
],
source: '/channel/:channelId',
},
],
}