Skip to content

Commit af37d6a

Browse files
authored
Merge pull request #12328 from ethereum/fix-redirect-trailing-slash
hot-fix: add trailing slash to redirects
2 parents 5d71fb9 + aae43c9 commit af37d6a

File tree

1 file changed

+10
-6
lines changed

1 file changed

+10
-6
lines changed

src/middleware.ts

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -33,14 +33,18 @@ export async function middleware(req: NextRequest) {
3333
}
3434

3535
if (req.nextUrl.locale === FAKE_LOCALE) {
36-
// Apparently `localeDetection` does not work when using the faked locale
37-
// hack. So, detect the locale manually
36+
// Apparently, the built-in `localeDetection`from Next does not work when
37+
// using the faked locale hack. So, we need to detect the locale manually
3838
const localeDetected = detectLocale(req.headers.get("accept-language"))
3939
const locale = localeDetected || DEFAULT_LOCALE
4040

41-
return NextResponse.redirect(
42-
new URL(`/${locale}${req.nextUrl.pathname}`, req.url),
43-
{ status: 301 }
44-
)
41+
const redirectUrl = new URL(`/${locale}${req.nextUrl.pathname}`, req.url)
42+
43+
// Add trailing slash if it's not present
44+
if (!redirectUrl.pathname.endsWith("/")) {
45+
redirectUrl.pathname = redirectUrl.pathname + "/"
46+
}
47+
48+
return NextResponse.redirect(redirectUrl, { status: 301 })
4549
}
4650
}

0 commit comments

Comments
 (0)