Home

Issue: 1218

required params in dynamic routes does not work in all cases

[issue link]

As per the documentation of dynamic routes to make a route param :slug required, we need to create an index.vue file in _slug directory and this behaviour is working only when _slug is at the first level directory under pages directory. But when _slug is deeply nested in directory tree, the param becomes optional.

To reproduce this issue create pages with following structure

pages
├── README.md
├── _slug
│   ├── comments.vue
│   └── index.vue
├── index.vue
└── users
    └── _id
        └── index.vue

The routes definition in generated .nuxt/router.js will be


const _617e8672 = () => import('../pages/index.vue' /* webpackChunkName: "pages/index" */).then(m => m.default || m)
const _1b1466ab = () => import('../pages/users/_id/index.vue' /* webpackChunkName: "pages/users-id" */).then(m => m.default || m)
const _cf64727c = () => import('../pages/_slug/index.vue' /* webpackChunkName: "pages/slug" */).then(m => m.default || m)
const _3ac95998 = () => import('../pages/_slug/comments.vue' /* webpackChunkName: "pages/slug-comments" */).then(m => m.default || m)

export function createRouter () {
  return new Router({
    mode: 'history',
    base: '/',
    linkActiveClass: 'nuxt-link-active',
    linkExactActiveClass: 'nuxt-link-exact-active',
    scrollBehavior,
    routes: [
    {
      path: "/",
      component: _617e8672,
      name: "index"
    },
    {
      path: "/users/:id?", // THIS SHOULD BE /users/:id as per documentation
      component: _1b1466ab,
      name: "users-id"
    },
    {
      path: "/:slug", // This is working as expected, as _slug is at first level under pages directory **
      component: _cf64727c,
      name: "slug"
    },
    {
      path: "/:slug/comments",
      component: _3ac95998,
      name: "slug-comments"
    }
    ],
    fallback: false
  })
}

In above use case, path for users-id route should be /users/:id, not /users/:id? as generated.

This is reproducible with 1.0.0-rc3 version of nuxt as I tested today.

This question is available on Nuxt.js community (#c1076)