Skip to content

Commit 170df11

Browse files
committed
feat: allow HMR Callback
Close #503
1 parent b734d9a commit 170df11

File tree

3 files changed

+35
-5
lines changed

3 files changed

+35
-5
lines changed

client.d.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ declare module 'vue-router/auto-routes' {
99
/**
1010
* Setups hot module replacement for routes.
1111
* @param router - The router instance
12+
* @param hotUpdateCallback - Callback to be called after replacing the routes and before the navigation
1213
* @example
1314
* ```ts
1415
* import { createRouter, createWebHistory } from 'vue-router'
@@ -22,7 +23,10 @@ declare module 'vue-router/auto-routes' {
2223
* }
2324
* ```
2425
*/
25-
export function handleHotUpdate(router: Router): void
26+
export function handleHotUpdate(
27+
router: Router,
28+
hotUpdateCallback?: (newRoutes: RouteRecordRaw[]) => void
29+
): void
2630
}
2731

2832
declare module 'vue-router' {

playground/src/router.ts

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,21 @@ export const router = createRouter({
77
routes,
88
})
99

10+
function addRedirects() {
11+
router.addRoute({
12+
path: '/new-about',
13+
redirect: '/about?from=hoho',
14+
})
15+
}
16+
1017
if (import.meta.hot) {
11-
handleHotUpdate(router)
18+
handleHotUpdate(router, (routes) => {
19+
console.log('🔥 HMR with', routes)
20+
addRedirects()
21+
})
22+
} else {
23+
// production
24+
addRedirects()
1225
}
1326

1427
// manual extension of route types

src/core/context.ts

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import { generateVueRouterProxy as _generateVueRouterProxy } from '../codegen/vu
1919
import { definePageTransform, extractDefinePageNameAndPath } from './definePage'
2020
import { EditableTreeNode } from './extendRoutes'
2121
import { isPackageExists as isPackageInstalled } from 'local-pkg'
22+
import { ts } from '../utils'
2223

2324
export function createRoutesContext(options: ResolvedOptions) {
2425
const { dts: preferDTS, root, routesFolder } = options
@@ -186,10 +187,11 @@ export function createRoutesContext(options: ResolvedOptions) {
186187
importsMap
187188
)}\n`
188189

189-
let hmr = `
190-
export function handleHotUpdate(_router) {
190+
let hmr = ts`
191+
export function handleHotUpdate(_router, _hotUpdateCallback) {
191192
if (import.meta.hot) {
192193
import.meta.hot.data.router = _router
194+
import.meta.hot.data.router_hotUpdateCallback = _hotUpdateCallback
193195
}
194196
}
195197
@@ -204,7 +206,18 @@ if (import.meta.hot) {
204206
for (const route of mod.routes) {
205207
router.addRoute(route)
206208
}
207-
router.replace('')
209+
// call the hotUpdateCallback for custom updates
210+
import.meta.hot.data.router_hotUpdateCallback?.(mod.routes)
211+
const route = router.currentRoute.value
212+
router.replace({
213+
...route,
214+
// NOTE: we should be able to just do ...route but the router
215+
// currently skips resolving and can give errors with renamed routes
216+
// so we explicitly set remove matched and name
217+
name: undefined,
218+
matched: undefined,
219+
force: true
220+
})
208221
})
209222
}
210223
`

0 commit comments

Comments
 (0)