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

Feature: add option keepDefaultRouter #16



Copy link

@IlyaSemenov IlyaSemenov commented May 19, 2018

In my project, I want to keep pages/ directory but I want to customize routes per request.

My real-life use case is:*** should come from pages/*.vue
https://****** (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 the pages/ 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. Custom router.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 only createRouter but also routerOptions). 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.

@Atinux Atinux merged commit 163f69c into nuxt-community:master Aug 6, 2018
Copy link

@Atinux Atinux commented Aug 6, 2018

Hey @IlyaSemenov

Sorry for the delay, great idea BTW, thanks for adding the tests as well!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
None yet
None yet

Successfully merging this pull request may close these issues.

None yet

2 participants