From deda4a2090683953a6eccd773bb6aea8b25bec78 Mon Sep 17 00:00:00 2001 From: Vordgi Date: Wed, 10 Nov 2021 00:04:00 +0200 Subject: [PATCH 1/3] bugfix/i18n Do not support the second locale in the pathname --- packages/next/server/router.ts | 39 ++++++---------------------------- 1 file changed, 7 insertions(+), 32 deletions(-) diff --git a/packages/next/server/router.ts b/packages/next/server/router.ts index fb0176c03274139..b435eb7589e60cb 100644 --- a/packages/next/server/router.ts +++ b/packages/next/server/router.ts @@ -280,7 +280,6 @@ export default class Router { const isMiddlewareCatchall = testRoute.name === 'middleware catchall' const keepBasePath = isCustomRoute || isPublicFolderCatchall || isMiddlewareCatchall - const keepLocale = isCustomRoute const currentPathnameNoBasePath = replaceBasePath( this.basePath, @@ -289,39 +288,15 @@ export default class Router { if (!keepBasePath) { currentPathname = currentPathnameNoBasePath + } else if (getRequestMeta(req, '_nextHadBasePath')) { + currentPathname = `${this.basePath}${currentPathname}` } - const localePathResult = normalizeLocalePath( - currentPathnameNoBasePath, - this.locales - ) - const activeBasePath = keepBasePath ? this.basePath : '' - - if (keepLocale) { - if ( - !testRoute.internal && - parsedUrl.query.__nextLocale && - !localePathResult.detectedLocale - ) { - currentPathname = `${activeBasePath}/${parsedUrl.query.__nextLocale}${ - currentPathnameNoBasePath === '/' ? '' : currentPathnameNoBasePath - }` - } - - if ( - getRequestMeta(req, '__nextHadTrailingSlash') && - !currentPathname.endsWith('/') - ) { - currentPathname += '/' - } - } else { - currentPathname = `${ - getRequestMeta(req, '_nextHadBasePath') ? activeBasePath : '' - }${ - activeBasePath && localePathResult.pathname === '/' - ? '' - : localePathResult.pathname - }` + if ( + getRequestMeta(req, '__nextHadTrailingSlash') && + !currentPathname.endsWith('/') + ) { + currentPathname += '/' } let newParams = testRoute.match(currentPathname) From b2b0419e173bb85f39f7a338944bf203289ff9e5 Mon Sep 17 00:00:00 2001 From: Vordgi Date: Wed, 10 Nov 2021 02:11:22 +0200 Subject: [PATCH 2/3] bugfix/i18n Do not support the second locale in the pathname --- packages/next/server/router.ts | 39 ++++++++++++++++++++++++++++------ 1 file changed, 32 insertions(+), 7 deletions(-) diff --git a/packages/next/server/router.ts b/packages/next/server/router.ts index b435eb7589e60cb..a9a9f6e4fb710d7 100644 --- a/packages/next/server/router.ts +++ b/packages/next/server/router.ts @@ -280,6 +280,7 @@ export default class Router { const isMiddlewareCatchall = testRoute.name === 'middleware catchall' const keepBasePath = isCustomRoute || isPublicFolderCatchall || isMiddlewareCatchall + const keepLocale = isCustomRoute const currentPathnameNoBasePath = replaceBasePath( this.basePath, @@ -288,15 +289,39 @@ export default class Router { if (!keepBasePath) { currentPathname = currentPathnameNoBasePath - } else if (getRequestMeta(req, '_nextHadBasePath')) { - currentPathname = `${this.basePath}${currentPathname}` } - if ( - getRequestMeta(req, '__nextHadTrailingSlash') && - !currentPathname.endsWith('/') - ) { - currentPathname += '/' + const localePathResult = normalizeLocalePath( + currentPathnameNoBasePath, + this.locales + ) + const activeBasePath = keepBasePath ? this.basePath : '' + + if (keepLocale) { + if ( + !testRoute.internal && + parsedUrl.query.__nextLocale && + !localePathResult.detectedLocale + ) { + currentPathname = `${activeBasePath}/${parsedUrl.query.__nextLocale}${ + currentPathnameNoBasePath === '/' ? '' : currentPathnameNoBasePath + }` + } + + if ( + getRequestMeta(req, '__nextHadTrailingSlash') && + !currentPathname.endsWith('/') + ) { + currentPathname += '/' + } + } else { + currentPathname = `${ + getRequestMeta(req, '_nextHadBasePath') ? activeBasePath : '' + }${ + activeBasePath && currentPathnameNoBasePath === '/' + ? '' + : currentPathnameNoBasePath + }` } let newParams = testRoute.match(currentPathname) From 9388ca2bebc815ba58ecd435af9d1787520751d3 Mon Sep 17 00:00:00 2001 From: Vordgi Date: Wed, 10 Nov 2021 20:10:23 +0200 Subject: [PATCH 3/3] bugfix/i18n add test for repeating locales --- .../i18n-support/test/index.test.js | 31 +++++++++++++++++++ 1 file changed, 31 insertions(+) diff --git a/test/integration/i18n-support/test/index.test.js b/test/integration/i18n-support/test/index.test.js index 7d8143b79a0900f..8212a6bb0827e46 100644 --- a/test/integration/i18n-support/test/index.test.js +++ b/test/integration/i18n-support/test/index.test.js @@ -412,6 +412,37 @@ describe('i18n Support', () => { } }) + it('should return 404 error for repeating locales', async () => { + const defaultLocale = 'en-US' + for (const locale of nonDomainLocales) { + for (const asPath of [ + '/gsp/fallback/always/', + '/post/comment/', + '/gssp/first/', + ]) { + const res = await fetchViaHTTP( + curCtx.appPort, + `/${locale}/${defaultLocale}${asPath}`, + undefined, + { + redirect: 'manual', + } + ) + expect(res.status).toBe(404) + const $ = cheerio.load(await res.text()) + const props = JSON.parse($('#props').text()) + console.log(props) + expect($('#not-found').text().length > 0).toBe(true) + expect(props).toEqual({ + is404: true, + locale, + locales, + defaultLocale, + }) + } + } + }) + it('should navigate between pages correctly', async () => { for (const locale of nonDomainLocales) { const localePath = `/${locale !== 'en-US' ? `${locale}/` : ''}`