-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathproxy.ts
More file actions
66 lines (55 loc) · 2.26 KB
/
proxy.ts
File metadata and controls
66 lines (55 loc) · 2.26 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
59
60
61
62
63
64
65
66
import { NextRequest, NextResponse } from "next/server";
import { i18n } from "./utils/translations/i18n-config";
export function proxy(request: NextRequest) {
const pathname = request.nextUrl.pathname;
// English is default: /en and /en/* should redirect to clean URLs (no /en)
if (pathname === "/en" || pathname === "/en/") {
return NextResponse.redirect(new URL("/", request.url), 308);
}
if (pathname.startsWith("/en/")) {
const pathWithoutEn = pathname.slice(4) || "/";
return NextResponse.redirect(new URL(pathWithoutEn, request.url), 308);
}
// Arabic: /ar and /ar/* - allow through (no change)
if (pathname.startsWith("/ar")) {
return NextResponse.next();
}
// No locale (e.g. /, /about, /thoughts)
const locale = getLocaleFromRequest(request);
// If user prefers Arabic, redirect to /ar/* so URL shows the locale
if (locale === "ar") {
const arPath = `/ar${pathname === "/" ? "" : pathname}`;
return NextResponse.redirect(new URL(arPath, request.url), 308);
}
// English (default): rewrite to /en/* internally, URL stays clean (no /en)
const internalPath = `/en${pathname === "/" ? "" : pathname}`;
return NextResponse.rewrite(new URL(internalPath, request.url));
}
// Define paths that don't need the locale prefix
export const config = {
matcher: ["/((?!api|_next/static|_next/image|images|favicons|favicon\\.ico|robots\\.txt|sitemap|llms\\.txt).*)"],
};
// Get locale preference from request (cookies or headers)
function getLocaleFromRequest(request: NextRequest): string {
// Check cookie first
const cookieLocale = request.cookies.get("NEXT_LOCALE")?.value;
if (cookieLocale && i18n.locales.includes(cookieLocale as any)) {
return cookieLocale;
}
// Then check accepted languages header
const acceptLanguage = request.headers.get("accept-language");
if (acceptLanguage) {
// Parse the accept-language header
const acceptedLanguages = acceptLanguage
.split(",")
.map((lang) => lang.split(";")[0].trim().substring(0, 2));
// Find the first accepted language that matches our supported locales
for (const lang of acceptedLanguages) {
if (i18n.locales.includes(lang as any)) {
return lang;
}
}
}
// Default to the default locale
return i18n.defaultLocale;
}