Feature: add option keepDefaultRouter#16
Merged
atinux merged 1 commit intoAug 6, 2018
Conversation
Contributor
|
Hey @IlyaSemenov Sorry for the delay, great idea BTW, thanks for adding the tests as well! |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
In my project, I want to keep
pages/directory but I want to customize routes per request.My real-life use case is:
https://myproject.com/*** should come from
pages/*.vuehttps://***.myproject.com/*** (subdomains) should come from
pages/app/*.vue(entirely different set of pages)Unfortunately, Nuxt.js lib/app/index.js and lib/app/router.js don't provide any suitable hooks.
If I use
@nuxtjs/routerwith fully custom routes, I lose thepages/directory parser,scrollBehaviorand possibly other goodies. I am happy to use the default routes generator, I just want to have some post-processing.This PR is an attempt to solve the problem. How it works: if
@nuxtjs/routermodule is created with{ keepDefaultRouter: true }(false by default), the default router will not be thrown away but kept in.nuxt/defaultRouter.js. Customrouter.jsmay then import it and reuse its options when creating a new Router instance, then amend, filter or modify routes or other router options. I included a simple unit test that shows how it may work (it's more complicated in real life, of course).This solution is surely not perfect. Instantiating default router simply to access its options is suboptimal (it would rather be better if Nuxt's
lib/app/router.jsexported not onlycreateRouterbut alsorouterOptions). Filtering and modifying rules per request is also suboptimal, in my case I only need two static subsets and it would be great if I could emit them with the builder rather than do that in runtime for each request. But at least it's something.