From 751aae38d43dde51be247bf166c8fc9d62d12393 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?David=20M=C3=BCller?= Date: Sun, 11 Mar 2018 07:09:27 +0700 Subject: [PATCH 1/3] fix: dynamic routes must start with an underscore Folders or files within `pages/` should only lead to dynamic routes, if they start with an underscore. Previously, folders like `some_folder` would lead to a route parameter `folder` being introduced. --- lib/common/utils.mjs | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/lib/common/utils.mjs b/lib/common/utils.mjs index 6980bac494..91df8bb262 100644 --- a/lib/common/utils.mjs +++ b/lib/common/utils.mjs @@ -297,8 +297,13 @@ export const createRoutes = function createRoutes(files, srcDir, pagesDir) { if (key === 'index' && i + 1 === keys.length) { route.path += i > 0 ? '' : '/' } else { - route.path += '/' + (key === '_' ? '*' : key.replace('_', ':')) - if (key !== '_' && key.indexOf('_') !== -1) { + route.path += '/' + + (key === '_' + ? '*' + : key.indexOf('_') === 0 + ? key.replace('_', ':') + : key) + if (key !== '_' && key.indexOf('_') === 0) { route.path += '?' } } From 051be92ea5a3d3eaa32613645e713e5ffa6a9e99 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?David=20M=C3=BCller?= Date: Sun, 11 Mar 2018 14:59:37 +0100 Subject: [PATCH 2/3] fix: snake case route names --- lib/common/utils.mjs | 18 +++++++++++------- 1 file changed, 11 insertions(+), 7 deletions(-) diff --git a/lib/common/utils.mjs b/lib/common/utils.mjs index 91df8bb262..1631661c3f 100644 --- a/lib/common/utils.mjs +++ b/lib/common/utils.mjs @@ -283,9 +283,13 @@ export const createRoutes = function createRoutes(files, srcDir, pagesDir) { let route = { name: '', path: '', component: r(srcDir, file) } let parent = routes keys.forEach((key, i) => { + // remove underscore only, if its the prefix + const sanatizedKey = key.indexOf('_') === 0 + ? key.replace('_', '') + : key route.name = route.name - ? route.name + '-' + key.replace('_', '') - : key.replace('_', '') + ? route.name + '-' + sanatizedKey + : sanatizedKey route.name += key === '_' ? 'all' : '' route.chunkName = file.replace(/\.(vue|js)$/, '') let child = _.find(parent, { name: route.name }) @@ -297,11 +301,11 @@ export const createRoutes = function createRoutes(files, srcDir, pagesDir) { if (key === 'index' && i + 1 === keys.length) { route.path += i > 0 ? '' : '/' } else { - route.path += '/' + - (key === '_' - ? '*' - : key.indexOf('_') === 0 - ? key.replace('_', ':') + route.path += '/' + + (key === '_' + ? '*' + : key.indexOf('_') === 0 + ? key.replace('_', ':') : key) if (key !== '_' && key.indexOf('_') === 0) { route.path += '?' From 49cd2e92d25b9345b09e597ea0d14af202437dbb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?David=20M=C3=BCller?= Date: Mon, 19 Mar 2018 19:48:43 +0100 Subject: [PATCH 3/3] test: snake case route names --- test/unit/utils.test.js | 40 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) diff --git a/test/unit/utils.test.js b/test/unit/utils.test.js index 546df984bb..59c3795265 100644 --- a/test/unit/utils.test.js +++ b/test/unit/utils.test.js @@ -198,3 +198,43 @@ describe('utils', () => { expect(chainedFn({}, 10)).toEqual({ foo: 11, bar: 12 }) }) }) + +test('createRoutes should allow snake case routes', () => { + const files = [ + 'pages/_param.vue', + 'pages/subpage/_param.vue', + 'pages/snake_case_route.vue', + 'pages/another_route/_id.vue' + ] + const srcDir = '/some/nuxt/app' + const pagesDir = 'pages' + const routesResult = Utils.createRoutes(files, srcDir, pagesDir) + const expectedResult = [ + { + name: 'snake_case_route', + path: '/snake_case_route', + component: Utils.r('/some/nuxt/app/pages/snake_case_route.vue'), + chunkName: 'pages/snake_case_route' + }, + { + name: 'another_route-id', + path: '/another_route/:id?', + component: Utils.r('/some/nuxt/app/pages/another_route/_id.vue'), + chunkName: 'pages/another_route/_id' + }, + { + name: 'subpage-param', + path: '/subpage/:param?', + component: Utils.r('/some/nuxt/app/pages/subpage/_param.vue'), + chunkName: 'pages/subpage/_param' + }, + { + name: 'param', + path: '/:param?', + component: Utils.r('/some/nuxt/app/pages/_param.vue'), + chunkName: 'pages/_param' + } + ] + + expect(routesResult).toEqual(expectedResult) +})