-
Notifications
You must be signed in to change notification settings - Fork 727
Expand file tree
/
Copy pathmiddleware.ts
More file actions
54 lines (44 loc) · 1.57 KB
/
Copy pathmiddleware.ts
File metadata and controls
54 lines (44 loc) · 1.57 KB
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
47
48
49
50
51
52
53
54
import { NextRequest, NextResponse } from "next/server";
import { BASE_PATH } from "./base-path.mjs";
const PUBLIC_FILE = /\.(.*)$/;
const locales = ["zh", "en"];
const defaultLocale = "zh";
function withoutBasePath(pathname: string): string {
if (!BASE_PATH || (pathname !== BASE_PATH && !pathname.startsWith(`${BASE_PATH}/`))) {
return pathname;
}
return pathname.slice(BASE_PATH.length) || "/";
}
export function middleware(req: NextRequest) {
const { pathname } = req.nextUrl;
const appPathname = withoutBasePath(pathname);
// Ignore static resources and API routes.
if (
appPathname.startsWith("/_next") ||
appPathname.startsWith("/api") ||
PUBLIC_FILE.test(appPathname)
) {
return;
}
const hasLocale = locales.some(
(locale) => appPathname === `/${locale}` || appPathname.startsWith(`/${locale}/`)
);
if (!hasLocale) {
let detectedLocale = defaultLocale;
const cookieLocale = req.cookies.get("NEXT_LOCALE")?.value;
if (cookieLocale && locales.includes(cookieLocale)) {
detectedLocale = cookieLocale;
} else {
const acceptLang = req.headers.get("accept-language");
if (acceptLang) {
const preferred = acceptLang.split(",")[0].toLowerCase();
if (preferred.startsWith("en")) detectedLocale = "en";
else if (preferred.startsWith("zh")) detectedLocale = "zh";
}
}
const url = req.nextUrl.clone();
const redirectPrefix = req.nextUrl.basePath ? "" : BASE_PATH;
url.pathname = `${redirectPrefix}/${detectedLocale}${appPathname}`;
return NextResponse.redirect(url);
}
}