Feature: add option keepDefaultRouter #16
Merged
+107
−5
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.
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/*.vue
https://***.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/router
with fully custom routes, I lose thepages/
directory parser,scrollBehavior
and 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/router
module is created with{ keepDefaultRouter: true }
(false by default), the default router will not be thrown away but kept in.nuxt/defaultRouter.js
. Customrouter.js
may 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.js
exported not onlycreateRouter
but 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.