File tree Expand file tree Collapse file tree 3 files changed +35
-5
lines changed Expand file tree Collapse file tree 3 files changed +35
-5
lines changed Original file line number Diff line number Diff line change @@ -9,6 +9,7 @@ declare module 'vue-router/auto-routes' {
9
9
/**
10
10
* Setups hot module replacement for routes.
11
11
* @param router - The router instance
12
+ * @param hotUpdateCallback - Callback to be called after replacing the routes and before the navigation
12
13
* @example
13
14
* ```ts
14
15
* import { createRouter, createWebHistory } from 'vue-router'
@@ -22,7 +23,10 @@ declare module 'vue-router/auto-routes' {
22
23
* }
23
24
* ```
24
25
*/
25
- export function handleHotUpdate ( router : Router ) : void
26
+ export function handleHotUpdate (
27
+ router : Router ,
28
+ hotUpdateCallback ?: ( newRoutes : RouteRecordRaw [ ] ) => void
29
+ ) : void
26
30
}
27
31
28
32
declare module 'vue-router' {
Original file line number Diff line number Diff line change @@ -7,8 +7,21 @@ export const router = createRouter({
7
7
routes,
8
8
} )
9
9
10
+ function addRedirects ( ) {
11
+ router . addRoute ( {
12
+ path : '/new-about' ,
13
+ redirect : '/about?from=hoho' ,
14
+ } )
15
+ }
16
+
10
17
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 ( )
12
25
}
13
26
14
27
// manual extension of route types
Original file line number Diff line number Diff line change @@ -19,6 +19,7 @@ import { generateVueRouterProxy as _generateVueRouterProxy } from '../codegen/vu
19
19
import { definePageTransform , extractDefinePageNameAndPath } from './definePage'
20
20
import { EditableTreeNode } from './extendRoutes'
21
21
import { isPackageExists as isPackageInstalled } from 'local-pkg'
22
+ import { ts } from '../utils'
22
23
23
24
export function createRoutesContext ( options : ResolvedOptions ) {
24
25
const { dts : preferDTS , root, routesFolder } = options
@@ -186,10 +187,11 @@ export function createRoutesContext(options: ResolvedOptions) {
186
187
importsMap
187
188
) } \n`
188
189
189
- let hmr = `
190
- export function handleHotUpdate(_router) {
190
+ let hmr = ts `
191
+ export function handleHotUpdate(_router, _hotUpdateCallback ) {
191
192
if (import.meta.hot) {
192
193
import.meta.hot.data.router = _router
194
+ import.meta.hot.data.router_hotUpdateCallback = _hotUpdateCallback
193
195
}
194
196
}
195
197
@@ -204,7 +206,18 @@ if (import.meta.hot) {
204
206
for (const route of mod.routes) {
205
207
router.addRoute(route)
206
208
}
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
+ })
208
221
})
209
222
}
210
223
`
You can’t perform that action at this time.
0 commit comments