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(pages): routes HMR #8941

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open

feat(pages): routes HMR #8941

wants to merge 1 commit into from

Conversation

antfu
Copy link
Member

@antfu antfu commented Nov 13, 2022

πŸ”— Linked issue

❓ Type of change

  • πŸ“– Documentation (updates to the documentation or readme)
  • 🐞 Bug fix (a non-breaking change that fixes an issue)
  • πŸ‘Œ Enhancement (improving an existing functionality like performance)
  • ✨ New feature (a non-breaking change that adds functionality)
  • ⚠️ Breaking change (fix or feature that would cause existing functionality to change)

πŸ“š Description

This make adding/removing routes HMR, without requiring a full reload.

πŸ“ Checklist

  • I have linked an issue or discussion.
  • I have updated the documentation accordingly.

@codesandbox
Copy link

codesandbox bot commented Nov 13, 2022

CodeSandbox logoCodeSandbox logoΒ  Open in CodeSandbox Web Editor | VS Code | VS Code Insiders

@netlify
Copy link

netlify bot commented Nov 13, 2022

βœ… Deploy Preview for nuxt3-docs ready!

Name Link
πŸ”¨ Latest commit 78b7c7b
πŸ” Latest deploy log https://app.netlify.com/sites/nuxt3-docs/deploys/6370455604777a00091bdda6
😎 Deploy Preview https://deploy-preview-8941--nuxt3-docs.netlify.app
πŸ“± Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify site settings.

@antfu
Copy link
Member Author

antfu commented Nov 13, 2022

/cc @posva πŸ‘€

})
let clearRoutes = router.addRoute({ path: '', children: routes })
Copy link
Collaborator

@posva posva Nov 13, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

shouldn't this be

Suggested change
let clearRoutes = router.addRoute({ path: '', children: routes })
let clearRoutes = router.addRoute({ path: '/', children: routes })

did you find any trouble with it? The router is more preared to take paths that start with / when they have no parent

I want to know because if you find a problem, it's likely a vue-router bug

'if (import.meta.hot) {',
' import.meta.hot.accept((mod) => {',
' onUpdate._fn?.(mod.default)',
' mod.onUpdate._fn = onUpdate._fn',
' })',
'}'
Copy link
Member

@danielroe danielroe Nov 14, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

do we need to add an equivalent for webpack, like the following?

if (import.meta.webpackHot) {
import.meta.webpackHot.accept('#build/app.config.mjs', () => {
applyHMR(__appConfig)
})
}

Copy link
Member Author

@antfu antfu Nov 14, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I know nothing about Webpack HMR πŸ˜…, feel free to push code you think necessary.

Copy link
Member

@danielroe danielroe Nov 14, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ok, will have a look later!

clearRoutes = router.addRoute({ path: '', children: routes })
})
})
}
Copy link
Member

@danielroe danielroe Nov 14, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

as above - do we need to add a webpack equivalent?

...imports,
`export default ${routes}`,
'export function onUpdate(fn) { onUpdate._fn = fn }',
'if (import.meta.hot) {',
Copy link
Member

@danielroe danielroe Nov 14, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It might be worth also adding a test for process.dev

Copy link
Member Author

@antfu antfu Nov 14, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I supposed it would be tree-shaken in production? But sure I can do that.

nuxtApp.vueApp.use(router)

// Routes HMR
if (import.meta.hot) {
Copy link
Member

@danielroe danielroe Nov 14, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

as above, it might be worth also adding a test for process.dev

Copy link
Collaborator

@posva posva Nov 14, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

isn't import.meta.hot automatically false in production too?

})
let clearRoutes = router.addRoute({ path: '', children: routes })
Copy link
Member

@danielroe danielroe Nov 14, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is there any runtime overhead to doing this this way? if so, I guess we can use process.dev to only do this in dev mode?

Copy link
Collaborator

@posva posva Nov 14, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It shouldn't have too much of an overhead but I haven't benchmarked it. This is the code that runs with this: https://github.com/vuejs/router/blob/main/packages/router/src/matcher/index.ts#L161-L170

If it does end up having a bad impact on performance, then another more verbose version that inlines the routes at router creation in production and adds each route individually in dev would work too:

let clearRoutes = routes.map(route => router.addRoute(route))

clearRoutes.forEach(removeRoute => removeRoute())

@danielroe
Copy link
Member

danielroe commented Nov 14, 2022

Thank you! This will be amazing πŸš€

@danielroe danielroe added enhancement New feature or request 🍰 p2-nice-to-have Priority 2: nothing is broken but it's worth addressing pages labels Nov 14, 2022 — with Volta.net
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request 🍰 p2-nice-to-have Priority 2: nothing is broken but it's worth addressing pages
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants