kriasoft / universal-router

A simple middleware-style router for isomorphic JavaScript web apps
https://www.kriasoft.com/universal-router/
MIT License
1.7k stars 104 forks source link

Skip nested routes when middleware route returns null #140

Closed frenzzy closed 6 years ago

frenzzy commented 6 years ago

Note that if the middleware action returns null then the router will skip all nested routes and go to the next sibling route. But if the action is missing or returns undefined then the router will try to match the child routes.

This can be useful for permissions check:

const middlewareRoute = {
  path: '/admin',
  action(context) {
    if (!context.user) {
      return null; // route does not match (skip all /admin* routes)
    }
    if (context.user.role !== 'Admin') {
      return 'Access denied!'; // return a page (for any /admin* urls)
    }
    return undefined; // or `return context.next();` - try to match child routes
  },
  children: [/* admin routes here */],
};

The change is breaking just because users may return null from their custom resolveRoute function instead of undefined in cases when route should not match.

coveralls commented 6 years ago

Coverage Status

Coverage remained the same at 100.0% when pulling 3bd30a113c533873ddced311614730b0630a8f22 on frenzzy:skip-nested-routes into 85370662a30cf5e5cb1a7d8ba85e281401934d5e on kriasoft:master.

coveralls commented 6 years ago

Coverage Status

Coverage remained the same at 100.0% when pulling ed77e6619cc6473753b0ee11346f25499cb7effc on frenzzy:skip-nested-routes into 85370662a30cf5e5cb1a7d8ba85e281401934d5e on kriasoft:master.

coveralls commented 6 years ago

Coverage Status

Coverage remained the same at 100.0% when pulling 1c36ed8ef23fd351025ff0b42132aa3cd33c6b5c on frenzzy:skip-nested-routes into 85370662a30cf5e5cb1a7d8ba85e281401934d5e on kriasoft:master.