Skip to content

Commit 9e7537c

Browse files
committed
fix: handle deprecated locale redirects in middleware to work on Netlify Edge
1 parent 79e067e commit 9e7537c

File tree

2 files changed

+34
-19
lines changed

2 files changed

+34
-19
lines changed

middleware.ts

Lines changed: 34 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,18 +3,44 @@ import createMiddleware from "next-intl/middleware"
33

44
import { routing } from "./src/i18n/routing"
55
import { DEFAULT_LOCALE } from "./src/lib/constants"
6+
import { getFirstSegment } from "./src/lib/utils/url"
67

78
const handleI18nRouting = createMiddleware(routing)
89

10+
// Locales that have been removed but may have external links pointing to them
11+
const DEPRECATED_LOCALES = new Set(["pcm", "fil", "ph"])
12+
13+
// Legacy locale codes that should redirect to their current equivalents
14+
const LOCALE_ALIASES: Record<string, string> = { no: "nb" }
15+
16+
function redirectTo(request: NextRequest, pathname: string, status: number) {
17+
const url = request.nextUrl.clone()
18+
url.pathname = pathname
19+
return NextResponse.redirect(url, status)
20+
}
21+
922
export default function middleware(request: NextRequest) {
10-
// Normalize to lowercase paths site-wide (URLs are case-insensitive by spec,
11-
// but our routes are defined in lowercase). Do this BEFORE i18n routing.
12-
const originalPath = request.nextUrl.pathname
13-
const lowerPath = originalPath.toLowerCase()
14-
if (originalPath !== lowerPath) {
15-
const url = request.nextUrl.clone()
16-
url.pathname = lowerPath
17-
return NextResponse.redirect(url, 301)
23+
const { pathname } = request.nextUrl
24+
25+
const lowerPath = pathname.toLowerCase()
26+
if (pathname !== lowerPath) {
27+
return redirectTo(request, lowerPath, 301)
28+
}
29+
30+
const firstSegment = getFirstSegment(lowerPath)
31+
32+
if (firstSegment && DEPRECATED_LOCALES.has(firstSegment)) {
33+
// Strip deprecated locale and redirect to default locale version
34+
const rest = lowerPath.slice(firstSegment.length + 1)
35+
const newPath = !rest ? "/" : rest
36+
return redirectTo(request, newPath, 302)
37+
}
38+
39+
if (firstSegment && firstSegment in LOCALE_ALIASES) {
40+
// Replace legacy locale code with current one
41+
const newLocale = LOCALE_ALIASES[firstSegment]
42+
const newPath = `/${newLocale}${lowerPath.slice(firstSegment.length + 1)}`
43+
return redirectTo(request, newPath, 301)
1844
}
1945

2046
// Handle i18n routing

next.config.js

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -166,17 +166,6 @@ module.exports = (phase, { defaultConfig }) => {
166166
...redirects.flatMap(([source, destination, permanent]) =>
167167
createRedirect(source, destination, permanent)
168168
),
169-
170-
// Custom locale aliases redirects
171-
{ source: "/no/:path*", destination: "/nb/:path*/", permanent: true },
172-
173-
// Deprecated locale redirects
174-
{ source: "/ph", destination: "/", permanent: true },
175-
{ source: "/ph/:path*", destination: "/:path*/", permanent: true },
176-
{ source: "/pcm", destination: "/", permanent: false },
177-
{ source: "/pcm/:path*", destination: "/:path*/", permanent: false },
178-
{ source: "/fil", destination: "/", permanent: false },
179-
{ source: "/fil/:path*", destination: "/:path*/", permanent: false },
180169
]
181170
},
182171
}

0 commit comments

Comments
 (0)