-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathmiddleware.ts
More file actions
61 lines (47 loc) · 1.57 KB
/
middleware.ts
File metadata and controls
61 lines (47 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
55
56
57
58
import { NextResponse, type NextRequest } from "next/server";
const COUNTRY_LOCALE_MAP: Record<string, string> = {
US: "en",
GB: "en",
CA: "en",
AU: "en",
FR: "fr",
DE: "en",
ES: "en",
PT: "en",
};
const DEFAULT_LOCALE = "en";
function getLocaleFromCountry(country?: string | null): string {
if (!country) return DEFAULT_LOCALE;
const upper = country.toUpperCase();
return COUNTRY_LOCALE_MAP[upper] || DEFAULT_LOCALE;
}
export function middleware(request: NextRequest) {
const { pathname } = request.nextUrl;
// Skip next internals and assets
if (
pathname.startsWith("/_next") ||
pathname.startsWith("/api") ||
pathname.startsWith("/static") ||
pathname.startsWith("/fonts") ||
pathname.startsWith("/images") ||
pathname.match(/\.(png|jpg|jpeg|gif|svg|ico|webp|css|js|map)$/)
) {
return NextResponse.next();
}
const pathSegments = pathname.split("/").filter(Boolean);
const hasLocale = pathSegments[0]?.length === 2; // e.g. /en/...
if (!hasLocale && pathname !== "/") {
const country = request.headers.get("x-vercel-ip-country") || request.cookies.get("locale")?.value;
const locale = getLocaleFromCountry(country);
const url = request.nextUrl.clone();
url.pathname = `/${locale}${pathname}` || `/${locale}`;
const response = NextResponse.redirect(url);
response.cookies.set("locale", locale, { path: "/" });
return response;
}
// If locale is present, let the request continue
return NextResponse.next();
}
export const config = {
matcher: ["/((?!_next|api|static|fonts|images).*)"],
};