-
Notifications
You must be signed in to change notification settings - Fork 0
/
middleware.ts
99 lines (86 loc) · 2.89 KB
/
middleware.ts
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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
import { type NextRequest, NextResponse, userAgent } from 'next/server';
import { handleDeployment } from './lib/services/deployment';
declare const console: Console;
export async function middleware(request: NextRequest) {
const headers = new Headers(request.headers);
console.log(
'req headers',
JSON.stringify(Array.from(headers.entries()), null, 2)
);
const { device, isBot } = userAgent(request);
const isMobile = device.type === 'mobile' ? '1' : '0';
const authToken = request.cookies.get('authToken');
const uuidCookie = request.cookies.get('ser_uuid');
const url = new URL(request.url);
const cpn = url.searchParams.get('cpn');
const lang = 'en';
const uuid = Date.now().toString();
if (!request.headers.get('x-deployment-override')) {
headers.set(
'nextLocation',
JSON.stringify({
ip: headers.get('x-real-ip') || '',
country: headers.get('x-vercel-ip-country') || '',
region: headers.get('x-vercel-ip-country-region') || '',
city: headers.get('x-vercel-ip-city') || '',
latitude: headers.get('x-vercel-ip-latitude') || '',
longitude: headers.get('x-vercel-ip-longitude') || '',
})
);
}
headers.set('ser_uuid', uuid);
headers.set('pathname', url.pathname);
headers.set('params', url.searchParams as unknown as string);
headers.set('isMobile', isMobile);
headers.set('isBot', isBot ? 'true' : 'false');
try {
let token: string | undefined = undefined;
if (authToken) {
token = authToken.value.replace(/ /g, '+');
} else {
try {
headers.set('authToken', btoa(Date.now().toString()));
headers.set('lang', lang);
} catch (e) {
console.error(`error in /auth/token endpoint: ${e}`, 'middleware');
}
}
const response = NextResponse.next({ request: { headers } });
response.cookies.set('debug-uuid', uuid, { maxAge: 3000 });
if (!authToken && token) {
response.cookies.set('authToken', token, { maxAge: 3000 });
}
if (!uuidCookie) {
response.cookies.set('ser_uuid', uuid, { maxAge: 3000 });
}
if (cpn) response.cookies.set('cpn', cpn);
response.headers.append(
'Access-Control-Allow-Origin',
'https://00301507-blue-green-env-test.preview.vercel-support.app'
);
return await handleDeployment(
request,
response,
headers,
uuid,
'nectarsleep-production-rc-configuration',
'rc-00301507.vercel-support.app'
);
} catch (e) {
console.error(`middleware error: ${e}`, 'middleware');
}
}
export const config = {
runtime: 'experimental-edge',
unstable_allowDynamic: [
'**/node_modules/lodash/*',
'**/node_modules/lodash.throttle/*',
'**/node_modules/@mui/utils/esm/**',
],
matcher: [
{
// source: '/((?!api|_next/static|_next/image|favicon.ico).*)',
source: '/((?!_next/static|_next/image|favicon.ico).*)',
},
],
};