fix: set lastProductId cookie in proxy instead of Server Component
Cookies can only be written in Server Actions or Route Handlers. Moved the write to proxy.ts where NextResponse.cookies.set is allowed. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
parent
d27c55c7fc
commit
1abbd4e5e4
2 changed files with 17 additions and 4 deletions
18
proxy.ts
18
proxy.ts
|
|
@ -5,6 +5,9 @@ import { sessionOptions } from '@/lib/session'
|
|||
const protectedRoutes = ['/dashboard', '/products', '/todos', '/settings', '/solo']
|
||||
const authRoutes = ['/login', '/register']
|
||||
|
||||
const SOLO_ROUTE = /^\/products\/([^/]+)\/solo$/
|
||||
const THIRTY_DAYS_SECONDS = 60 * 60 * 24 * 30
|
||||
|
||||
export function proxy(request: NextRequest) {
|
||||
const path = request.nextUrl.pathname
|
||||
const isProtected = protectedRoutes.some(r => path.startsWith(r))
|
||||
|
|
@ -21,7 +24,20 @@ export function proxy(request: NextRequest) {
|
|||
return NextResponse.redirect(new URL('/dashboard', request.url))
|
||||
}
|
||||
|
||||
return NextResponse.next()
|
||||
const response = NextResponse.next()
|
||||
|
||||
// Remember last visited product for /solo redirect
|
||||
const soloMatch = path.match(SOLO_ROUTE)
|
||||
if (soloMatch) {
|
||||
response.cookies.set('lastProductId', soloMatch[1], {
|
||||
httpOnly: true,
|
||||
sameSite: 'lax',
|
||||
maxAge: THIRTY_DAYS_SECONDS,
|
||||
path: '/',
|
||||
})
|
||||
}
|
||||
|
||||
return response
|
||||
}
|
||||
|
||||
export const config = {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue