Skip to content

Commit

Permalink
fix: default locale catch-all route overrides locale-specific one
Browse files Browse the repository at this point in the history
With there being a catch-all route (_.vue) in pages directory,
depending on the order of locales in configuration, default locale's
route could shadow locale-specific one due to being first on the list
of generated routes. For example, routes were generated like so:

```
  routes: [{
    path: "/*",
    component: _0bdbeb02,
    name: "all___en"
  }, {
    path: "/fr/*",
    component: _0bdbeb02,
    name: "all___fr"
  }],
```

which meant that `/*` route never allowed later ones to be matched.

Fix by sorting routes by specificity, using function exposed
by `@nuxt/utils` (only from 2.10.2).

Resolves #152
  • Loading branch information
rchl committed Nov 11, 2019
1 parent 0046041 commit 196bf9c
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 3 deletions.
11 changes: 8 additions & 3 deletions src/helpers/routes.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
const {
STRATEGIES
} = require('./constants')
const { STRATEGIES } = require('./constants')
const { extractComponentOptions } = require('./components')
const { getPageOptions, getLocaleCodes } = require('./utils')

Expand Down Expand Up @@ -137,5 +135,12 @@ exports.makeRoutes = (baseRoutes, {
localizedRoutes = localizedRoutes.concat(buildLocalizedRoutes(route, { locales }))
}

try {
const { sortRoutes } = require('@nuxt/utils')
localizedRoutes = sortRoutes(localizedRoutes)
} catch (error) {
// Ignore
}

return localizedRoutes
}
7 changes: 7 additions & 0 deletions test/fixture/dynamic/nuxt.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
const { resolve } = require('path')

module.exports = {
...require('../base.config.js'),
buildDir: resolve(__dirname, '.nuxt'),
srcDir: __dirname
}
9 changes: 9 additions & 0 deletions test/fixture/dynamic/pages/_.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<template>
<div>
<div id="current-locale">locale: {{ $i18n.locale }}</div>
</div>
</template>

<script>
export default {}
</script>
22 changes: 22 additions & 0 deletions test/module.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -569,6 +569,28 @@ describe('invalid strategy', () => {
})
})

describe('dynamic route', () => {
let nuxt

beforeAll(async () => {
nuxt = (await setup(loadConfig(__dirname, 'dynamic'))).nuxt
})

afterAll(async () => {
await nuxt.close()
})

test('can access catch-all route for every locale', async () => {
let html = await get('/aaa')
let dom = getDom(html)
expect(dom.querySelector('#current-locale').textContent).toBe('locale: en')

html = await get('/fr/aaa')
dom = getDom(html)
expect(dom.querySelector('#current-locale').textContent).toBe('locale: fr')
})
})

describe('hash mode', () => {
let nuxt

Expand Down

0 comments on commit 196bf9c

Please sign in to comment.