Skip to content

Commit 79e067e

Browse files
committed
handle deprecated locales and locale aliases in middleware
1 parent 78717eb commit 79e067e

File tree

2 files changed

+24
-10
lines changed

2 files changed

+24
-10
lines changed

next.config.js

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -162,21 +162,21 @@ module.exports = (phase, { defaultConfig }) => {
162162
}
163163

164164
return [
165+
// All primary redirects
166+
...redirects.flatMap(([source, destination, permanent]) =>
167+
createRedirect(source, destination, permanent)
168+
),
169+
165170
// Custom locale aliases redirects
166-
{ source: "/no/:path*", destination: "/nb/:path*", permanent: true },
171+
{ source: "/no/:path*", destination: "/nb/:path*/", permanent: true },
167172

168173
// Deprecated locale redirects
174+
{ source: "/ph", destination: "/", permanent: true },
175+
{ source: "/ph/:path*", destination: "/:path*/", permanent: true },
169176
{ source: "/pcm", destination: "/", permanent: false },
170-
{ source: "/pcm/:path*", destination: "/:path*", permanent: false },
177+
{ source: "/pcm/:path*", destination: "/:path*/", permanent: false },
171178
{ source: "/fil", destination: "/", permanent: false },
172-
{ source: "/fil/:path*", destination: "/:path*", permanent: false },
173-
{ source: "/ph", destination: "/", permanent: false },
174-
{ source: "/ph/:path*", destination: "/:path*", permanent: false },
175-
176-
// All primary redirects
177-
...redirects.flatMap(([source, destination, permanent]) =>
178-
createRedirect(source, destination, permanent)
179-
),
179+
{ source: "/fil/:path*", destination: "/:path*/", permanent: false },
180180
]
181181
},
182182
}

src/lib/utils/url.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,20 @@ export const normalizeSlug = (slug: string) => {
6161
return `/${slug.replace(/^\/+|\/+$/g, "")}`
6262
}
6363

64+
/**
65+
* Extracts the first path segment from a pathname.
66+
* Useful for extracting locale prefixes from URLs.
67+
*
68+
* @example
69+
* getFirstSegment("/en/about") // "en"
70+
* getFirstSegment("/about") // "about"
71+
* getFirstSegment("/") // ""
72+
*/
73+
export const getFirstSegment = (pathname: string): string => {
74+
const secondSlash = pathname.indexOf("/", 1)
75+
return secondSlash === -1 ? pathname.slice(1) : pathname.slice(1, secondSlash)
76+
}
77+
6478
/**
6579
* Converts a string to a URL-friendly slug
6680
* @param text - The text to convert (e.g., "Governance/DAO", "Bridge Aave 1", "Hello world")

0 commit comments

Comments
 (0)