Repository navigation
Expand file tree
/
Copy pathproxy.ts
More file actions
54 lines (46 loc) · 1.6 KB
/
Copy pathproxy.ts
File metadata and controls
54 lines (46 loc) · 1.6 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 { NextResponse, type NextRequest } from "next/server";
import {
DEFAULT_LOCALE,
localizePathname,
shouldBypassLocale,
splitLocaleFromPathname,
} from "@/lib/i18n";
const legacyRedirects: Record<string, string> = {
"/blog-as-service": "/technical-content-marketing",
"/docs-as-service": "/developer-documentation-dx-audit",
"/content-creation": "/developer-video-production",
"/devrel-as-service": "/developer-relations-growth-campaigns",
"/audit-services": "/developer-documentation-dx-audit",
"/video-production": "/developer-video-production",
"/product-launch": "/developer-relations-growth-campaigns",
"/organic-campaign": "/developer-relations-growth-campaigns",
"/influencer-management": "/developer-relations-growth-campaigns",
};
export function proxy(request: NextRequest) {
const { nextUrl } = request;
const { pathname } = nextUrl;
if (shouldBypassLocale(pathname)) {
return NextResponse.next();
}
const { locale, pathname: unprefixedPathname } =
splitLocaleFromPathname(pathname);
const activeLocale = locale ?? DEFAULT_LOCALE;
const redirectTarget = legacyRedirects[unprefixedPathname];
if (redirectTarget) {
const url = nextUrl.clone();
url.pathname = localizePathname(redirectTarget, activeLocale);
return NextResponse.redirect(url, 308);
}
if (locale) {
const response = NextResponse.next();
response.cookies.set("studio1-locale", locale, {
maxAge: 60 * 60 * 24 * 365,
sameSite: "lax",
});
return response;
}
return NextResponse.next();
}
export const config = {
matcher: ["/((?!api|_next|.*\\..*).*)"],
};