forked from ebowwar/simulation
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmiddleware.ts
40 lines (33 loc) · 1.34 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
import { NextRequest, NextResponse } from 'next/server'
import countries from '@/lib/countries.json'
import Cookies from 'js-cookie'
// run only on homepage
export const config = {
matcher: '/',
}
export async function middleware(req: NextRequest) {
const { nextUrl: url, geo } = req
const country = geo.country || 'US'
const city = geo.city || 'San Francisco'
const region = geo.region || 'CA'
const countryInfo = countries.find((x) => x.cca2 === country)
const currencyCode = Object.keys(countryInfo.currencies)[0]
const currency = countryInfo.currencies[currencyCode]
const languages = Object.values(countryInfo.languages).join(', ')
// Save the data as cookies
Cookies.set('country', country)
Cookies.set('city', city)
Cookies.set('region', region)
Cookies.set('currencyCode', currencyCode)
Cookies.set('currencySymbol', currency.symbol)
Cookies.set('currencyName', currency.name)
Cookies.set('languages', languages)
url.searchParams.set('country', country)
url.searchParams.set('city', city)
url.searchParams.set('region', region)
url.searchParams.set('currencyCode', currencyCode)
url.searchParams.set('currencySymbol', currency.symbol)
url.searchParams.set('name', currency.name)
url.searchParams.set('languages', languages)
return NextResponse.rewrite(url)
}