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

feat(vue-app): export router options #6231

Merged
merged 4 commits into from
Aug 14, 2019
Merged

feat(vue-app): export router options #6231

merged 4 commits into from
Aug 14, 2019

Conversation

ricardogobbosouza
Copy link
Contributor

@ricardogobbosouza ricardogobbosouza commented Aug 14, 2019

Export router options

Types of changes

  • Bug fix (a non-breaking change which fixes an issue)
  • New feature (a non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to change)

Description

Sometimes we need to use the @nuxtjs/router module for more custom creation, in which case vue-router is initialized twice.

nuxt-community/router-module#56

Checklist:

  • My change requires a change to the documentation.
  • I have updated the documentation accordingly. (PR: #)
  • I have added tests to cover my changes (if not applicable, please state why)
  • All new and existing tests are passing.

@ricardogobbosouza ricardogobbosouza changed the title feat(vue-app): export router options feat(vue-app): custom options in createRouter Aug 14, 2019
return new Router(routerOptions())
export async function createRouter(ssrContext, options = routerOptions) {
if (typeof options === 'function') {
options = await options(ssrContext)
Copy link
Member

@pi0 pi0 Aug 14, 2019

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.

Copy link
Contributor Author

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

Copy link
Member

@pi0 pi0 Aug 14, 2019

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.

Copy link
Contributor Author

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

Copy link
Member

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?

Copy link
Contributor Author

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

Copy link
Member

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?

Copy link
Contributor Author

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

Copy link
Contributor Author

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

Copy link
Contributor Author

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-io
Copy link

codecov-io commented Aug 14, 2019

Codecov Report

Merging #6231 into dev will not change coverage.
The diff coverage is n/a.

Impacted file tree graph

@@           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
Flag Coverage Δ
#e2e 100% <ø> (ø) ⬆️
#fixtures 50.75% <ø> (ø) ⬆️
#unit 92.44% <ø> (ø) ⬆️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 35ba655...17c9104. Read the comment docs.

@ricardogobbosouza ricardogobbosouza changed the title feat(vue-app): custom options in createRouter feat(vue-app): export router options Aug 14, 2019
pi0
pi0 previously approved these changes Aug 14, 2019
fallback: <%= router.fallback %>
}

export async function createRouter() {
Copy link
Member

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 :)

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

Successfully merging this pull request may close these issues.

None yet

4 participants