Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
support
nextConfig.basePath
with i18n + tests (#1123)
- Loading branch information
1 parent
f35e724
commit cc1cb5f
Showing
5 changed files
with
171 additions
and
107 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
'nextra': patch | ||
--- | ||
|
||
support `nextConfig.basePath` with i18n |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
// Next.js' `addBasePath`, `hasBasePath` and `removeBasePath` functions depend | ||
// on `__NEXT_ROUTER_BASEPATH` environment variable that must be set before | ||
// importing middleware | ||
process.env.__NEXT_ROUTER_BASEPATH = '/testBasePath' | ||
import { locales as originalLocales } from '../src/locales' | ||
import { describe, expect, it, vi } from 'vitest' | ||
import { NextRequest } from 'next/server' | ||
|
||
vi.mock('next/server', async () => { | ||
const mod = await vi.importActual<typeof import('next/server')>('next/server') | ||
return { | ||
...mod, | ||
NextResponse: { | ||
redirect: (url: string) => ({ url, type: 'redirect' }), | ||
rewrite: (url: string) => ({ url, type: 'rewrite' }) | ||
} | ||
} | ||
}) | ||
|
||
const locales = originalLocales as unknown as (req: NextRequest) => { | ||
type: string | ||
url: URL | ||
} | ||
|
||
const createRequest = (url: string, localeCookie = '') => { | ||
return new NextRequest(url, { | ||
headers: { cookie: localeCookie }, | ||
nextConfig: { | ||
i18n: { | ||
locales: ['en-US', 'zh-CN'], | ||
defaultLocale: 'en-US' | ||
} | ||
} | ||
}) | ||
} | ||
|
||
/** | ||
* @vitest-environment edge-runtime | ||
*/ | ||
describe('basePath', () => { | ||
// Next.js' `addLocale` and `removeLocale` functions depend on `__NEXT_I18N_SUPPORT` | ||
process.env.__NEXT_I18N_SUPPORT = 'true' | ||
|
||
it('should rewrite basePath', () => { | ||
const request = createRequest( | ||
'http://localhost:3000/zh-CN/docs/getting-started' | ||
) | ||
const result = locales(request) | ||
expect(result.type).toBe('rewrite') | ||
expect(result.url.href).toBe( | ||
'http://localhost:3000/testBasePath/zh-CN/docs/getting-started.zh-CN' | ||
) | ||
}) | ||
|
||
it('should redirect with basePath', () => { | ||
const request = createRequest('http://localhost:3000', 'NEXT_LOCALE=zh-CN') | ||
const result = locales(request) | ||
expect(result.type).toBe('redirect') | ||
expect(result.url.href).toBe('http://localhost:3000/testBasePath/zh-CN') | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
cc1cb5f
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Successfully deployed to the following URLs:
nextra-v2 – ./docs
nextra.vercel.app
nextra-v2-shuding1.vercel.app
nextra.site
nextra-v2-git-main-shuding1.vercel.app
www.nextra.site
nextra-v2.vercel.app