Skip to content

Feature: add option keepDefaultRouter#16

Merged
atinux merged 1 commit into
nuxt-community:masterfrom
IlyaSemenov:features/keepDefaultRouter
Aug 6, 2018
Merged

Feature: add option keepDefaultRouter#16
atinux merged 1 commit into
nuxt-community:masterfrom
IlyaSemenov:features/keepDefaultRouter

Conversation

@IlyaSemenov

Copy link
Copy Markdown
Contributor

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 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
@atinux

atinux commented Aug 6, 2018

Copy link
Copy Markdown
Contributor

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants