-
Notifications
You must be signed in to change notification settings - Fork 0
/
middleware.ts
42 lines (36 loc) · 1.27 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
import { cookieName, fallbackLng, languages } from '@/i18n/settings';
import acceptLanguage from 'accept-language';
import { NextRequest, NextResponse } from 'next/server';
acceptLanguage.languages(languages);
export const config = {
matcher: ['/((?!api|_next/static|_next/image|assets|favicon.ico|sw.js).*)'],
};
export function middleware(req: NextRequest) {
let lng;
if (req.cookies.has(cookieName)) {
lng = acceptLanguage.get(req.cookies.get(cookieName)?.value ?? '');
}
if (!lng) lng = acceptLanguage.get(req.headers.get('Accept-Language'));
if (!lng) lng = fallbackLng;
if (
!languages.some((loc) => req.nextUrl.pathname.startsWith(`/${loc}`)) &&
!req.nextUrl.pathname.startsWith('/_next')
) {
return NextResponse.redirect(
new URL(`/${lng}${req.nextUrl.pathname}`, req.url)
);
}
if (req.headers.has('referer')) {
const refererHeaderValue = req.headers.get('referer');
if (refererHeaderValue) {
const refererUrl = new URL(refererHeaderValue);
const lngInReferer = languages.find((l) =>
refererUrl.pathname.startsWith(`/${l}`)
);
const response = NextResponse.next();
if (lngInReferer) response.cookies.set(cookieName, lngInReferer);
return response;
}
}
return NextResponse.next();
}