-
Notifications
You must be signed in to change notification settings - Fork 53
Expand file tree
/
Copy pathproxy.ts
More file actions
45 lines (40 loc) · 1.73 KB
/
Copy pathproxy.ts
File metadata and controls
45 lines (40 loc) · 1.73 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
import { NextResponse } from 'next/server';
import { encodeRequestContext } from '@/utils/request-context';
import type { NextRequest } from 'next/server';
function isUserAuthenticated(request: NextRequest): boolean {
return !!request.cookies.get('selectedAccountId')?.value;
}
export function proxy(request: NextRequest) {
/**
* Examples of other data you could include in the encoded context:
*
* locale?: string; // 'en', 'no', 'sv' - for internationalization
* theme?: 'light' | 'dark'; // user theme preference
* userGroups?: string[]; // ['gamer', 'developer'] - for targeted content
* userType?: 'b2c' | 'b2b'; // business vs consumer
* debugMode?: boolean; // for development/testing
* featureFlags?: string[]; // ['newCheckout', 'betaFeatures']
* region?: string; // 'eu', 'us', 'asia' - for regional content
* currency?: string; // 'USD', 'EUR', 'NOK' - for pricing
* experiments?: Record<string, string>; // A/B testing variants
*/
const encodedContext = encodeRequestContext({
loggedIn: isUserAuthenticated(request),
});
// Rewrites the request to include the encoded context
const nextUrl = new URL(`/${encodedContext}${request.nextUrl.pathname}${request.nextUrl.search}`, request.url);
return NextResponse.rewrite(nextUrl, { request });
}
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)
* - any file with an extension
*/
'/((?!api|_next/static|_next/image|favicon.ico|.*\\..*).*)',
],
};