-
-
Notifications
You must be signed in to change notification settings - Fork 4.8k
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
feat(vue-app): export router options #6231
Conversation
packages/vue-app/template/router.js
Outdated
return new Router(routerOptions()) | ||
export async function createRouter(ssrContext, options = routerOptions) { | ||
if (typeof options === 'function') { | ||
options = await options(ssrContext) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
await is unnecessary and costly. we can set default to options = routerOptions()
and when calling with custom arg, await before calling createRouter
:)
PS: ssrContext is passed and then reused which we can omit.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can't pass ssrContext
? I thought it might be useful for some kind of customization
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hmm actually what we are doing is:
- passing ssrContext and routerOptions to createRouter
- Let createRouter call options with ssrContext (with extra await)
- Use it to create router instance
This can be simply:
- Using ssrContext to create options object (can be await or not)
- Pass it to createRouter
createRouter(options = routerOptions)
These are identical in terms of customization. Just refactor on function to make it simpler and lighter.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
But this can generate a break change for those who use ssrContext
in createRouter
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not sure. At least currently nuxt accepts no arguments for createRouter
. Maybe I'm misunderstanding how are you going to use it for router-module. Can you please provide more details about the usage of (new) exported function?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I just wanted to have access to router options without having to boot
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is see. So isn't just exporting routerOptions
functon enough to import from defaultRouter
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Exactly, I just need to export routeOptions
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
When we enable keepDefaultRouter
https://github.com/nuxt-community/router-module#accessing-default-router
The default router is created and I return a new router, ie the router is being started twice, I want to avoid this by using the default router options
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We can simplify by doing this:
export const routerOptions = {
...
}
export async function createRouter(ssrContext) {
return new Router(routerOptions)
}
Codecov Report
@@ Coverage Diff @@
## dev #6231 +/- ##
=======================================
Coverage 95.75% 95.75%
=======================================
Files 80 80
Lines 2660 2660
Branches 684 684
=======================================
Hits 2547 2547
Misses 97 97
Partials 16 16
Continue to review full report at Codecov.
|
packages/vue-app/template/router.js
Outdated
fallback: <%= router.fallback %> | ||
} | ||
|
||
export async function createRouter() { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think async is unnecessary :)
Export router options
Types of changes
Description
Sometimes we need to use the
@nuxtjs/router
module for more custom creation, in which casevue-router
is initialized twice.nuxt-community/router-module#56
Checklist: