-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmiddleware.ts
More file actions
57 lines (46 loc) · 1.7 KB
/
Copy pathmiddleware.ts
File metadata and controls
57 lines (46 loc) · 1.7 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
55
56
57
import { NextRequest, NextResponse } from "next/server";
import { locales, defaultLocale } from "@/lib/i18n";
function getPreferredLocale(request: NextRequest): string {
const acceptLanguage = request.headers.get("accept-language");
if (!acceptLanguage) return defaultLocale;
const preferred = acceptLanguage
.split(",")
.map((lang) => {
const [code, q] = lang.trim().split(";q=");
return { code: code.split("-")[0].toLowerCase(), q: q ? parseFloat(q) : 1 };
})
.sort((a, b) => b.q - a.q);
for (const { code } of preferred) {
const match = locales.find((l) => l === code);
if (match) return match;
}
return defaultLocale;
}
export function middleware(request: NextRequest) {
const { pathname } = request.nextUrl;
// Redirect www to non-www (canonical domain)
const host = request.headers.get("host") || "";
if (host.startsWith("www.")) {
const url = request.nextUrl.clone();
url.host = host.replace(/^www\./, "");
url.port = "";
return NextResponse.redirect(url, 301);
}
// Check if the pathname already has a locale prefix
const pathnameHasLocale = locales.some(
(locale) =>
pathname.startsWith(`/${locale}/`) || pathname === `/${locale}`
);
if (pathnameHasLocale) return;
// Detect preferred locale from Accept-Language header
const locale = getPreferredLocale(request);
const url = request.nextUrl.clone();
url.pathname = `/${locale}${pathname}`;
return NextResponse.redirect(url);
}
export const config = {
matcher: [
// Match all paths except static files and Next.js internals
"/((?!_next|favicon\\.ico|favicon|apple-touch-icon|site\\.webmanifest|robots\\.txt|sitemap\\.xml|og-image|llms\\.txt|.*\\.(?:png|jpg|jpeg|gif|svg|ico|webp|xml|txt|webmanifest)).*)",
],
};