-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmiddleware.ts
45 lines (38 loc) · 1.18 KB
/
middleware.ts
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
import { type NextRequest, NextResponse } from "next/server";
export function middleware(request: NextRequest) {
const pathname = request.nextUrl.pathname;
if (
[
"/manifest.json",
"/favicon.ico",
// Your other files in `public`
].includes(pathname)
)
return;
// Check if there is any supported locale in the pathname
const pathnameIsMissingLocale = ["en", "he"].every(
(locale) =>
!pathname.startsWith(`/${locale}/`) && pathname !== `/${locale}`,
);
if (pathnameIsMissingLocale) {
// Redirect if there is no locale
const locale = "en";
// e.g. incoming request is /products
// The new URL is now /en-US/products
return NextResponse.redirect(
new URL(
`/${locale}${pathname.startsWith("/") ? "" : "/"}${pathname}`,
request.url,
),
);
}
const headers = new Headers(request.headers);
headers.set("x-current-path", request.nextUrl.pathname);
headers.set("Accept-CH", "Sec-CH-Prefers-Color-Scheme");
headers.set("Vary", "Sec-CH-Prefers-Color-Scheme");
headers.set("Critical-CH", "Sec-CH-Prefers-Color-Scheme");
return NextResponse.next({ headers });
}
export const config = {
matcher: ["/((?!api|icons|_next/static|_next/image|favicon.ico).*)"],
};