-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmiddleware.ts
29 lines (20 loc) · 967 Bytes
/
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
import { NextResponse } from 'next/server'
import type { NextRequest } from 'next/server'
import Personalize from '@contentstack/personalize-edge-sdk';
export const config = {
matcher: [
'/((?!api|_next/static|_next/image|favicon.ico).*)',
],
}
export default async function middleware(request: NextRequest) {
const projectUid = process.env.NEXT_PUBLIC_CONTENTSTACK_P13N_PROJECT_ID as string;
const edgeApiUrl = process.env.NEXT_PUBLIC_CONTENTSTACK_REGION === 'EU' ? 'https://eu-personalize-edge.contentstack.com' : 'https://personalize-edge.contentstack.com'
Personalize.setEdgeApiUrl(edgeApiUrl);
await Personalize.init(projectUid, { request });
const variantParam = Personalize.getVariantParam();
const parsedUrl = new URL(request.url);
parsedUrl.searchParams.set(Personalize.VARIANT_QUERY_PARAM, variantParam);
const response = NextResponse.rewrite(parsedUrl);
await Personalize.addStateToResponse(response)
return response;
}