-
Notifications
You must be signed in to change notification settings - Fork 0
/
middleware.ts
38 lines (32 loc) · 971 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
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
import { NextResponse } from 'next/server';
import { match } from '@formatjs/intl-localematcher';
import Negotiator from 'negotiator';
let locales = ['ru', 'en'];
function getLocale(request) {
let languages = new Negotiator(request).languages(locales);
let defaultLocale = 'ru';
return match(languages, locales, defaultLocale);
}
export function middleware(request) {
const pathname = request.nextUrl.pathname;
const pathnameIsMissingLocale = locales.every(
(locale) => !pathname.startsWith(`/${locale}/`) && pathname !== `/${locale}`,
);
const locale = getLocale(request);
if (
(pathname === '/' && locale !== 'ru') ||
(pathname !== '/' && pathnameIsMissingLocale)
) {
return NextResponse.redirect(
new URL(`/${locale}${pathname}`, request.url),
);
}
}
export const config = {
matcher: [
// Skip all internal paths (_next)
'/((?!_next|static).*)',
// Optional: only run on root (/) URL
// '/'
],
};