Unverified Commit 07723be8 authored by Sølve Tornøe's avatar Sølve Tornøe Committed by GitHub

Refactor default locale prefix to support clone() (#35874)



## Bug
fixes https://github.com/vercel/next.js/issues/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`
parent 9805399f
...@@ -170,24 +170,19 @@ import { NextRequest, NextResponse } from 'next/server' ...@@ -170,24 +170,19 @@ import { NextRequest, NextResponse } from 'next/server'
const PUBLIC_FILE = /\.(.*)$/ const PUBLIC_FILE = /\.(.*)$/
const stripDefaultLocale = (str: string): string => {
const stripped = str.replace('/default', '')
return stripped
}
export function middleware(request: NextRequest) { export function middleware(request: NextRequest) {
const shouldHandleLocale = const shouldHandleLocale =
!PUBLIC_FILE.test(request.nextUrl.pathname) && !PUBLIC_FILE.test(request.nextUrl.pathname) &&
!request.nextUrl.pathname.includes('/api/') && !request.nextUrl.pathname.includes('/api/') &&
request.nextUrl.locale === 'default' request.nextUrl.locale === 'default'
return shouldHandleLocale if (shouldHandleLocale) {
? NextResponse.redirect( const url = request.nextUrl.clone()
`/en${stripDefaultLocale(request.nextUrl.pathname)}${ url.pathname = `/en${request.nextUrl.pathname}`
request.nextUrl.search return NextResponse.redirect(url)
}` }
)
: undefined return undefined
} }
``` ```
......
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