fridays / next-routes

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

Is it possible to use default param values in routes.js #171

Closed skywickenden closed 1 year ago

skywickenden commented 6 years ago

I have

routes
.add('page', '/page/:slug')

Which directs pages such as /page/about-us to /pages/page.js with param slug equal to about-us

I want to also add a route for /contact-us which also routes through /pages/page.js with a slug of contact. Is there a way of achieving this?

skywickenden commented 6 years ago

I solved this with a hack:

I used this as the route:

.add('contact-us', '/contact-us', 'reroute|/page|{"slug":"contact-us"}')

and then in server.js I added a custom handler

const handler = routes.getRequestHandler(app, ({req, res, route, query}) => {
  if (route.page.substring(0, 8) === '/reroute') {
    const routeParts = route.page.split('|')
    app.render(req, res, routeParts[1], JSON.parse(routeParts[2]))
  } else {
    app.render(req, res, route.page, query)
  }
})

I would be great if it was possible to send a customization object through for the custom handler so this could be less hackish. I can't be the only one who has SEO requirements that make direct url to filename conversions harder than they should be!

If this is welcome I'll put a pull request together. Essentially just a fourth parameter in the route definition that gets passed through.