Skip to content

Commit

Permalink
Fix Link generation for SSG pages if locale domains are used (#36818)
Browse files Browse the repository at this point in the history
* Fix SSG Link generation if using domain locales

* Updated condition and test

* Changed export worker domain locale check

* Update check

Co-authored-by: JJ Kasper <jj@jjsweb.site>
  • Loading branch information
pekarja5 and ijjk committed Aug 8, 2022
1 parent 60c8e5c commit 289bfa6
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 1 deletion.
16 changes: 15 additions & 1 deletion packages/next/export/worker.ts
Expand Up @@ -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'
Expand All @@ -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')

Expand Down Expand Up @@ -84,6 +85,7 @@ interface RenderOpts {
locales?: string[]
locale?: string
defaultLocale?: string
domainLocales?: DomainLocale[]
trailingSlash?: boolean
appDir?: boolean
}
Expand Down Expand Up @@ -208,6 +210,18 @@ export default async function exportPage({
req.url += '/'
}

if (
locale &&
buildExport &&
renderOpts.domainLocales &&
renderOpts.domainLocales.some(
(dl) =>
dl.defaultLocale === locale || dl.locales?.includes(locale || '')
)
) {
addRequestMeta(req, '__nextIsLocaleDomain', true)
}

envConfig.setConfig({
serverRuntimeConfig,
publicRuntimeConfig: renderOpts.runtimeConfig,
Expand Down
43 changes: 43 additions & 0 deletions test/integration/i18n-support/test/shared.js
Expand Up @@ -407,6 +407,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,
Expand Down

0 comments on commit 289bfa6

Please sign in to comment.