Skip to content

Commit

Permalink
fix: Fix duplicate child routes with PREFIX_AND_DEFAULT strategy (fixes
Browse files Browse the repository at this point in the history
#292) (#294)

Fixes two problems when using PREFIX_AND_DEFAULT strategy:

 - parent route with child routes had strange name like
  'undefined__default'. Fixed by not appending default suffix if route
  has children. Such routes don't need name as navigating to them
  wouldn't really work properly anyway (need to navigate to child
  route).

 - Extra child routes were added with 'default' suffix. Fixed by not
   adding extra routes in children arrays. Those don't make sense as
   only parent routes should have extra route with no locale prefix.
  • Loading branch information
rchl authored and paulgv committed May 9, 2019
1 parent 3b2de84 commit 76d5948
Showing 1 changed file with 27 additions and 7 deletions.
34 changes: 27 additions & 7 deletions src/helpers/routes.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ exports.makeRoutes = (baseRoutes, {
locales = getLocaleCodes(locales)
let localizedRoutes = []

const buildLocalizedRoutes = (route, routeOptions = {}, isChild = false) => {
const buildLocalizedRoutes = (route, routeOptions = {}, isChild = false, isExtraRouteTree = false) => {
const routes = []
let pageOptions

Expand Down Expand Up @@ -73,7 +73,7 @@ exports.makeRoutes = (baseRoutes, {
delete localizedRoute.name
localizedRoute.children = []
for (let i = 0, length1 = route.children.length; i < length1; i++) {
localizedRoute.children = localizedRoute.children.concat(buildLocalizedRoutes(route.children[i], { locales: [locale] }, true))
localizedRoute.children = localizedRoute.children.concat(buildLocalizedRoutes(route.children[i], { locales: [locale] }, true, isExtraRouteTree))
}
}

Expand All @@ -82,6 +82,31 @@ exports.makeRoutes = (baseRoutes, {
path = encodePaths ? encodeURI(componentOptions.paths[locale]) : componentOptions.paths[locale]
}

// For PREFIX_AND_DEFAULT strategy and default locale:
// - if it's a parent route, add it with default locale suffix added (no suffix if route has children)
// - if it's a child route of that extra parent route, append default suffix to it
if (locale === defaultLocale && strategy === STRATEGIES.PREFIX_AND_DEFAULT) {
if (!isChild) {
const defaultRoute = { ...localizedRoute, path }

// Only routes without children have name
if (!defaultRoute.children) {
defaultRoute.name = localizedRoute.name + routesNameSeparator + defaultLocaleRouteNameSuffix
} else {
// Recreate child routes with default suffix added
defaultRoute.children = []
for (const childRoute of route.children) {
// isExtraRouteTree argument is true to indicate that this is extra route added for PREFIX_AND_DEFAULT strategy
defaultRoute.children = defaultRoute.children.concat(buildLocalizedRoutes(childRoute, { locales: [locale] }, true, true))
}
}

routes.push(defaultRoute)
} else if (isChild && isExtraRouteTree) {
localizedRoute.name += routesNameSeparator + defaultLocaleRouteNameSuffix
}
}

// Add route prefix if needed
const shouldAddPrefix = (
// No prefix if app uses different locale domains
Expand All @@ -92,11 +117,6 @@ exports.makeRoutes = (baseRoutes, {
!(locale === defaultLocale && strategy === STRATEGIES.PREFIX_EXCEPT_DEFAULT)
)

if (locale === defaultLocale && strategy === STRATEGIES.PREFIX_AND_DEFAULT) {
const nameDefault = localizedRoute.name + routesNameSeparator + defaultLocaleRouteNameSuffix
routes.push({ ...localizedRoute, path, name: nameDefault })
}

if (shouldAddPrefix) {
path = `/${locale}${path}`
}
Expand Down

0 comments on commit 76d5948

Please sign in to comment.