38 lines
975 B
TypeScript
38 lines
975 B
TypeScript
import { NextResponse } from 'next/server';
|
|
import type { NextRequest } from 'next/server';
|
|
|
|
export function middleware(request: NextRequest) {
|
|
const url = request.nextUrl;
|
|
|
|
// Check if there is a referral parameter ?ref=
|
|
const ref = url.searchParams.get('ref');
|
|
|
|
if (ref) {
|
|
const response = NextResponse.redirect(url.pathname);
|
|
// Set cookie for 30 days
|
|
response.cookies.set('vw_referral_agent', ref, {
|
|
maxAge: 30 * 24 * 60 * 60,
|
|
path: '/',
|
|
httpOnly: true,
|
|
secure: process.env.NODE_ENV === 'production',
|
|
sameSite: 'lax',
|
|
});
|
|
return response;
|
|
}
|
|
|
|
return NextResponse.next();
|
|
}
|
|
|
|
export const config = {
|
|
matcher: [
|
|
/*
|
|
* Match all request paths except for the ones starting with:
|
|
* - api (API routes)
|
|
* - _next/static (static files)
|
|
* - _next/image (image optimization files)
|
|
* - favicon.ico (favicon file)
|
|
*/
|
|
'/((?!api|_next/static|_next/image|favicon.ico).*)',
|
|
],
|
|
};
|