Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Route priorities #38

Closed
pokorson opened this issue Aug 5, 2016 · 9 comments · May be fixed by #86
Closed

Route priorities #38

pokorson opened this issue Aug 5, 2016 · 9 comments · May be fixed by #86

Comments

@pokorson
Copy link

pokorson commented Aug 5, 2016

Hi, in my project I along with route path like /welcome, /sign_in (specific string matched) I have also path /:id. I've found only way to match this route is to place it on top of routes
so it looks like:

const routes = [
  { path: '/:id', name: 'idRoute' },
  { path: '/welcome', name: 'welcome' },
  { path: '/sign_in', name: 'signIn' }
];

And it seems kind of strange for routes to be resolved bottom->top, wouldn't it make more sense to go top->bottom? So it checks all routes first and if none matches then it comes to /:id ?

@frenzzy
Copy link
Member

frenzzy commented Aug 5, 2016

The ordering works the same way as in express. So :id param matches everything and next route will not be called. Playground: https://jsfiddle.net/frenzzy/ko22k9cg/

@pokorson
Copy link
Author

pokorson commented Aug 5, 2016

@frenzzy you're right routes are resolved top->bottom, and after further investigation my problem is different. It look like route matching is not stopped after first matched route. so with

const routes = [
  { path: '/welcome', name: 'welcome' },
  { path: '/sign_in', name: 'signIn' },
  { path: '/:id', name: 'idRoute' }
];

and path /welcome it matches both /welcome and /:id

@frenzzy
Copy link
Member

frenzzy commented Aug 5, 2016

Yes, because the action method of a route will have to return anything different from undefined to stop iterating and finish routing process.

const anyRoute = {
  path: '/'
  action() {
    if (condition) {
      return undefined // skip this route
    }
    return 'page'
  }
}

Playground: https://jsfiddle.net/frenzzy/ko22k9cg/1/

@pokorson
Copy link
Author

pokorson commented Aug 5, 2016

I followed example from docs

const routes = [
  { path: '/one', action: ({ render }) => render(<h1>Page One</h1>) },
  { path: '/two', action: ({ render }) => render(<h1>Page Two</h1>) }
];

function render(component) {
  return Promise(resolve => {
    ReactDOM.render(component, document.body, resolve);
  });
}

so all of my actions are returning Promise but still this error appears.

@koistya
Copy link
Member

koistya commented Aug 5, 2016

const container = document.getElementById('root');

function render(component) {
  return Promise((resolve, reject) => {
    try {
      ReactDOM.render(component, container, () => resolve(true /* something not falsy */));
    } catch (err) { reject(err); }
  });
}

@frenzzy
Copy link
Member

frenzzy commented Aug 5, 2016

Router waits until your promise will be finished and if promise returns undefined Router skips current route.

@pokorson
Copy link
Author

pokorson commented Aug 5, 2016

@koistya that corrects the issue, thanks! Would be good to include this in documentation.

@frenzzy
Copy link
Member

frenzzy commented Aug 5, 2016

By the way I think it is much better to start render strictly after the navigation process completely finished. This will help to avoid a lot of troubles like this and make the code easier to understand.

const routes = [
  { path: '/one', action() { return <div>One</div> } },
  { path: '/two', action() { return <div>Two</div> } },
  { path: '/*', action() { return <div>Not Found</div> } },
];

UniversalRouter
  .resolve(routes, { path: '/one' })
  .then(component =>
    ReactDOM.render(component, document.getElementById('root'))
  )

Playground:

@frenzzy
Copy link
Member

frenzzy commented Oct 20, 2016

Closing this as resolved. Please reopen if needed.

@frenzzy frenzzy closed this as completed Oct 20, 2016
@frenzzy frenzzy mentioned this issue Mar 29, 2017
20 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants