-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmiddleware.ts
46 lines (39 loc) · 1.25 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
46
import { match } from "@formatjs/intl-localematcher";
import Negotiator from "negotiator";
import { NextRequest } from "next/server";
let locales = ["en", "zh"];
let defaultLocale = "en";
function getLocale(request: NextRequest) {
let languages = new Negotiator({
headers: Object.fromEntries(request.headers.entries()),
})
.languages()
.filter((lang) => {
try {
// The type of `Intl.getCanonicalLocales` is not correct currently.
// @ts-ignore
Intl.getCanonicalLocales(lang);
return true;
} catch (e) {
return false;
}
});
return match(languages, locales, defaultLocale);
}
export function middleware(request: NextRequest) {
// Check if there is any supported locale in the pathname
const { pathname } = request.nextUrl;
const pathnameHasLocale = locales.some(
(locale) => pathname.startsWith(`/${locale}/`) || pathname === `/${locale}`,
);
if (pathnameHasLocale) return;
// Redirect if there is no locale
const locale = getLocale(request);
request.nextUrl.pathname = `/${locale}${pathname}`;
// e.g. incoming request is /products
// The new URL is now /en-US/products
return Response.redirect(request.nextUrl);
}
export const config = {
matcher: ["/", "/works"],
};