Unverified Commit 55e4a3d1 authored by JJ Kasper's avatar JJ Kasper Committed by GitHub

Only create locale domain links if on locale domain (#22032)

This updates to only create locale domain links if the current hostname is a locale domain to prevent making links unvisitable in development and preview environments where the configured locale domains don't point to the current build. 

Closes: https://github.com/vercel/next.js/issues/21211
Closes: https://github.com/vercel/next.js/issues/22011
parent 5febe218
...@@ -349,6 +349,7 @@ export function getUtils({ ...@@ -349,6 +349,7 @@ export function getUtils({
if (detectedDomain) { if (detectedDomain) {
defaultLocale = detectedDomain.defaultLocale defaultLocale = detectedDomain.defaultLocale
detectedLocale = defaultLocale detectedLocale = defaultLocale
;(req as any).__nextIsLocaleDomain = true
} }
// if not domain specific locale use accept-language preferred // if not domain specific locale use accept-language preferred
......
...@@ -301,12 +301,17 @@ function Link(props: React.PropsWithChildren<LinkProps>) { ...@@ -301,12 +301,17 @@ function Link(props: React.PropsWithChildren<LinkProps>) {
const curLocale = const curLocale =
typeof locale !== 'undefined' ? locale : router && router.locale typeof locale !== 'undefined' ? locale : router && router.locale
const localeDomain = getDomainLocale( // we only render domain locales if we are currently on a domain locale
as, // so that locale links are still visitable in development/preview envs
curLocale, const localeDomain =
router && router.locales, router &&
router && router.domainLocales router.isLocaleDomain &&
) getDomainLocale(
as,
curLocale,
router && router.locales,
router && router.domainLocales
)
childProps.href = childProps.href =
localeDomain || localeDomain ||
......
...@@ -41,6 +41,7 @@ const urlPropertyFields = [ ...@@ -41,6 +41,7 @@ const urlPropertyFields = [
'locales', 'locales',
'defaultLocale', 'defaultLocale',
'isReady', 'isReady',
'isLocaleDomain',
] ]
const routerEvents = [ const routerEvents = [
'routeChangeStart', 'routeChangeStart',
......
...@@ -348,6 +348,7 @@ export type BaseRouter = { ...@@ -348,6 +348,7 @@ export type BaseRouter = {
locales?: string[] locales?: string[]
defaultLocale?: string defaultLocale?: string
domainLocales?: DomainLocales domainLocales?: DomainLocales
isLocaleDomain: boolean
} }
export type NextRouter = BaseRouter & export type NextRouter = BaseRouter &
...@@ -488,6 +489,7 @@ export default class Router implements BaseRouter { ...@@ -488,6 +489,7 @@ export default class Router implements BaseRouter {
defaultLocale?: string defaultLocale?: string
domainLocales?: DomainLocales domainLocales?: DomainLocales
isReady: boolean isReady: boolean
isLocaleDomain: boolean
private _idx: number = 0 private _idx: number = 0
...@@ -579,12 +581,17 @@ export default class Router implements BaseRouter { ...@@ -579,12 +581,17 @@ export default class Router implements BaseRouter {
self.__NEXT_DATA__.gip || self.__NEXT_DATA__.gip ||
(!autoExportDynamic && !self.location.search) (!autoExportDynamic && !self.location.search)
) )
this.isLocaleDomain = false
if (process.env.__NEXT_I18N_SUPPORT) { if (process.env.__NEXT_I18N_SUPPORT) {
this.locale = locale this.locale = locale
this.locales = locales this.locales = locales
this.defaultLocale = defaultLocale this.defaultLocale = defaultLocale
this.domainLocales = domainLocales this.domainLocales = domainLocales
this.isLocaleDomain = !!detectDomainLocale(
domainLocales,
self.location.hostname
)
} }
if (typeof window !== 'undefined') { if (typeof window !== 'undefined') {
...@@ -817,6 +824,7 @@ export default class Router implements BaseRouter { ...@@ -817,6 +824,7 @@ export default class Router implements BaseRouter {
if ( if (
!didNavigate && !didNavigate &&
detectedDomain && detectedDomain &&
this.isLocaleDomain &&
self.location.hostname !== detectedDomain.domain self.location.hostname !== detectedDomain.domain
) { ) {
const asNoBasePath = delBasePath(as) const asNoBasePath = delBasePath(as)
......
...@@ -443,6 +443,7 @@ export default class Server { ...@@ -443,6 +443,7 @@ export default class Server {
if (detectedDomain) { if (detectedDomain) {
defaultLocale = detectedDomain.defaultLocale defaultLocale = detectedDomain.defaultLocale
detectedLocale = defaultLocale detectedLocale = defaultLocale
;(req as any).__nextIsLocaleDomain = true
} }
// if not domain specific locale use accept-language preferred // if not domain specific locale use accept-language preferred
......
...@@ -80,6 +80,7 @@ class ServerRouter implements NextRouter { ...@@ -80,6 +80,7 @@ class ServerRouter implements NextRouter {
locales?: string[] locales?: string[]
defaultLocale?: string defaultLocale?: string
domainLocales?: DomainLocales domainLocales?: DomainLocales
isLocaleDomain: boolean
// TODO: Remove in the next major version, as this would mean the user is adding event listeners in server-side `render` method // TODO: Remove in the next major version, as this would mean the user is adding event listeners in server-side `render` method
static events: MittEmitter = mitt() static events: MittEmitter = mitt()
...@@ -90,10 +91,11 @@ class ServerRouter implements NextRouter { ...@@ -90,10 +91,11 @@ class ServerRouter implements NextRouter {
{ isFallback }: { isFallback: boolean }, { isFallback }: { isFallback: boolean },
isReady: boolean, isReady: boolean,
basePath: string, basePath: string,
locale?: string, locale: string | undefined,
locales?: string[], locales: string[] | undefined,
defaultLocale?: string, defaultLocale: string | undefined,
domainLocales?: DomainLocales domainLocales: DomainLocales | undefined,
isLocaleDomain: boolean
) { ) {
this.route = pathname.replace(/\/$/, '') || '/' this.route = pathname.replace(/\/$/, '') || '/'
this.pathname = pathname this.pathname = pathname
...@@ -106,6 +108,7 @@ class ServerRouter implements NextRouter { ...@@ -106,6 +108,7 @@ class ServerRouter implements NextRouter {
this.defaultLocale = defaultLocale this.defaultLocale = defaultLocale
this.isReady = isReady this.isReady = isReady
this.domainLocales = domainLocales this.domainLocales = domainLocales
this.isLocaleDomain = isLocaleDomain
} }
push(): any { push(): any {
...@@ -550,7 +553,8 @@ export async function renderToHTML( ...@@ -550,7 +553,8 @@ export async function renderToHTML(
renderOpts.locale, renderOpts.locale,
renderOpts.locales, renderOpts.locales,
renderOpts.defaultLocale, renderOpts.defaultLocale,
renderOpts.domainLocales renderOpts.domainLocales,
(req as any).__nextIsLocaleDomain
) )
const ctx = { const ctx = {
err, err,
......
...@@ -67,7 +67,9 @@ export function runTests(ctx) { ...@@ -67,7 +67,9 @@ export function runTests(ctx) {
}) })
}) })
it('should redirect to locale domain correctly client-side', async () => { // this test can not currently be tested in browser without modifying the
// host resolution since it needs a domain to test locale domains behavior
it.skip('should redirect to locale domain correctly client-side', async () => {
const browser = await webdriver(ctx.appPort, `${ctx.basePath || '/'}`) const browser = await webdriver(ctx.appPort, `${ctx.basePath || '/'}`)
await browser.eval(`(function() { await browser.eval(`(function() {
...@@ -104,7 +106,9 @@ export function runTests(ctx) { ...@@ -104,7 +106,9 @@ export function runTests(ctx) {
) )
}) })
it('should render the correct href for locale domain', async () => { // this test can not currently be tested in browser without modifying the
// host resolution since it needs a domain to test locale domains behavior
it.skip('should render the correct href for locale domain', async () => {
let browser = await webdriver( let browser = await webdriver(
ctx.appPort, ctx.appPort,
`${ctx.basePath || ''}/links?nextLocale=go` `${ctx.basePath || ''}/links?nextLocale=go`
...@@ -142,6 +146,46 @@ export function runTests(ctx) { ...@@ -142,6 +146,46 @@ export function runTests(ctx) {
} }
}) })
it('should render the correct href with locale domains but not on a locale domain', async () => {
let browser = await webdriver(
ctx.appPort,
`${ctx.basePath || ''}/links?nextLocale=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')
const { hostname, pathname: hrefPathname } = url.parse(href)
expect(hostname).not.toBe('example.com')
expect(hrefPathname).toBe(`${ctx.basePath || ''}/go${pathname}`)
}
browser = await webdriver(
ctx.appPort,
`${ctx.basePath || ''}/links?nextLocale=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')
const { hostname, pathname: hrefPathname } = url.parse(href)
expect(hostname).not.toBe('example.com')
expect(hrefPathname).toBe(`${ctx.basePath || ''}/go-BE${pathname}`)
}
})
it('should navigate through history with query correctly', async () => { it('should navigate through history with query correctly', async () => {
const browser = await webdriver(ctx.appPort, `${ctx.basePath || '/'}`) const browser = await webdriver(ctx.appPort, `${ctx.basePath || '/'}`)
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment