-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmiddleware.ts
More file actions
22 lines (18 loc) · 753 Bytes
/
Copy pathmiddleware.ts
File metadata and controls
22 lines (18 loc) · 753 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
import { NextResponse, type NextRequest } from 'next/server';
import { localeCookie } from './lib/site';
/**
* Visitors in Japan land on /ja once, unless they have already chosen a
* language (the switch in the header writes the cookie). Everyone else gets
* the English page at "/". Same behaviour as the previous site.
*/
export function middleware(request: NextRequest) {
if (request.cookies.get(localeCookie)?.value) return NextResponse.next();
if (request.headers.get('x-vercel-ip-country') === 'JP') {
const url = request.nextUrl.clone();
url.pathname = '/ja';
return NextResponse.redirect(url, 307);
}
return NextResponse.next();
}
/** Only the bare root is ever redirected. */
export const config = { matcher: '/' };