Next-routes: Optional params

Created on 1 May 2018  路  4Comments  路  Source: fridays/next-routes

Is there a syntax for optional params?

// Commonly used syntax
Router.add('pageID', '/page-path/:optional1?/:optional2?')

Did I miss something in the docs or just have not thought of an implementation to achieve this?

Most helpful comment

Adding ? is indeed the answer. https://github.com/fridays/next-routes/issues/86#issuecomment-323854224 This works with multiple optional params.

routes.add({
  name: 'some-cool-route',
  pattern: '/foo/bar/:id/:banana?/:anchovy?/:lemon?',
  page: 'superpage',
})

Router.pushRoute('some-cool-route', {
  id: 'asdf',
  lemon: 'boop',
})

All 4 comments

Why did you close it @jchiellini ?

Hi @jchiellini,

Could you provide the answer?

I'm facing the same issue. I tested multiple solutions from path-to-regexp but I'm not able to match the route with two optionals parameters in the row.

Thanks!

Adding ? is indeed the answer. https://github.com/fridays/next-routes/issues/86#issuecomment-323854224 This works with multiple optional params.

routes.add({
  name: 'some-cool-route',
  pattern: '/foo/bar/:id/:banana?/:anchovy?/:lemon?',
  page: 'superpage',
})

Router.pushRoute('some-cool-route', {
  id: 'asdf',
  lemon: 'boop',
})

Adding ? always matches the index route

.add({
    name:'/',
    pattern:'/:country?/:lang?',
    page:'index'
})
.add({
    name:'our-firm',
    pattern:'/:country?/:lang?',
    page:'our-firm'
})

On navigation to http://localhost:3000/en/our-firm it opens index page but it should open our-firm

Was this page helpful?
0 / 5 - 0 ratings

Related issues

apapacy picture apapacy  路  4Comments

khankuan picture khankuan  路  7Comments

JiiB picture JiiB  路  4Comments

adekbadek picture adekbadek  路  4Comments

bliitzkrieg picture bliitzkrieg  路  4Comments