-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmiddleware.ts
45 lines (35 loc) · 1.66 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
import { NextRequest, NextResponse } from 'next/server';
import Negotiator from 'negotiator';
import { i18nConfig } from './project.config';
export function middleware(request: NextRequest) {
const headers = Object.fromEntries(request.headers.entries());
const search = request.nextUrl.searchParams.toString();
const path = request.nextUrl.pathname + (search ? `?${search}` : '');
const pathMissingLocale = i18nConfig.locales.every((locale) => !path.startsWith(`/${locale}/`));
let locale: string;
let response: NextResponse;
if (pathMissingLocale) {
const storedLocale = request.cookies.get('locale')?.value;
const preferredLocale = new Negotiator({ headers }).language(i18nConfig.locales);
locale = [storedLocale, preferredLocale, i18nConfig.defaultLocale]
.filter(Boolean)
.filter((l) => i18nConfig.locales.includes(l as string))[0] as string;
response = NextResponse.redirect(new URL(`/${locale}${path}`, request.url));
} else {
locale = path.split('/')[1];
const containsMultipleLocaleOccurrences = new RegExp(`^/${locale}/.*(/${locale}/)`, 'g').test(path);
if (containsMultipleLocaleOccurrences) {
const correctedPath = path.replace(new RegExp(`^/${locale}/.*(/${locale}/)`), `/${locale}/`);
response = NextResponse.redirect(new URL(correctedPath, request.url));
} else {
response = NextResponse.next();
}
}
response.cookies.set('locale', locale, { maxAge: 60 * 60 * 24 * 7 * 4 * 12 * 100 }); // 100 years expiry
return response;
}
export const config = {
matcher: [
'/((?!api|_next|favicon|manifest|storybook|images|sb-assets|sitemap\\.xml|robots\\.txt|sw\\.js|workbox|icons).*)',
],
};