Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ensure locale is not duplicated from differing casing #24187

Merged
merged 2 commits into from
Apr 18, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 10 additions & 3 deletions packages/next/next-server/lib/router/router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -113,10 +113,13 @@ export function addLocale(
defaultLocale?: string
) {
if (process.env.__NEXT_I18N_SUPPORT) {
const pathLower = path.toLowerCase()
const localeLower = locale && locale.toLowerCase()

return locale &&
locale !== defaultLocale &&
!path.startsWith('/' + locale + '/') &&
path !== '/' + locale
!pathLower.startsWith('/' + localeLower + '/') &&
pathLower !== '/' + localeLower
? addPathPrefix(path, '/' + locale)
: path
}
Expand All @@ -126,8 +129,12 @@ export function addLocale(
export function delLocale(path: string, locale?: string) {
if (process.env.__NEXT_I18N_SUPPORT) {
const pathname = pathNoQueryHash(path)
const pathLower = pathname.toLowerCase()
const localeLower = locale && locale.toLowerCase()

return locale &&
(pathname.startsWith('/' + locale + '/') || pathname === '/' + locale)
(pathLower.startsWith('/' + localeLower + '/') ||
pathLower === '/' + localeLower)
? (pathname.length === locale.length + 1 ? '/' : '') +
path.substr(locale.length + 1)
: path
Expand Down
34 changes: 34 additions & 0 deletions test/integration/i18n-support/test/shared.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,40 @@ async function addDefaultLocaleCookie(browser) {
}

export function runTests(ctx) {
it('should handle navigating back to different casing of locale', async () => {
const browser = await webdriver(
ctx.appPort,
`${ctx.basePath || ''}/FR/links`
)

expect(await browser.eval(() => document.location.pathname)).toBe(
`${ctx.basePath || ''}/FR/links`
)
expect(await browser.elementByCss('#router-pathname').text()).toBe('/links')
expect(await browser.elementByCss('#router-locale').text()).toBe('fr')

await browser
.elementByCss('#to-another')
.click()
.waitForElementByCss('#another')

expect(await browser.eval(() => document.location.pathname)).toBe(
`${ctx.basePath || ''}/fr/another`
)
expect(await browser.elementByCss('#router-pathname').text()).toBe(
'/another'
)
expect(await browser.elementByCss('#router-locale').text()).toBe('fr')

await browser.back().waitForElementByCss('#links')

expect(await browser.eval(() => document.location.pathname)).toBe(
`${ctx.basePath || ''}/FR/links`
)
expect(await browser.elementByCss('#router-pathname').text()).toBe('/links')
expect(await browser.elementByCss('#router-locale').text()).toBe('fr')
})

it('should have correct initial query values for fallback', async () => {
const res = await fetchViaHTTP(
ctx.appPort,
Expand Down