-
Notifications
You must be signed in to change notification settings - Fork 0
/
middleware.ts
23 lines (19 loc) · 752 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
import { NextRequest, NextResponse } from 'next/server'
const PUBLIC_FILE = /\.(.*)$/
export async function middleware(req: NextRequest) {
const country = req!.geo!.country?.toLowerCase() || 'ma'
const countryIso3 = await fetch('http:https://country.io/iso3.json')
.then((res) => res.json())
.then((data) => data[country.toUpperCase()])
// no changes on routes that are public files
if (!PUBLIC_FILE.test(req.nextUrl.pathname)) {
// send all routes to the home page
if (req.nextUrl.pathname !== '/') {
const url = req.nextUrl.clone()
url.pathname = '/'
return NextResponse.redirect(url)
}
req.nextUrl.pathname = `/${countryIso3}`
return NextResponse.rewrite(req.nextUrl)
}
}