From d0e80fe61029f39ed3691b219cfcda70aad1e8d6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jakub=20Peka=C5=99?= Date: Tue, 10 May 2022 13:57:55 +0200 Subject: [PATCH 1/4] Fix SSG Link generation if using domain locales --- packages/next/export/worker.ts | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/packages/next/export/worker.ts b/packages/next/export/worker.ts index 08c3c14fcacd..6548fbf05137 100644 --- a/packages/next/export/worker.ts +++ b/packages/next/export/worker.ts @@ -2,7 +2,7 @@ import type { ComponentType } from 'react' import type { FontManifest } from '../server/font-utils' import type { GetStaticProps } from '../types' import type { IncomingMessage, ServerResponse } from 'http' -import type { NextConfigComplete } from '../server/config-shared' +import type { DomainLocale, NextConfigComplete } from '../server/config-shared' import type { NextParsedUrlQuery } from '../server/request-meta' import url from 'url' import { extname, join, dirname, sep } from 'path' @@ -23,6 +23,7 @@ import { isInAmpMode } from '../shared/lib/amp-mode' import { setHttpAgentOptions } from '../server/config' import RenderResult from '../server/render-result' import isError from '../lib/is-error' +import { addRequestMeta } from '../server/request-meta' const envConfig = require('../shared/lib/runtime-config') @@ -84,6 +85,7 @@ interface RenderOpts { locales?: string[] locale?: string defaultLocale?: string + domainLocales?: DomainLocale[] trailingSlash?: boolean appDir?: boolean } @@ -208,6 +210,10 @@ export default async function exportPage({ req.url += '/' } + if (buildExport && renderOpts.domainLocales) { + addRequestMeta(req, '__nextIsLocaleDomain', true) + } + envConfig.setConfig({ serverRuntimeConfig, publicRuntimeConfig: renderOpts.runtimeConfig, From 99c59b0c0e0d9dfaa863b2df997da2dc5b339456 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jakub=20Peka=C5=99?= Date: Tue, 10 May 2022 20:48:41 +0200 Subject: [PATCH 2/4] Updated condition and test --- packages/next/export/worker.ts | 6 ++- test/integration/i18n-support/test/shared.js | 43 ++++++++++++++++++++ 2 files changed, 48 insertions(+), 1 deletion(-) diff --git a/packages/next/export/worker.ts b/packages/next/export/worker.ts index 6548fbf05137..0d40d786df95 100644 --- a/packages/next/export/worker.ts +++ b/packages/next/export/worker.ts @@ -210,7 +210,11 @@ export default async function exportPage({ req.url += '/' } - if (buildExport && renderOpts.domainLocales) { + if ( + buildExport && + renderOpts.domainLocales && + renderOpts.domainLocales.length > 0 + ) { addRequestMeta(req, '__nextIsLocaleDomain', true) } diff --git a/test/integration/i18n-support/test/shared.js b/test/integration/i18n-support/test/shared.js index b95c70805c04..511265d583bb 100644 --- a/test/integration/i18n-support/test/shared.js +++ b/test/integration/i18n-support/test/shared.js @@ -395,6 +395,49 @@ export function runTests(ctx) { } }) + // The page is accessible on subpath as well as on the domain url without subpath. + // Once this is not the case the test will need to be changed to access it via domain. + // Beware of the different expectations on dev and prod version since the pre-rendering on dev does not work with domain locales + it('should prerender with the correct href for locale domain', async () => { + let browser = await webdriver(ctx.appPort, `${ctx.basePath || ''}/go`) + + for (const [element, pathname] of [ + ['#to-another', '/another'], + ['#to-gsp', '/gsp'], + ['#to-fallback-first', '/gsp/fallback/first'], + ['#to-fallback-hello', '/gsp/fallback/hello'], + ['#to-gssp', '/gssp'], + ['#to-gssp-slug', '/gssp/first'], + ]) { + const href = await browser.elementByCss(element).getAttribute('href') + if (ctx.isDev) { + expect(href).toBe(`${ctx.basePath || ''}/go${pathname}`) + } else { + expect(href).toBe(`https://example.com${ctx.basePath || ''}${pathname}`) + } + } + + browser = await webdriver(ctx.appPort, `${ctx.basePath || ''}/go-BE`) + + for (const [element, pathname] of [ + ['#to-another', '/another'], + ['#to-gsp', '/gsp'], + ['#to-fallback-first', '/gsp/fallback/first'], + ['#to-fallback-hello', '/gsp/fallback/hello'], + ['#to-gssp', '/gssp'], + ['#to-gssp-slug', '/gssp/first'], + ]) { + const href = await browser.elementByCss(element).getAttribute('href') + if (ctx.isDev) { + expect(href).toBe(`${ctx.basePath || ''}/go-BE${pathname}`) + } else { + expect(href).toBe( + `https://example.com${ctx.basePath || ''}/go-BE${pathname}` + ) + } + } + }) + it('should render the correct href with locale domains but not on a locale domain', async () => { let browser = await webdriver( ctx.appPort, From 53361723fd5d02b2513c6b638d571b3b68bbf6ac Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jakub=20Peka=C5=99?= Date: Mon, 8 Aug 2022 11:06:21 +0200 Subject: [PATCH 3/4] Changed export worker domain locale check --- packages/next/export/worker.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/next/export/worker.ts b/packages/next/export/worker.ts index 0d40d786df95..4943d1fb0902 100644 --- a/packages/next/export/worker.ts +++ b/packages/next/export/worker.ts @@ -213,7 +213,7 @@ export default async function exportPage({ if ( buildExport && renderOpts.domainLocales && - renderOpts.domainLocales.length > 0 + renderOpts.domainLocales.some((dl => dl.locales?.includes(locale))) ) { addRequestMeta(req, '__nextIsLocaleDomain', true) } From f88b90808939720df7a0a9c183ab74dfc2e397cf Mon Sep 17 00:00:00 2001 From: JJ Kasper Date: Mon, 8 Aug 2022 18:14:43 -0500 Subject: [PATCH 4/4] Update check --- packages/next/export/worker.ts | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/packages/next/export/worker.ts b/packages/next/export/worker.ts index 4943d1fb0902..265aed3cf1ee 100644 --- a/packages/next/export/worker.ts +++ b/packages/next/export/worker.ts @@ -211,9 +211,13 @@ export default async function exportPage({ } if ( + locale && buildExport && renderOpts.domainLocales && - renderOpts.domainLocales.some((dl => dl.locales?.includes(locale))) + renderOpts.domainLocales.some( + (dl) => + dl.defaultLocale === locale || dl.locales?.includes(locale || '') + ) ) { addRequestMeta(req, '__nextIsLocaleDomain', true) }