-
Notifications
You must be signed in to change notification settings - Fork 5
/
middleware.ts
36 lines (32 loc) · 1.13 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
import { NextRequest, NextResponse } from "next/server";
const PUBLIC_FILE = /\.(.*)$/;
const ALLOWED_LOCALES = ["en", "cs", "de", "it", "es", "fr", "pt-BR"];
export async function middleware(
req: NextRequest
): Promise<NextResponse | undefined> {
if (
req.nextUrl.pathname.startsWith("/_next") ||
req.nextUrl.pathname.includes("/api/") ||
PUBLIC_FILE.test(req.nextUrl.pathname)
) {
return;
}
const localeParam = req.nextUrl.searchParams.get("locale");
const localeTestRegex = new RegExp("&?locale=" + localeParam); //looks for locale as a query param (optionally preceded by &)
const queryString = req.nextUrl.search.replace(localeTestRegex, "");
// locale is removed from query parameters and user is redirected if the locale is supported
if (localeParam) {
if (ALLOWED_LOCALES.includes(localeParam)) {
return NextResponse.redirect(
new URL(`/${localeParam}${req.nextUrl.pathname}${queryString}`, req.url)
);
} else {
return NextResponse.redirect(
new URL(
`/${req.nextUrl.locale}${req.nextUrl.pathname}${queryString}`,
req.url
)
);
}
}
}