-
Notifications
You must be signed in to change notification settings - Fork 0
/
middleware.ts
66 lines (54 loc) · 1.54 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
import { NextRequest, NextResponse } from 'next/server'
import { choose } from '@lib/ab-testing'
import { MARKETING_BUCKETS } from '@lib/buckets'
type Route = {
page: string
cookie: string
buckets: readonly string[]
}
const ROUTES: Record<string, Route | undefined> = {
'/abtesting': {
page: '/abtesting',
cookie: 'bucket-marketing',
buckets: MARKETING_BUCKETS,
},
}
export const config = {
matcher: ['/home', '/abtesting'],
}
export default async function middleware(req: NextRequest) {
const { pathname } = req.nextUrl
const route = ROUTES[pathname]
const apiKey = process.env.NEXT_PUBLIC_DY_API_KEY || '';
const selectors = [`${process.env.NEXT_PUBLIC_SELECTOR}`] || [''];
if (!route) return
// Get the bucket from the cookie
let bucket = req.cookies.get(route.cookie)?.value
let hasBucket = !!bucket
const dyContext = {
page: {
location: req.url,
referrer: req.referrer,
data: [],
type: 'HOMEPAGE'
},
device: {
userAgent: req.headers.get('user-agent') || '',
ip: req.ip,
},
}
if (!bucket || !route.buckets.includes(bucket as any)) {
bucket = await choose(apiKey, dyContext, selectors)
hasBucket = false
}
// Create a rewrite to the page matching th bucket
const url = req.nextUrl.clone()
url.pathname = `${route.page}/${bucket}`
const res = NextResponse.rewrite(url)
// Add the bucket to the response cookies if it's not there
// or if its value was invalid
if (!hasBucket) {
res.cookies.set(route.cookie, bucket)
}
return res
}