Unverified Commit fa519f63 authored by realazy's avatar realazy Committed by GitHub

fix some issues on examples/app-dir-i18n-routing (#51576)

parent 1033a03d
......@@ -15,7 +15,11 @@ function getLocale(request: NextRequest): string | undefined {
let languages = new Negotiator({ headers: negotiatorHeaders }).languages()
// @ts-ignore locales are readonly
const locales: string[] = i18n.locales
return matchLocale(languages, locales, i18n.defaultLocale)
try {
return matchLocale(languages, locales, i18n.defaultLocale)
} catch (_e) {
return i18n.defaultLocale
}
}
export function middleware(request: NextRequest) {
......@@ -43,7 +47,12 @@ export function middleware(request: NextRequest) {
// e.g. incoming request is /products
// The new URL is now /en-US/products
return NextResponse.redirect(new URL(`/${locale}/${pathname}`, request.url))
return NextResponse.redirect(
new URL(
`/${locale}${pathname.startsWith('/') ? '' : '/'}${pathname}`,
request.url
)
)
}
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment