Unverified Commit 5cab03fe authored by JJ Kasper's avatar JJ Kasper Committed by GitHub

Add handling for domain to locale mapping (#17771)

Follow-up to https://github.com/vercel/next.js/pull/17370 this adds mapping of locales to domains and handles default locales for specific domains also allowing specifying which locales can be visited for each domain. 

This PR also updates to output all statically generated pages under the locale prefix to make it easier to locate/lookup and to not redirect to the default locale prefixed path when no `accept-language` header is provided. 
parent e334c4ec
...@@ -780,7 +780,6 @@ export default async function build( ...@@ -780,7 +780,6 @@ export default async function build(
const isFallback = isSsg && ssgStaticFallbackPages.has(page) const isFallback = isSsg && ssgStaticFallbackPages.has(page)
for (const locale of i18n.locales) { for (const locale of i18n.locales) {
if (!isSsg && locale === i18n.defaultLocale) continue
// skip fallback generation for SSG pages without fallback mode // skip fallback generation for SSG pages without fallback mode
if (isSsg && isDynamic && !isFallback) continue if (isSsg && isDynamic && !isFallback) continue
const outputPath = `/${locale}${page === '/' ? '' : page}` const outputPath = `/${locale}${page === '/' ? '' : page}`
...@@ -869,22 +868,19 @@ export default async function build( ...@@ -869,22 +868,19 @@ export default async function build(
// for SSG files with i18n the non-prerendered variants are // for SSG files with i18n the non-prerendered variants are
// output with the locale prefixed so don't attempt moving // output with the locale prefixed so don't attempt moving
// without the prefix // without the prefix
if (!i18n || !isSsg || additionalSsgFile) { if (!i18n || additionalSsgFile) {
await promises.mkdir(path.dirname(dest), { recursive: true }) await promises.mkdir(path.dirname(dest), { recursive: true })
await promises.rename(orig, dest) await promises.rename(orig, dest)
} else if (i18n && !isSsg) {
// this will be updated with the locale prefixed variant
// since all files are output with the locale prefix
delete pagesManifest[page]
} }
if (i18n) { if (i18n) {
if (additionalSsgFile) return if (additionalSsgFile) return
for (const locale of i18n.locales) { for (const locale of i18n.locales) {
// auto-export default locale files exist at root
// TODO: should these always be prefixed with locale
// similar to SSG prerender/fallback files?
if (!isSsg && locale === i18n.defaultLocale) {
continue
}
const localeExt = page === '/' ? path.extname(file) : '' const localeExt = page === '/' ? path.extname(file) : ''
const relativeDestNoPages = relativeDest.substr('pages/'.length) const relativeDestNoPages = relativeDest.substr('pages/'.length)
......
...@@ -222,24 +222,33 @@ const nextServerlessLoader: loader.Loader = function () { ...@@ -222,24 +222,33 @@ const nextServerlessLoader: loader.Loader = function () {
const i18n = ${i18n} const i18n = ${i18n}
const accept = require('@hapi/accept') const accept = require('@hapi/accept')
const { detectLocaleCookie } = require('next/dist/next-server/lib/i18n/detect-locale-cookie') const { detectLocaleCookie } = require('next/dist/next-server/lib/i18n/detect-locale-cookie')
const { detectDomainLocales } = require('next/dist/next-server/lib/i18n/detect-domain-locales')
const { normalizeLocalePath } = require('next/dist/next-server/lib/i18n/normalize-locale-path') const { normalizeLocalePath } = require('next/dist/next-server/lib/i18n/normalize-locale-path')
let detectedLocale = detectLocaleCookie(req, i18n.locales) let detectedLocale = detectLocaleCookie(req, i18n.locales)
const { defaultLocale, locales } = detectDomainLocales(
req,
i18n.domains,
i18n.locales,
i18n.defaultLocale,
)
if (!detectedLocale) { if (!detectedLocale) {
detectedLocale = accept.language( detectedLocale = accept.language(
req.headers['accept-language'], req.headers['accept-language'],
i18n.locales locales
) )
} }
const denormalizedPagePath = denormalizePagePath(parsedUrl.pathname || '/') const denormalizedPagePath = denormalizePagePath(parsedUrl.pathname || '/')
const detectedDefaultLocale = detectedLocale === i18n.defaultLocale const detectedDefaultLocale = !detectedLocale || detectedLocale === defaultLocale
const shouldStripDefaultLocale = const shouldStripDefaultLocale =
detectedDefaultLocale && detectedDefaultLocale &&
denormalizedPagePath === \`/\${i18n.defaultLocale}\` denormalizedPagePath === \`/\${defaultLocale}\`
const shouldAddLocalePrefix = const shouldAddLocalePrefix =
!detectedDefaultLocale && denormalizedPagePath === '/' !detectedDefaultLocale && denormalizedPagePath === '/'
detectedLocale = detectedLocale || i18n.defaultLocale
detectedLocale = detectedLocale || defaultLocale
if ( if (
!fromExport && !fromExport &&
...@@ -260,8 +269,7 @@ const nextServerlessLoader: loader.Loader = function () { ...@@ -260,8 +269,7 @@ const nextServerlessLoader: loader.Loader = function () {
return return
} }
// TODO: domain based locales (domain to locale mapping needs to be provided in next.config.js) const localePathResult = normalizeLocalePath(parsedUrl.pathname, locales)
const localePathResult = normalizeLocalePath(parsedUrl.pathname, i18n.locales)
if (localePathResult.detectedLocale) { if (localePathResult.detectedLocale) {
detectedLocale = localePathResult.detectedLocale detectedLocale = localePathResult.detectedLocale
...@@ -272,11 +280,13 @@ const nextServerlessLoader: loader.Loader = function () { ...@@ -272,11 +280,13 @@ const nextServerlessLoader: loader.Loader = function () {
parsedUrl.pathname = localePathResult.pathname parsedUrl.pathname = localePathResult.pathname
} }
detectedLocale = detectedLocale || i18n.defaultLocale detectedLocale = detectedLocale || defaultLocale
` `
: ` : `
const i18n = {} const i18n = {}
const detectedLocale = undefined const detectedLocale = undefined
const defaultLocale = undefined
const locales = undefined
` `
if (page.match(API_ROUTE)) { if (page.match(API_ROUTE)) {
...@@ -468,8 +478,8 @@ const nextServerlessLoader: loader.Loader = function () { ...@@ -468,8 +478,8 @@ const nextServerlessLoader: loader.Loader = function () {
nextExport: fromExport, nextExport: fromExport,
isDataReq: _nextData, isDataReq: _nextData,
locale: detectedLocale, locale: detectedLocale,
locales: i18n.locales, locales,
defaultLocale: i18n.defaultLocale, defaultLocale,
}, },
options, options,
) )
......
...@@ -11,11 +11,7 @@ import type { ...@@ -11,11 +11,7 @@ import type {
AppProps, AppProps,
PrivateRouteInfo, PrivateRouteInfo,
} from '../next-server/lib/router/router' } from '../next-server/lib/router/router'
import { import { delBasePath, hasBasePath } from '../next-server/lib/router/router'
delBasePath,
hasBasePath,
delLocale,
} from '../next-server/lib/router/router'
import { isDynamicRoute } from '../next-server/lib/router/utils/is-dynamic' import { isDynamicRoute } from '../next-server/lib/router/utils/is-dynamic'
import * as querystring from '../next-server/lib/router/utils/querystring' import * as querystring from '../next-server/lib/router/utils/querystring'
import * as envConfig from '../next-server/lib/runtime-config' import * as envConfig from '../next-server/lib/runtime-config'
...@@ -65,10 +61,9 @@ const { ...@@ -65,10 +61,9 @@ const {
isFallback, isFallback,
head: initialHeadData, head: initialHeadData,
locales, locales,
defaultLocale,
} = data } = data
let { locale } = data let { locale, defaultLocale } = data
const prefix = assetPrefix || '' const prefix = assetPrefix || ''
...@@ -88,19 +83,21 @@ if (hasBasePath(asPath)) { ...@@ -88,19 +83,21 @@ if (hasBasePath(asPath)) {
asPath = delBasePath(asPath) asPath = delBasePath(asPath)
} }
asPath = delLocale(asPath, locale)
if (process.env.__NEXT_i18n_SUPPORT) { if (process.env.__NEXT_i18n_SUPPORT) {
const { const {
normalizeLocalePath, normalizeLocalePath,
} = require('../next-server/lib/i18n/normalize-locale-path') } = require('../next-server/lib/i18n/normalize-locale-path')
if (isFallback && locales) { if (locales) {
const localePathResult = normalizeLocalePath(asPath, locales) const localePathResult = normalizeLocalePath(asPath, locales)
if (localePathResult.detectedLocale) { if (localePathResult.detectedLocale) {
asPath = asPath.substr(localePathResult.detectedLocale.length + 1) asPath = asPath.substr(localePathResult.detectedLocale.length + 1)
locale = localePathResult.detectedLocale } else {
// derive the default locale if it wasn't detected in the asPath
// since we don't prerender static pages with all possible default
// locales
defaultLocale = locale
} }
} }
} }
......
...@@ -203,23 +203,13 @@ export default class PageLoader { ...@@ -203,23 +203,13 @@ export default class PageLoader {
* @param {string} href the route href (file-system path) * @param {string} href the route href (file-system path)
* @param {string} asPath the URL as shown in browser (virtual path); used for dynamic routes * @param {string} asPath the URL as shown in browser (virtual path); used for dynamic routes
*/ */
getDataHref( getDataHref(href: string, asPath: string, ssg: boolean, locale?: string) {
href: string,
asPath: string,
ssg: boolean,
locale?: string,
defaultLocale?: string
) {
const { pathname: hrefPathname, query, search } = parseRelativeUrl(href) const { pathname: hrefPathname, query, search } = parseRelativeUrl(href)
const { pathname: asPathname } = parseRelativeUrl(asPath) const { pathname: asPathname } = parseRelativeUrl(asPath)
const route = normalizeRoute(hrefPathname) const route = normalizeRoute(hrefPathname)
const getHrefForSlug = (path: string) => { const getHrefForSlug = (path: string) => {
const dataRoute = addLocale( const dataRoute = addLocale(getAssetPathFromRoute(path, '.json'), locale)
getAssetPathFromRoute(path, '.json'),
locale,
defaultLocale
)
return addBasePath( return addBasePath(
`/_next/data/${this.buildId}${dataRoute}${ssg ? '' : search}` `/_next/data/${this.buildId}${dataRoute}${ssg ? '' : search}`
) )
...@@ -239,12 +229,7 @@ export default class PageLoader { ...@@ -239,12 +229,7 @@ export default class PageLoader {
* @param {string} href the route href (file-system path) * @param {string} href the route href (file-system path)
* @param {string} asPath the URL as shown in browser (virtual path); used for dynamic routes * @param {string} asPath the URL as shown in browser (virtual path); used for dynamic routes
*/ */
prefetchData( prefetchData(href: string, asPath: string, locale?: string) {
href: string,
asPath: string,
locale?: string,
defaultLocale?: string
) {
const { pathname: hrefPathname } = parseRelativeUrl(href) const { pathname: hrefPathname } = parseRelativeUrl(href)
const route = normalizeRoute(hrefPathname) const route = normalizeRoute(hrefPathname)
return this.promisedSsgManifest!.then( return this.promisedSsgManifest!.then(
...@@ -252,13 +237,7 @@ export default class PageLoader { ...@@ -252,13 +237,7 @@ export default class PageLoader {
// Check if the route requires a data file // Check if the route requires a data file
s.has(route) && s.has(route) &&
// Try to generate data href, noop when falsy // Try to generate data href, noop when falsy
(_dataHref = this.getDataHref( (_dataHref = this.getDataHref(href, asPath, true, locale)) &&
href,
asPath,
true,
locale,
defaultLocale
)) &&
// noop when data has already been prefetched (dedupe) // noop when data has already been prefetched (dedupe)
!document.querySelector( !document.querySelector(
`link[rel="${relPrefetch}"][href^="${_dataHref}"]` `link[rel="${relPrefetch}"][href^="${_dataHref}"]`
......
import { IncomingMessage } from 'http'
export function detectDomainLocales(
req: IncomingMessage,
domainItems:
| Array<{
domain: string
locales: string[]
defaultLocale: string
}>
| undefined,
locales: string[],
defaultLocale: string
) {
let curDefaultLocale = defaultLocale
let curLocales = locales
const { host } = req.headers
if (host && domainItems) {
// remove port from host and remove port if present
const hostname = host.split(':')[0].toLowerCase()
for (const item of domainItems) {
if (hostname === item.domain.toLowerCase()) {
curDefaultLocale = item.defaultLocale
curLocales = item.locales
break
}
}
}
return {
defaultLocale: curDefaultLocale,
locales: curLocales,
}
}
...@@ -974,8 +974,7 @@ export default class Router implements BaseRouter { ...@@ -974,8 +974,7 @@ export default class Router implements BaseRouter {
formatWithValidation({ pathname, query }), formatWithValidation({ pathname, query }),
delBasePath(as), delBasePath(as),
__N_SSG, __N_SSG,
this.locale, this.locale
this.defaultLocale
) )
} }
......
...@@ -227,6 +227,47 @@ function assignDefaults(userConfig: { [key: string]: any }) { ...@@ -227,6 +227,47 @@ function assignDefaults(userConfig: { [key: string]: any }) {
throw new Error(`Specified i18n.defaultLocale should be a string`) throw new Error(`Specified i18n.defaultLocale should be a string`)
} }
if (typeof i18n.domains !== 'undefined' && !Array.isArray(i18n.domains)) {
throw new Error(
`Specified i18n.domains must be an array of domain objects e.g. [ { domain: 'example.fr', defaultLocale: 'fr', locales: ['fr'] } ] received ${typeof i18n.domains}`
)
}
if (i18n.domains) {
const invalidDomainItems = i18n.domains.filter((item: any) => {
if (!item || typeof item !== 'object') return true
if (!item.defaultLocale) return true
if (!item.domain || typeof item.domain !== 'string') return true
if (!item.locales || !Array.isArray(item.locales)) return true
const invalidLocales = item.locales.filter(
(locale: string) => !i18n.locales.includes(locale)
)
if (invalidLocales.length > 0) {
console.error(
`i18n.domains item "${
item.domain
}" has the following locales (${invalidLocales.join(
', '
)}) that aren't provided in the main i18n.locales. Add them to the i18n.locales list or remove them from the domains item locales to continue.\n`
)
return true
}
return false
})
if (invalidDomainItems.length > 0) {
throw new Error(
`Invalid i18n.domains values:\n${invalidDomainItems
.map((item: any) => JSON.stringify(item))
.join(
'\n'
)}\n\ndomains value must follow format { domain: 'example.fr', defaultLocale: 'fr', locales: ['fr'] }`
)
}
}
if (!Array.isArray(i18n.locales)) { if (!Array.isArray(i18n.locales)) {
throw new Error( throw new Error(
`Specified i18n.locales must be an array of locale strings e.g. ["en-US", "nl-NL"] received ${typeof i18n.locales}` `Specified i18n.locales must be an array of locale strings e.g. ["en-US", "nl-NL"] received ${typeof i18n.locales}`
......
...@@ -79,6 +79,7 @@ import accept from '@hapi/accept' ...@@ -79,6 +79,7 @@ import accept from '@hapi/accept'
import { normalizeLocalePath } from '../lib/i18n/normalize-locale-path' import { normalizeLocalePath } from '../lib/i18n/normalize-locale-path'
import { detectLocaleCookie } from '../lib/i18n/detect-locale-cookie' import { detectLocaleCookie } from '../lib/i18n/detect-locale-cookie'
import * as Log from '../../build/output/log' import * as Log from '../../build/output/log'
import { detectDomainLocales } from '../lib/i18n/detect-domain-locales'
const getCustomRouteMatcher = pathMatch(true) const getCustomRouteMatcher = pathMatch(true)
...@@ -193,8 +194,6 @@ export default class Server { ...@@ -193,8 +194,6 @@ export default class Server {
? requireFontManifest(this.distDir, this._isLikeServerless) ? requireFontManifest(this.distDir, this._isLikeServerless)
: null, : null,
optimizeImages: this.nextConfig.experimental.optimizeImages, optimizeImages: this.nextConfig.experimental.optimizeImages,
locales: this.nextConfig.experimental.i18n?.locales,
defaultLocale: this.nextConfig.experimental.i18n?.defaultLocale,
} }
// Only the `publicRuntimeConfig` key is exposed to the client side // Only the `publicRuntimeConfig` key is exposed to the client side
...@@ -309,21 +308,29 @@ export default class Server { ...@@ -309,21 +308,29 @@ export default class Server {
const { pathname, ...parsed } = parseUrl(req.url || '/') const { pathname, ...parsed } = parseUrl(req.url || '/')
let detectedLocale = detectLocaleCookie(req, i18n.locales) let detectedLocale = detectLocaleCookie(req, i18n.locales)
const { defaultLocale, locales } = detectDomainLocales(
req,
i18n.domains,
i18n.locales,
i18n.defaultLocale
)
if (!detectedLocale) { if (!detectedLocale) {
detectedLocale = accept.language( detectedLocale = accept.language(
req.headers['accept-language'], req.headers['accept-language'],
i18n.locales locales
) )
} }
const denormalizedPagePath = denormalizePagePath(pathname || '/') const denormalizedPagePath = denormalizePagePath(pathname || '/')
const detectedDefaultLocale = detectedLocale === i18n.defaultLocale const detectedDefaultLocale =
!detectedLocale || detectedLocale === defaultLocale
const shouldStripDefaultLocale = const shouldStripDefaultLocale =
detectedDefaultLocale && detectedDefaultLocale && denormalizedPagePath === `/${defaultLocale}`
denormalizedPagePath === `/${i18n.defaultLocale}`
const shouldAddLocalePrefix = const shouldAddLocalePrefix =
!detectedDefaultLocale && denormalizedPagePath === '/' !detectedDefaultLocale && denormalizedPagePath === '/'
detectedLocale = detectedLocale || i18n.defaultLocale
detectedLocale = detectedLocale || defaultLocale
if ( if (
i18n.localeDetection !== false && i18n.localeDetection !== false &&
...@@ -342,8 +349,7 @@ export default class Server { ...@@ -342,8 +349,7 @@ export default class Server {
return return
} }
// TODO: domain based locales (domain to locale mapping needs to be provided in next.config.js) const localePathResult = normalizeLocalePath(pathname!, locales)
const localePathResult = normalizeLocalePath(pathname!, i18n.locales)
if (localePathResult.detectedLocale) { if (localePathResult.detectedLocale) {
detectedLocale = localePathResult.detectedLocale detectedLocale = localePathResult.detectedLocale
...@@ -354,7 +360,12 @@ export default class Server { ...@@ -354,7 +360,12 @@ export default class Server {
parsedUrl.pathname = localePathResult.pathname parsedUrl.pathname = localePathResult.pathname
} }
parsedUrl.query.__nextLocale = detectedLocale || i18n.defaultLocale // TODO: render with domain specific locales and defaultLocale also?
// Currently locale specific domains will have all locales populated
// under router.locales instead of only the domain specific ones
parsedUrl.query.__nextLocales = i18n.locales
// parsedUrl.query.__nextDefaultLocale = defaultLocale
parsedUrl.query.__nextLocale = detectedLocale || defaultLocale
} }
res.statusCode = 200 res.statusCode = 200
...@@ -504,13 +515,21 @@ export default class Server { ...@@ -504,13 +515,21 @@ export default class Server {
if (i18n) { if (i18n) {
const localePathResult = normalizeLocalePath(pathname, i18n.locales) const localePathResult = normalizeLocalePath(pathname, i18n.locales)
let detectedLocale = detectLocaleCookie(req, i18n.locales) const { defaultLocale } = detectDomainLocales(
req,
i18n.domains,
i18n.locales,
i18n.defaultLocale
)
let detectedLocale = defaultLocale
if (localePathResult.detectedLocale) { if (localePathResult.detectedLocale) {
pathname = localePathResult.pathname pathname = localePathResult.pathname
detectedLocale = localePathResult.detectedLocale detectedLocale = localePathResult.detectedLocale
} }
_parsedUrl.query.__nextLocale = detectedLocale || i18n.defaultLocale _parsedUrl.query.__nextLocales = i18n.locales
_parsedUrl.query.__nextLocale = detectedLocale
// _parsedUrl.query.__nextDefaultLocale = defaultLocale
} }
pathname = getRouteFromAssetPath(pathname, '.json') pathname = getRouteFromAssetPath(pathname, '.json')
...@@ -1037,6 +1056,18 @@ export default class Server { ...@@ -1037,6 +1056,18 @@ export default class Server {
pagePath!, pagePath!,
!this.renderOpts.dev && this._isLikeServerless !this.renderOpts.dev && this._isLikeServerless
) )
// if loading an static HTML file the locale is required
// to be present since all HTML files are output under their locale
if (
query.__nextLocale &&
typeof components.Component === 'string' &&
!pagePath?.startsWith(`/${query.__nextLocale}`)
) {
const err = new Error('NOT_FOUND')
;(err as any).code = 'ENOENT'
throw err
}
return { return {
components, components,
query: { query: {
...@@ -1045,6 +1076,8 @@ export default class Server { ...@@ -1045,6 +1076,8 @@ export default class Server {
amp: query.amp, amp: query.amp,
_nextDataReq: query._nextDataReq, _nextDataReq: query._nextDataReq,
__nextLocale: query.__nextLocale, __nextLocale: query.__nextLocale,
__nextLocales: query.__nextLocales,
// __nextDefaultLocale: query.__nextDefaultLocale,
} }
: query), : query),
...(params || {}), ...(params || {}),
...@@ -1156,7 +1189,11 @@ export default class Server { ...@@ -1156,7 +1189,11 @@ export default class Server {
} }
const locale = query.__nextLocale as string const locale = query.__nextLocale as string
const locales = query.__nextLocales as string[]
// const defaultLocale = query.__nextDefaultLocale as string
delete query.__nextLocale delete query.__nextLocale
delete query.__nextLocales
// delete query.__nextDefaultLocale
const ssgCacheKey = const ssgCacheKey =
isPreviewMode || !isSSG isPreviewMode || !isSSG
...@@ -1230,7 +1267,8 @@ export default class Server { ...@@ -1230,7 +1267,8 @@ export default class Server {
{ {
fontManifest: this.renderOpts.fontManifest, fontManifest: this.renderOpts.fontManifest,
locale, locale,
locales: this.renderOpts.locales, locales,
// defaultLocale,
} }
) )
...@@ -1251,6 +1289,8 @@ export default class Server { ...@@ -1251,6 +1289,8 @@ export default class Server {
isDataReq, isDataReq,
resolvedUrl, resolvedUrl,
locale, locale,
locales,
// defaultLocale,
// For getServerSideProps we need to ensure we use the original URL // For getServerSideProps we need to ensure we use the original URL
// and not the resolved URL to prevent a hydration mismatch on // and not the resolved URL to prevent a hydration mismatch on
// asPath // asPath
......
...@@ -414,6 +414,8 @@ export async function renderToHTML( ...@@ -414,6 +414,8 @@ export async function renderToHTML(
const isFallback = !!query.__nextFallback const isFallback = !!query.__nextFallback
delete query.__nextFallback delete query.__nextFallback
delete query.__nextLocale delete query.__nextLocale
delete query.__nextLocales
delete query.__nextDefaultLocale
const isSSG = !!getStaticProps const isSSG = !!getStaticProps
const isBuildTimeSSG = isSSG && renderOpts.nextExport const isBuildTimeSSG = isSSG && renderOpts.nextExport
......
...@@ -2,8 +2,20 @@ module.exports = { ...@@ -2,8 +2,20 @@ module.exports = {
// target: 'experimental-serverless-trace', // target: 'experimental-serverless-trace',
experimental: { experimental: {
i18n: { i18n: {
locales: ['nl-NL', 'nl-BE', 'nl', 'en-US', 'en'], locales: ['nl-NL', 'nl-BE', 'nl', 'fr-BE', 'fr', 'en-US', 'en'],
defaultLocale: 'en-US', defaultLocale: 'en-US',
domains: [
{
domain: 'example.be',
defaultLocale: 'nl-BE',
locales: ['nl-BE', 'fr-BE'],
},
{
domain: 'example.fr',
locales: ['fr', 'fr-BE'],
defaultLocale: 'fr',
},
],
}, },
}, },
} }
...@@ -24,9 +24,115 @@ let app ...@@ -24,9 +24,115 @@ let app
let appPort let appPort
// let buildId // let buildId
const locales = ['en-US', 'nl-NL', 'nl-BE', 'nl', 'en'] const locales = ['en-US', 'nl-NL', 'nl-BE', 'nl', 'fr-BE', 'fr', 'en']
function runTests() { function runTests() {
it('should use correct default locale for locale domains', async () => {
const res = await fetchViaHTTP(appPort, '/', undefined, {
headers: {
host: 'example.fr',
},
})
expect(res.status).toBe(200)
const html = await res.text()
const $ = cheerio.load(html)
expect($('html').attr('lang')).toBe('fr')
expect($('#router-locale').text()).toBe('fr')
expect($('#router-as-path').text()).toBe('/')
expect($('#router-pathname').text()).toBe('/')
// expect(JSON.parse($('#router-locales').text())).toEqual(['fr','fr-BE'])
expect(JSON.parse($('#router-locales').text())).toEqual(locales)
const res2 = await fetchViaHTTP(appPort, '/', undefined, {
headers: {
host: 'example.be',
},
})
expect(res2.status).toBe(200)
const html2 = await res2.text()
const $2 = cheerio.load(html2)
expect($2('html').attr('lang')).toBe('nl-BE')
expect($2('#router-locale').text()).toBe('nl-BE')
expect($2('#router-as-path').text()).toBe('/')
expect($2('#router-pathname').text()).toBe('/')
// expect(JSON.parse($2('#router-locales').text())).toEqual(['nl-BE','fr-BE'])
expect(JSON.parse($2('#router-locales').text())).toEqual(locales)
})
it('should strip locale prefix for default locale with locale domains', async () => {
const res = await fetchViaHTTP(appPort, '/fr', undefined, {
headers: {
host: 'example.fr',
},
redirect: 'manual',
})
expect(res.status).toBe(307)
const result = url.parse(res.headers.get('location'), true)
expect(result.pathname).toBe('/')
expect(result.query).toEqual({})
const res2 = await fetchViaHTTP(appPort, '/nl-BE', undefined, {
headers: {
host: 'example.be',
},
redirect: 'manual',
})
expect(res2.status).toBe(307)
const result2 = url.parse(res2.headers.get('location'), true)
expect(result2.pathname).toBe('/')
expect(result2.query).toEqual({})
})
it('should only handle the domain specific locales', async () => {
const checkDomainLocales = async (
domainLocales = [],
domainDefault = '',
domain = ''
) => {
for (const locale of locales) {
const res = await fetchViaHTTP(
appPort,
`/${locale === domainDefault ? '' : locale}`,
undefined,
{
headers: {
host: domain,
},
redirect: 'manual',
}
)
const isDomainLocale = domainLocales.includes(locale)
expect(res.status).toBe(isDomainLocale ? 200 : 404)
if (isDomainLocale) {
const html = await res.text()
const $ = cheerio.load(html)
expect($('html').attr('lang')).toBe(locale)
expect($('#router-locale').text()).toBe(locale)
// expect(JSON.parse($('#router-locales').text())).toEqual(domainLocales)
expect(JSON.parse($('#router-locales').text())).toEqual(locales)
}
}
}
await checkDomainLocales(['nl-BE', 'fr-BE'], 'nl-BE', 'example.be')
await checkDomainLocales(['fr', 'fr-BE'], 'fr', 'example.fr')
})
it('should generate fallbacks with all locales', async () => { it('should generate fallbacks with all locales', async () => {
for (const locale of locales) { for (const locale of locales) {
const html = await renderViaHTTP( const html = await renderViaHTTP(
...@@ -175,15 +281,20 @@ function runTests() { ...@@ -175,15 +281,20 @@ function runTests() {
expect(parsedUrl2.query).toEqual({ hello: 'world' }) expect(parsedUrl2.query).toEqual({ hello: 'world' })
}) })
it('should redirect to default locale route for / without accept-language', async () => { it('should use default locale for / without accept-language', async () => {
const res = await fetchViaHTTP(appPort, '/', undefined, { const res = await fetchViaHTTP(appPort, '/', undefined, {
redirect: 'manual', redirect: 'manual',
}) })
expect(res.status).toBe(307) expect(res.status).toBe(200)
const parsedUrl = url.parse(res.headers.get('location'), true) const html = await res.text()
expect(parsedUrl.pathname).toBe('/en-US') const $ = cheerio.load(html)
expect(parsedUrl.query).toEqual({})
expect($('#router-locale').text()).toBe('en-US')
expect(JSON.parse($('#router-locales').text())).toEqual(locales)
expect(JSON.parse($('#router-query').text())).toEqual({})
expect($('#router-pathname').text()).toBe('/')
expect($('#router-as-path').text()).toBe('/')
const res2 = await fetchViaHTTP( const res2 = await fetchViaHTTP(
appPort, appPort,
...@@ -193,11 +304,17 @@ function runTests() { ...@@ -193,11 +304,17 @@ function runTests() {
redirect: 'manual', redirect: 'manual',
} }
) )
expect(res2.status).toBe(307) expect(res2.status).toBe(200)
const parsedUrl2 = url.parse(res2.headers.get('location'), true) const html2 = await res2.text()
expect(parsedUrl2.pathname).toBe('/en-US') const $2 = cheerio.load(html2)
expect(parsedUrl2.query).toEqual({ hello: 'world' })
expect($2('#router-locale').text()).toBe('en-US')
expect(JSON.parse($2('#router-locales').text())).toEqual(locales)
// page is auto-export so query isn't hydrated until client
expect(JSON.parse($2('#router-query').text())).toEqual({})
expect($2('#router-pathname').text()).toBe('/')
expect($2('#router-as-path').text()).toBe('/')
}) })
it('should load getStaticProps page correctly SSR', async () => { it('should load getStaticProps page correctly SSR', async () => {
......
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