From 0d17884f7b2be3bea40186a53f4297147ee4b425 Mon Sep 17 00:00:00 2001
From: divine <48183131+divine@users.noreply.github.com>
Date: Thu, 22 Jul 2021 15:56:24 +0300
Subject: [PATCH 1/3] feat: make sortRoutes optional
---
docs/content/en/options-reference.md | 9 +++++++++
src/helpers/constants.js | 1 +
src/helpers/routes.js | 15 +++++++++------
types/index.d.ts | 1 +
4 files changed, 20 insertions(+), 6 deletions(-)
diff --git a/docs/content/en/options-reference.md b/docs/content/en/options-reference.md
index c1eabc4b7..62c23517e 100644
--- a/docs/content/en/options-reference.md
+++ b/docs/content/en/options-reference.md
@@ -104,6 +104,15 @@ The app's default locale. Should match code of one of defined `locales`.
When using `prefix_except_default` strategy, URLs for locale specified here won't have a prefix. **It's recommended to set this to some locale** regardless of chosen strategy, as it will be used as a fallback locale when navigating to a non-existent route.
+## `sortRoutes`
+
+v6.27.4+
+
+- type: `boolean`
+- default: `true`
+
+Whether to sort routes by using `sortRoutes` from `@nuxt/utils`.
+
## `strategy`
- type: `string`
diff --git a/src/helpers/constants.js b/src/helpers/constants.js
index 13e1b0319..a40f656b2 100644
--- a/src/helpers/constants.js
+++ b/src/helpers/constants.js
@@ -28,6 +28,7 @@ export const DEFAULT_OPTIONS = {
defaultDirection: 'ltr',
routesNameSeparator: '___',
defaultLocaleRouteNameSuffix: 'default',
+ sortRoutes: true,
strategy: STRATEGY_PREFIX_EXCEPT_DEFAULT,
lazy: false,
langDir: null,
diff --git a/src/helpers/routes.js b/src/helpers/routes.js
index 2a2fb9810..51ae66ee3 100644
--- a/src/helpers/routes.js
+++ b/src/helpers/routes.js
@@ -24,6 +24,7 @@ export function makeRoutes (baseRoutes, {
pagesDir,
parsePages,
routesNameSeparator,
+ sortRoutes,
strategy,
trailingSlash
}) {
@@ -171,12 +172,14 @@ export function makeRoutes (baseRoutes, {
localizedRoutes = localizedRoutes.concat(buildLocalizedRoutes(route, localeCodes))
}
- try {
- // @ts-ignore
- const { sortRoutes } = require('@nuxt/utils')
- localizedRoutes = sortRoutes(localizedRoutes)
- } catch (error) {
- // Ignore
+ if (sortRoutes) {
+ try {
+ // @ts-ignore
+ const { sortRoutes: sortRoutesUtils } = require('@nuxt/utils')
+ localizedRoutes = sortRoutesUtils(localizedRoutes)
+ } catch (error) {
+ // Ignore
+ }
}
return localizedRoutes
diff --git a/types/index.d.ts b/types/index.d.ts
index 13eb64ec8..6c1d8248a 100644
--- a/types/index.d.ts
+++ b/types/index.d.ts
@@ -73,6 +73,7 @@ export interface Options extends BaseOptions {
routesNameSeparator?: string
seo?: boolean
skipSettingLocaleOnNavigate?: boolean,
+ sortRoutes?: boolean,
strategy?: Strategies
vueI18n?: I18nOptions | string
vueI18nLoader?: boolean
From 4e0d53e7e9e01e0821c37233bc575305482ab0d5 Mon Sep 17 00:00:00 2001
From: divine <48183131+divine@users.noreply.github.com>
Date: Thu, 22 Jul 2021 15:57:53 +0300
Subject: [PATCH 2/3] docs: fix sortRoutes version badge
---
docs/content/en/options-reference.md | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/docs/content/en/options-reference.md b/docs/content/en/options-reference.md
index 62c23517e..987dab840 100644
--- a/docs/content/en/options-reference.md
+++ b/docs/content/en/options-reference.md
@@ -106,7 +106,7 @@ When using `prefix_except_default` strategy, URLs for locale specified here won'
## `sortRoutes`
-v6.27.4+
+v6.28.0+
- type: `boolean`
- default: `true`
From 4ab887999afdd6e065eb8d79e1ee254056372d04 Mon Sep 17 00:00:00 2001
From: Rafal Chlodnicki
Date: Thu, 22 Jul 2021 15:47:01 +0200
Subject: [PATCH 3/3] extend documentation
---
docs/content/en/options-reference.md | 4 +++-
src/helpers/routes.js | 4 ++--
2 files changed, 5 insertions(+), 3 deletions(-)
diff --git a/docs/content/en/options-reference.md b/docs/content/en/options-reference.md
index 987dab840..77ba18371 100644
--- a/docs/content/en/options-reference.md
+++ b/docs/content/en/options-reference.md
@@ -111,7 +111,9 @@ When using `prefix_except_default` strategy, URLs for locale specified here won'
- type: `boolean`
- default: `true`
-Whether to sort routes by using `sortRoutes` from `@nuxt/utils`.
+Whether to sort routes by using the `sortRoutes` function from the `@nuxt/utils` package.
+
+While Nuxt sorts the routes itself, it does that before **nuxt-i18n** has added its own generated routes so the module has to re-sort them again. This is necessary as otherwise some routes might become inaccessible due to being shadowed by more generic routes. If you are adding custom routes programmatically, the sorting might change the order of your custom routes in unexpected ways so in that case you might want to disable sorting and handle that yourself. In that case you have to ensure the correct order yourself so that, for example, a more generic route like `/en/*` doesn't shadow a more specific `/en/foo/*` route (the latter should be registered first to work properly).
## `strategy`
diff --git a/src/helpers/routes.js b/src/helpers/routes.js
index 51ae66ee3..e2f28ea24 100644
--- a/src/helpers/routes.js
+++ b/src/helpers/routes.js
@@ -175,8 +175,8 @@ export function makeRoutes (baseRoutes, {
if (sortRoutes) {
try {
// @ts-ignore
- const { sortRoutes: sortRoutesUtils } = require('@nuxt/utils')
- localizedRoutes = sortRoutesUtils(localizedRoutes)
+ const { sortRoutes: sortRoutesFn } = require('@nuxt/utils')
+ localizedRoutes = sortRoutesFn(localizedRoutes)
} catch (error) {
// Ignore
}