fridays / next-routes

Universal dynamic routes for Next.js
MIT License
2.47k stars 230 forks source link

Optional params #168

Closed jchiellini closed 6 years ago

jchiellini commented 6 years ago

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?

typeofweb commented 6 years ago

Why did you close it @jchiellini ?

DonovanCharpin commented 5 years ago

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!

reergymerej commented 5 years ago

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',
})
murtazamzk commented 4 years ago

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