-
Notifications
You must be signed in to change notification settings - Fork 76
Expand file tree
/
Copy pathmiddleware.ts
More file actions
51 lines (42 loc) · 1.16 KB
/
middleware.ts
File metadata and controls
51 lines (42 loc) · 1.16 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
import { NextResponse } from "next/server"
import type { NextRequest } from "next/server"
// List of previous supported language
const LANGUAGE_CODES = ["en", "it", "de", "es", "fr", "ja", "ko"]
export function middleware(request: NextRequest) {
const { pathname } = request.nextUrl
if (LANGUAGE_CODES.includes(pathname.slice(1))) {
return NextResponse.redirect(new URL("/", request.url))
}
const languageCode = LANGUAGE_CODES.find((code) =>
pathname.startsWith(`/${code}/`)
)
if (languageCode) {
const newPathname = pathname.replace(`/${languageCode}`, "")
const newUrl = new URL(newPathname, request.url)
request.nextUrl.searchParams.forEach((value, key) => {
newUrl.searchParams.set(key, value)
})
return NextResponse.redirect(newUrl)
}
return NextResponse.next()
}
export const config = {
matcher: [
// Match exact language codes (e.g., /en, /fr)
"/en",
"/it",
"/de",
"/es",
"/fr",
"/ja",
"/ko",
// Match all paths that start with any of the language codes
"/en/:path*",
"/it/:path*",
"/de/:path*",
"/es/:path*",
"/fr/:path*",
"/ja/:path*",
"/ko/:path*",
],
}