Nuxt.js: params not matching with route

Created on 17 Aug 2020  路  3Comments  路  Source: nuxt/nuxt.js

Versions

  • nuxt: v2.14.3
  • node: v12.16.1

Reproduction

when using the following directory structure

dir: blog/
dir: blog/_country/
dir: blog/_country/_year
file: blog/_country/_year/index.vue
file: blog/_country/_year/_slug.vue

and the following content of file blog/_country/_year/index.vue

async asyncData({ params }) {
    console.log(params)
}

Steps to reproduce

go to http://localhost:3000/blog/new-zealand

What is Expected?

{
  country: 'new-zealand',
  year: undefined,
}

What is actually happening?

{
  country: undefined,
  year: 'new-zealand',
}

params are not correctly matched with _params in folders/files

bug-report

Most helpful comment

I believe this is the correct behavior based on your directory structure.
You should create file blog/_country/index.vue to handle /blog/new-zealand as you expected.

Explanations:

With your structure router will create two routes.

  • /blog/:country?/:year
  • /blog/:country?/:year?/:slug

On the page /blog/new-zealand router matches with first route. As you see :year params is required and :country is optional, so router use new-zealand for :year and left :country empty

All 3 comments

FYI: this works as expected in [email protected] and [email protected] but has stopped working from 2.14.1 onwards

I believe this is the correct behavior based on your directory structure.
You should create file blog/_country/index.vue to handle /blog/new-zealand as you expected.

Explanations:

With your structure router will create two routes.

  • /blog/:country?/:year
  • /blog/:country?/:year?/:slug

On the page /blog/new-zealand router matches with first route. As you see :year params is required and :country is optional, so router use new-zealand for :year and left :country empty

@farnabaz Thanks for that clear explanation.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

maicong picture maicong  路  3Comments

shyamchandranmec picture shyamchandranmec  路  3Comments

surmon-china picture surmon-china  路  3Comments

vadimsg picture vadimsg  路  3Comments

mikekidder picture mikekidder  路  3Comments