-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathproxy.ts
More file actions
24 lines (19 loc) · 860 Bytes
/
Copy pathproxy.ts
File metadata and controls
24 lines (19 loc) · 860 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
import { NextResponse, type NextRequest } from "next/server";
import { isLocale, preferredLocale } from "@/lib/i18n";
import { clientIp, isRateLimited } from "@/lib/rate-limit";
export function proxy(request: NextRequest) {
const { pathname } = request.nextUrl;
if (pathname.startsWith("/api/")) {
return isRateLimited(clientIp(request.headers), pathname)
? NextResponse.json({ error: "Too many requests" }, { status: 429 })
: NextResponse.next();
}
const [, locale] = pathname.split("/");
if (isLocale(locale)) return NextResponse.next();
const url = request.nextUrl.clone();
url.pathname = `/${preferredLocale(request.headers.get("accept-language"))}${url.pathname}`;
return NextResponse.redirect(url);
}
export const config = {
matcher: ["/api/:path*", "/((?!api|_next/static|_next/image|favicon.ico|.*\\..*).*)"],
};