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

dynamically add routes #117

Closed
FredericHeem opened this issue Sep 14, 2017 · 2 comments
Closed

dynamically add routes #117

FredericHeem opened this issue Sep 14, 2017 · 2 comments

Comments

@FredericHeem
Copy link

Would it be possible to dynamically add routes found in a chunk loaded through import?
When a route is loaded asynchronously, the router context passed is a copy so the original context cannot be modified at the moment.

@frenzzy
Copy link
Member

frenzzy commented Sep 14, 2017

Basic example (load routes when requested):

const route = {
  path: '/admin',
  children: [], // place for lazy routes
  async action(context) {
    const routes = await import('./admin/routes.js'); // load routes asynchronously
    context.route.children = routes; // dynamically add routes
    delete context.route.action; // execute action method only once
    return context.next(); // go to just loaded routes
  }
}

Also you can dynamically add new routes at any time:

routes.push(newRoute)

Your routes are just plain javascript object and arrays, you can interact with them as you like.

Would you describe your use case more or provide examples, why do you want to modify context?

@FredericHeem
Copy link
Author

Thanks a lot, it works perfectly, see the full code at https://github.com/FredericHeem/starhackit/blob/master/client/src/app/router.js

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

No branches or pull requests

2 participants