visionmedia / page.js

Micro client-side router inspired by the Express router
http://visionmedia.github.com/page.js
7.67k stars 687 forks source link

Redirect support params #479

Open santomegonzalo opened 6 years ago

santomegonzalo commented 6 years ago

Hello, I'm having the following issue.

path(to, from) works perfects when I don't have param on the URL. But for this case page('/user/:userId/settings', '/user/:userId') will not work because when I navigate to page('/user/100/settings') page will redirect to page('/user/:userId')... of course this is not the expected behaivour.

I ended up doing the following:

page(from, (ctx) => {
  let urlTo = to;

  Object.keys(ctx.params).forEach((paramKey) => {
    urlTo = urlTo.replace(`:${paramKey}`, ctx.params[paramKey]);
  });
  setTimeout(() => {
    page.replace(urlTo);
  }, 0);
});

This is working perfect but it will be nice to have a solution from page.js directly...

If you want I could do a PullRequest with this change.

Thanks!