Skip to content

Commit

Permalink
different domains AND prefixes. see nuxt-modules#1161
Browse files Browse the repository at this point in the history
  • Loading branch information
lfglopes committed May 26, 2021
1 parent 4f95034 commit af22fda
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 9 deletions.
15 changes: 11 additions & 4 deletions src/helpers/routes.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ export function makeRoutes (baseRoutes, {
defaultLocaleRouteNameSuffix,
differentDomains,
includeUprefixedFallback,
localeCodes,
// localeCodes,
locales,
pages,
pagesDir,
parsePages,
Expand All @@ -37,9 +38,10 @@ export function makeRoutes (baseRoutes, {
* @param {boolean} [isExtraRouteTree=false]
* @return {NuxtRouteConfig | NuxtRouteConfig[]}
*/
const buildLocalizedRoutes = (route, allowedLocaleCodes, isChild = false, isExtraRouteTree = false) => {
const buildLocalizedRoutes = (route, locales, isChild = false, isExtraRouteTree = false) => {
/** @type {NuxtRouteConfig[]} */
const routes = []
const allowedLocaleCodes = locales.map(val => val.code)

// Skip route if it is only a redirect without a component.
if (route.redirect && !route.component) {
Expand All @@ -58,7 +60,7 @@ export function makeRoutes (baseRoutes, {
// Component-specific options
const componentOptions = {
// @ts-ignore
locales: localeCodes,
locales,
...pageOptions,
...{ locales: allowedLocaleCodes }
}
Expand All @@ -78,6 +80,7 @@ export function makeRoutes (baseRoutes, {
// Generate routes for component's supported locales
for (let i = 0, length1 = componentOptions.locales.length; i < length1; i++) {
const locale = componentOptions.locales[i]
const specialPrefix = locales.find(val => val.code === locale).prefix
const { name } = route
let { path } = route
const localizedRoute = { ...route }
Expand Down Expand Up @@ -145,6 +148,10 @@ export function makeRoutes (baseRoutes, {
path = `/${locale}${path}`
}

if (specialPrefix) {
path = `${specialPrefix}${path}`
}

// - Follow Nuxt and add or remove trailing slashes depending on "router.trailingSlash`
// - If "router.trailingSlash" is not specified then default to no trailing slash (like Nuxt)
// - Children with relative paths must not start with slash so don't append if path is empty.
Expand All @@ -168,7 +175,7 @@ export function makeRoutes (baseRoutes, {

for (let i = 0, length1 = baseRoutes.length; i < length1; i++) {
const route = baseRoutes[i]
localizedRoutes = localizedRoutes.concat(buildLocalizedRoutes(route, localeCodes))
localizedRoutes = localizedRoutes.concat(buildLocalizedRoutes(route, locales))
}

try {
Expand Down
25 changes: 20 additions & 5 deletions src/templates/utils-common.js
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ export function getDomainFromLocale (localeCode, req, { normalizedLocales }) {
*/
export function getLocaleDomain (locales, req) {
/** @type {string | undefined} */
let host
let host, path

if (process.client) {
host = window.location.host
Expand All @@ -156,10 +156,25 @@ export function getLocaleDomain (locales, req) {
host = Array.isArray(detectedHost) ? detectedHost[0] : detectedHost
}

if (host) {
const matchingLocale = locales.find(l => l.domain === host)
if (matchingLocale) {
return matchingLocale.code
if (process.client) {
path = window.location.pathname
} else if (req) {
path = req.url
}

if (host && path) {
const matchingLocale = locales.filter(l => l.domain === host)
if (matchingLocale.length === 1) {
return matchingLocale[0].code
}

if (matchingLocale.length > 1) {
const matcingLocaleWithPrefix = locales.find(l => path.match(new RegExp(`^${l.prefix}`)))
if (matcingLocaleWithPrefix) {
return matcingLocaleWithPrefix.code
}

return matchingLocale[0].code
}
}

Expand Down

0 comments on commit af22fda

Please sign in to comment.