Skip to content

Commit

Permalink
Merge pull request #14564 from ethereum/fix-double-locale-urls
Browse files Browse the repository at this point in the history
Fix double locale urls
  • Loading branch information
corwintines authored Jan 8, 2025
2 parents a86e174 + 244ab48 commit 1cb5a1b
Showing 1 changed file with 14 additions and 0 deletions.
14 changes: 14 additions & 0 deletions src/middleware.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ export const config = {
],
}

const doubleLocaleRegex = new RegExp(`^/(${LOCALES_CODES.join("|")})/.*`, "i")

// Middleware required to always display the locale prefix in the URL. It
// redirects to the default locale if the locale is not present in the URL
export async function middleware(req: NextRequest) {
Expand All @@ -45,6 +47,18 @@ export async function middleware(req: NextRequest) {
return
}

/**
* If an URL has double langs in the URL it leads to 500 error,
* e.g.: /ja/en/eth/
*
* It is a known bug:
* https://github.com/vercel/next.js/issues/52314
* https://github.com/vercel/next.js/issues/52316
*/
if (doubleLocaleRegex.test(pathname)) {
return NextResponse.redirect(new URL(`/${DEFAULT_LOCALE}/404`, req.url))
}

if (locale === FAKE_LOCALE) {
// Apparently, the built-in `localeDetection`from Next does not work when
// using the faked locale hack. So, we need to detect the locale manually
Expand Down

0 comments on commit 1cb5a1b

Please sign in to comment.