From 614b39170ad109c0fe29974acebe29b47b3c827b Mon Sep 17 00:00:00 2001 From: JJ Kasper Date: Sun, 11 Oct 2020 20:38:56 -0500 Subject: [PATCH] Fix a couple i18n cases --- packages/next/client/index.tsx | 2 +- .../next/next-server/server/next-server.ts | 16 +++---- .../i18n-support/test/index.test.js | 43 ++++++++++++++++++- 3 files changed, 50 insertions(+), 11 deletions(-) diff --git a/packages/next/client/index.tsx b/packages/next/client/index.tsx index 22e926dd8c73df3..a7a4b069f7b2cf9 100644 --- a/packages/next/client/index.tsx +++ b/packages/next/client/index.tsx @@ -92,7 +92,7 @@ if (process.env.__NEXT_i18n_SUPPORT) { const localePathResult = normalizeLocalePath(asPath, locales) if (localePathResult.detectedLocale) { - asPath = asPath.substr(localePathResult.detectedLocale.length + 1) + asPath = asPath.substr(localePathResult.detectedLocale.length + 1) || '/' } else { // derive the default locale if it wasn't detected in the asPath // since we don't prerender static pages with all possible default diff --git a/packages/next/next-server/server/next-server.ts b/packages/next/next-server/server/next-server.ts index 1afeba5ce1f0f22..69a4b75f00f07a3 100644 --- a/packages/next/next-server/server/next-server.ts +++ b/packages/next/next-server/server/next-server.ts @@ -1150,6 +1150,13 @@ export default class Server { const isDataReq = !!query._nextDataReq && (isSSG || isServerProps) delete query._nextDataReq + const locale = query.__nextLocale as string + const locales = query.__nextLocales as string[] + // const defaultLocale = query.__nextDefaultLocale as string + delete query.__nextLocale + delete query.__nextLocales + // delete query.__nextDefaultLocale + let previewData: string | false | object | undefined let isPreviewMode = false @@ -1178,7 +1185,7 @@ export default class Server { } if (this.nextConfig.experimental.i18n) { - return normalizeLocalePath(path, this.renderOpts.locales).pathname + return normalizeLocalePath(path, locales).pathname } return path } @@ -1190,13 +1197,6 @@ export default class Server { urlPathname = stripNextDataPath(urlPathname) } - const locale = query.__nextLocale as string - const locales = query.__nextLocales as string[] - // const defaultLocale = query.__nextDefaultLocale as string - delete query.__nextLocale - delete query.__nextLocales - // delete query.__nextDefaultLocale - const ssgCacheKey = isPreviewMode || !isSSG ? undefined // Preview mode bypasses the cache diff --git a/test/integration/i18n-support/test/index.test.js b/test/integration/i18n-support/test/index.test.js index 429be47a832b4ad..f0cd92b4da62c87 100644 --- a/test/integration/i18n-support/test/index.test.js +++ b/test/integration/i18n-support/test/index.test.js @@ -26,7 +26,46 @@ let appPort const locales = ['en-US', 'nl-NL', 'nl-BE', 'nl', 'fr-BE', 'fr', 'en'] -function runTests() { +function runTests(isDev) { + it('should update asPath on the client correctly', async () => { + for (const check of ['en', 'En']) { + const browser = await webdriver(appPort, `/${check}`) + + expect(await browser.elementByCss('html').getAttribute('lang')).toBe('en') + expect(await browser.elementByCss('#router-locale').text()).toBe('en') + expect( + JSON.parse(await browser.elementByCss('#router-locales').text()) + ).toEqual(locales) + expect(await browser.elementByCss('#router-as-path').text()).toBe('/') + expect(await browser.elementByCss('#router-pathname').text()).toBe('/') + } + }) + + if (!isDev) { + it('should handle fallback correctly after generating', async () => { + const browser = await webdriver( + appPort, + '/en/gsp/fallback/hello-fallback' + ) + + // wait for the fallback to be generated/stored to ISR cache + browser.waitForElementByCss('#gsp') + + // now make sure we're serving the previously generated file from the cache + const html = await renderViaHTTP( + appPort, + '/en/gsp/fallback/hello-fallback' + ) + const $ = cheerio.load(html) + + expect($('#gsp').text()).toBe('gsp page') + expect($('#router-locale').text()).toBe('en') + expect(JSON.parse($('#router-locales').text())).toEqual(locales) + expect($('#router-pathname').text()).toBe('/gsp/fallback/[slug]') + expect($('#router-as-path').text()).toBe('/gsp/fallback/hello-fallback') + }) + } + it('should use correct default locale for locale domains', async () => { const res = await fetchViaHTTP(appPort, '/', undefined, { headers: { @@ -729,7 +768,7 @@ describe('i18n Support', () => { }) afterAll(() => killApp(app)) - runTests() + runTests(true) }) describe('production mode', () => {