-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathproxy.ts
More file actions
65 lines (53 loc) · 1.93 KB
/
Copy pathproxy.ts
File metadata and controls
65 lines (53 loc) · 1.93 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
import type { NextRequest } from "next/server";
import { NextResponse } from "next/server";
import { updateSession } from "@/shared/api/supabase/middleware";
import { routing } from "@/shared/i18n";
export async function proxy(request: NextRequest) {
const pathname = request.nextUrl.pathname;
if (pathname === "/api" || pathname.startsWith("/api/")) {
return updateSession(request, NextResponse.next());
}
const defaultLocalePrefix = `/${routing.defaultLocale}`;
const hasDefaultLocalePrefix =
pathname === defaultLocalePrefix ||
pathname.startsWith(`${defaultLocalePrefix}/`);
const internalLocale = request.headers.get("x-next-intl-locale");
if (hasDefaultLocalePrefix) {
if (internalLocale === routing.defaultLocale) {
return updateSession(request, NextResponse.next());
}
const url = request.nextUrl.clone();
const nextPathname = pathname.slice(defaultLocalePrefix.length) || "/";
url.pathname = nextPathname;
return NextResponse.redirect(url);
}
const prefixedLocale = routing.locales.find(
(locale) =>
locale !== routing.defaultLocale &&
(pathname === `/${locale}` || pathname.startsWith(`/${locale}/`)),
);
const locale = prefixedLocale ?? routing.defaultLocale;
const requestHeaders = new Headers(request.headers);
requestHeaders.set("x-next-intl-locale", locale);
if (locale === routing.defaultLocale) {
const url = request.nextUrl.clone();
url.pathname = pathname === "/" ? `/${locale}` : `/${locale}${pathname}`;
const response = NextResponse.rewrite(url, {
request: {
headers: requestHeaders,
},
});
return updateSession(request, response);
}
const response = NextResponse.next({
request: {
headers: requestHeaders,
},
});
return updateSession(request, response);
}
export const config = {
matcher: [
"/((?!_next/static|_next/image|favicon.ico|.*\\.(?:svg|png|jpg|jpeg|gif|webp)$).*)",
],
};