Skip to content

Commit

Permalink
fix some issues on examples/app-dir-i18n-routing (#51576)
Browse files Browse the repository at this point in the history
  • Loading branch information
cxa committed Jun 24, 2023
1 parent 1033a03 commit fa519f6
Showing 1 changed file with 11 additions and 2 deletions.
13 changes: 11 additions & 2 deletions examples/app-dir-i18n-routing/middleware.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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
)
)
}
}

Expand Down

0 comments on commit fa519f6

Please sign in to comment.