From f0b1d6d46c554f9bf6b80bfd3031b06fe0b07e21 Mon Sep 17 00:00:00 2001 From: JJ Kasper Date: Thu, 19 Nov 2020 18:54:40 -0600 Subject: [PATCH] Update default locale handling with locale: false --- .../next/next-server/lib/router/router.ts | 9 +++-- .../pages/locale-false.js | 8 ++++ .../i18n-support/pages/locale-false.js | 8 ++++ test/integration/i18n-support/test/shared.js | 39 +++++++++++++++++++ 4 files changed, 60 insertions(+), 4 deletions(-) diff --git a/packages/next/next-server/lib/router/router.ts b/packages/next/next-server/lib/router/router.ts index 43c16506f26f7..fac214e74ffe0 100644 --- a/packages/next/next-server/lib/router/router.ts +++ b/packages/next/next-server/lib/router/router.ts @@ -616,7 +616,10 @@ export default class Router implements BaseRouter { let localeChange = options.locale !== this.locale if (process.env.__NEXT_I18N_SUPPORT) { - this.locale = options.locale || this.locale + this.locale = + options.locale === false + ? this.defaultLocale + : options.locale || this.locale if (typeof options.locale === 'undefined') { options.locale = this.locale @@ -627,12 +630,10 @@ export default class Router implements BaseRouter { } = require('../i18n/normalize-locale-path') as typeof import('../i18n/normalize-locale-path') const parsedAs = parseRelativeUrl(hasBasePath(as) ? delBasePath(as) : as) - const localePathResult = normalizeLocalePath( parsedAs.pathname, this.locales ) - if (localePathResult.detectedLocale) { this.locale = localePathResult.detectedLocale url = addBasePath(localePathResult.pathname) @@ -1228,7 +1229,7 @@ export default class Router implements BaseRouter { this.locales ) parsedAs.pathname = localePathResult.pathname - options.locale = localePathResult.detectedLocale || options.locale + options.locale = localePathResult.detectedLocale || this.defaultLocale asPath = formatWithValidation(parsedAs) } } diff --git a/test/integration/i18n-support-base-path/pages/locale-false.js b/test/integration/i18n-support-base-path/pages/locale-false.js index 8c1f6186861b7..e0de4c056aeea 100644 --- a/test/integration/i18n-support-base-path/pages/locale-false.js +++ b/test/integration/i18n-support-base-path/pages/locale-false.js @@ -42,6 +42,14 @@ export default function Page(props) { to /gssp/first
+ + to /gssp/first (default locale) + +
+ + to /gsp (default locale) + +
) } diff --git a/test/integration/i18n-support/pages/locale-false.js b/test/integration/i18n-support/pages/locale-false.js index 8c1f6186861b7..e0de4c056aeea 100644 --- a/test/integration/i18n-support/pages/locale-false.js +++ b/test/integration/i18n-support/pages/locale-false.js @@ -42,6 +42,14 @@ export default function Page(props) { to /gssp/first
+ + to /gssp/first (default locale) + +
+ + to /gsp (default locale) + +
) } diff --git a/test/integration/i18n-support/test/shared.js b/test/integration/i18n-support/test/shared.js index 0133bf52863eb..96d4982a43afd 100644 --- a/test/integration/i18n-support/test/shared.js +++ b/test/integration/i18n-support/test/shared.js @@ -27,6 +27,44 @@ async function addDefaultLocaleCookie(browser) { } export function runTests(ctx) { + it('should use default locale when no locale is in href with locale false', async () => { + const browser = await webdriver( + ctx.appPort, + `${ctx.basePath}/nl/locale-false?nextLocale=fr` + ) + + await browser.eval('window.beforeNav = 1') + + if (!ctx.isDev) { + await browser.eval(`(function() { + document.querySelector('#to-gssp-slug-default').scrollIntoView() + document.querySelector('#to-gsp-default').scrollIntoView() + })()`) + + await check(async () => { + const hrefs = await browser.eval(`Object.keys(window.next.router.sdc)`) + hrefs.sort() + + assert.deepEqual( + hrefs.map((href) => + new URL(href).pathname + .replace(ctx.basePath, '') + .replace(/^\/_next\/data\/[^/]+/, '') + ), + [ + '/en-US/gsp.json', + '/fr/gsp.json', + '/fr/gsp/fallback/first.json', + '/fr/gsp/fallback/hello.json', + ] + ) + return 'yes' + }, 'yes') + } + + expect(await browser.eval('window.beforeNav')).toBe(1) + }) + if (ctx.isDev) { it('should show error for redirect and notFound returned at same time', async () => { const html = await renderViaHTTP( @@ -715,6 +753,7 @@ export function runTests(ctx) { .replace(/^\/_next\/data\/[^/]+/, '') ), [ + '/en-US/gsp.json', '/fr/gsp.json', '/fr/gsp/fallback/first.json', '/fr/gsp/fallback/hello.json',