Skip to content

Commit

Permalink
fix: don't add trailing slashes to generated routes
Browse files Browse the repository at this point in the history
Module was adding a trailing slash for extra routes that
it was generating.

This isn't normally a problem as VueRouter runs by default in
non-strict mode and normalizes paths to not have trailing slashes but
when the "router.trailingSlash = false" option was set in Nuxt, then
the matching was strict and actually made trailing slashes appear in
the path.

Follow the same logic as Nuxt and:
 - don't add trailing slashes by default or when "router.trailingSlash"
   option is set to "false"
 - add trailing slashes when "router.trailingSlash" option is set
   to "true"

Resolves #717
  • Loading branch information
rchl committed May 25, 2020
1 parent 09d2c0f commit dd284a3
Show file tree
Hide file tree
Showing 4 changed files with 362 additions and 325 deletions.
34 changes: 21 additions & 13 deletions src/helpers/routes.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,17 @@ const { extractComponentOptions } = require('./components')
const { getPageOptions, getLocaleCodes } = require('./utils')

exports.makeRoutes = (baseRoutes, {
locales,
defaultLocale,
routesNameSeparator,
defaultLocaleRouteNameSuffix,
differentDomains,
isNuxtGenerate,
strategy,
parsePages,
locales,
pages,
pagesDir,
differentDomains
parsePages,
routesNameSeparator,
strategy,
trailingSlash
}) => {
locales = getLocaleCodes(locales)
let localizedRoutes = []
Expand Down Expand Up @@ -78,10 +79,12 @@ exports.makeRoutes = (baseRoutes, {
path = componentOptions.paths[locale]
}

const isDefaultLocale = locale === defaultLocale

// 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 (isDefaultLocale && strategy === STRATEGIES.PREFIX_AND_DEFAULT) {
if (!isChild) {
const defaultRoute = { ...localizedRoute, path }

Expand Down Expand Up @@ -113,18 +116,23 @@ exports.makeRoutes = (baseRoutes, {
// No need to add prefix if child's path is relative
!isChildWithRelativePath &&
// Skip default locale if strategy is PREFIX_EXCEPT_DEFAULT
!(locale === defaultLocale && strategy === STRATEGIES.PREFIX_EXCEPT_DEFAULT)
!(isDefaultLocale && strategy === STRATEGIES.PREFIX_EXCEPT_DEFAULT)
)

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

if (locale === defaultLocale && strategy === STRATEGIES.PREFIX && isNuxtGenerate) {
routes.push({
path: route.path,
redirect: 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.
path = path.replace(/\/+$/, '') + (trailingSlash ? '/' : '') || (isChildWithRelativePath ? '' : '/')

if (shouldAddPrefix && isDefaultLocale && strategy === STRATEGIES.PREFIX && isNuxtGenerate) {
routes.push({
path: route.path,
redirect: path
})
}

localizedRoute.path = path
Expand Down
4 changes: 3 additions & 1 deletion src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ module.exports = function (userOptions) {

if (options.strategy !== STRATEGIES.NO_PREFIX) {
if (localeCodes.length) {
const { trailingSlash } = this.options.router
let isNuxtGenerate = false
const extendRoutes = routes => {
// This import (or more specifically 'vue-template-compiler' in helpers/components.js) needs to
Expand All @@ -65,7 +66,8 @@ module.exports = function (userOptions) {
const localizedRoutes = makeRoutes(routes, {
...options,
pagesDir,
isNuxtGenerate
isNuxtGenerate,
trailingSlash
})
routes.splice(0, routes.length)
routes.unshift(...localizedRoutes)
Expand Down
4 changes: 2 additions & 2 deletions src/templates/seo-head.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,11 +66,11 @@ function addHreflangLinks (locales, baseUrl, link) {
localeMap.set(localeIso, locale)
}

for (const [iso, locale] of localeMap.entries()) {
for (const [iso, mapLocale] of localeMap.entries()) {
link.push({
hid: `alternate-hreflang-${iso}`,
rel: 'alternate',
href: baseUrl + this.switchLocalePath(locale.code),
href: baseUrl + this.switchLocalePath(mapLocale.code),
hreflang: iso
})
}
Expand Down
Loading

0 comments on commit dd284a3

Please sign in to comment.