Skip to content

Commit 7e0093f

Browse files
authoredOct 15, 2024··
Fix nextra/locales middleware, redirect to the docs URL relative to the /<basePath> (#3457)
* aa * aa * aa
1 parent 4eac7fb commit 7e0093f

File tree

2 files changed

+9
-1
lines changed

2 files changed

+9
-1
lines changed
 

‎.changeset/strange-coats-occur.md

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
'nextra-theme-docs': patch
3+
'nextra': patch
4+
---
5+
6+
Fix `nextra/locales` middleware, redirect to the docs URL relative to the `/<basePath>`.

‎packages/nextra/src/server/locales.ts

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { match as matchLocale } from '@formatjs/intl-localematcher'
22
import Negotiator from 'negotiator'
3+
import { addBasePath } from 'next/dist/client/add-base-path'
34
import { NextResponse } from 'next/server'
45
import type { NextRequest } from 'next/server'
56

@@ -35,9 +36,10 @@ export function middleware(request: NextRequest) {
3536
if (!pathnameHasLocale) {
3637
const locale = cookieLocale || getHeadersLocale(request)
3738

39+
const url = addBasePath(`/${locale}${pathname}`)
3840
// e.g. incoming request is /products
3941
// The new URL is now /en-US/products
40-
return NextResponse.redirect(new URL(`/${locale}${pathname}`, request.url))
42+
return NextResponse.redirect(new URL(url, request.url))
4143
}
4244

4345
const requestLocale = pathname.split('/', 2)[1]

0 commit comments

Comments
 (0)
Please sign in to comment.