-
Notifications
You must be signed in to change notification settings - Fork 0
/
middleware.ts
36 lines (29 loc) · 971 Bytes
/
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
import { NextResponse } from "next/server";
import type { NextRequest } from "next/server";
import { getLocale } from "./lib/getLocale";
const locales = ["en", "id"];
export async function middleware(request: NextRequest) {
const pathname = request.nextUrl.pathname;
// Handle i18n
const pathnameIsMissingLocale = locales.every(
(locale) => !pathname.startsWith(`/${locale}/`) && pathname !== `/${locale}`
);
// Redirect if there is no locale
if (pathnameIsMissingLocale) {
const locale = getLocale(request);
// e.g. incoming request is /products
// The new URL is now /en-US/products
return NextResponse.redirect(
new URL(`/${locale}/${pathname}`, request.url)
);
}
}
// See "Matching Paths" below to learn more
export const config = {
matcher: [
// Skip all internal paths (_next)
"/((?!api|_next/|favicon.ico|favicon-|assets|images/|studio).*)",
// Optional: only run on root (/) URL
// '/'
],
};