Skip to content

Commit

Permalink
Refactor default locale prefix to support clone() (#35874)
Browse files Browse the repository at this point in the history
## Bug
fixes #35273

- [x] Related issues linked using `fixes #number`
- [ ] Integration tests added
- [ ] Errors have helpful link attached, see `contributing.md`


## Documentation / Examples

- [ ] Make sure the linting passes by running `yarn lint`
  • Loading branch information
kamerat committed Apr 6, 2022
1 parent 9805399 commit 07723be
Showing 1 changed file with 7 additions and 12 deletions.
19 changes: 7 additions & 12 deletions docs/advanced-features/i18n-routing.md
Expand Up @@ -170,24 +170,19 @@ import { NextRequest, NextResponse } from 'next/server'

const PUBLIC_FILE = /\.(.*)$/

const stripDefaultLocale = (str: string): string => {
const stripped = str.replace('/default', '')
return stripped
}

export function middleware(request: NextRequest) {
const shouldHandleLocale =
!PUBLIC_FILE.test(request.nextUrl.pathname) &&
!request.nextUrl.pathname.includes('/api/') &&
request.nextUrl.locale === 'default'

return shouldHandleLocale
? NextResponse.redirect(
`/en${stripDefaultLocale(request.nextUrl.pathname)}${
request.nextUrl.search
}`
)
: undefined
if (shouldHandleLocale) {
const url = request.nextUrl.clone()
url.pathname = `/en${request.nextUrl.pathname}`
return NextResponse.redirect(url)
}

return undefined
}
```

Expand Down

0 comments on commit 07723be

Please sign in to comment.