diff --git a/packages/web-runtime/src/router/patchCleanPath.ts b/packages/web-runtime/src/router/patchCleanPath.ts index 4c811370cfe..4dc2d91ac9b 100644 --- a/packages/web-runtime/src/router/patchCleanPath.ts +++ b/packages/web-runtime/src/router/patchCleanPath.ts @@ -1,5 +1,4 @@ -import { RouteLocation, RouteLocationNormalizedLoaded, RouteLocationRaw, Router } from 'vue-router' -import get from 'lodash-es/get' +import { Router } from 'vue-router' // type: patch // temporary patch till we have upgraded web to the latest vue router which make this obsolete @@ -7,34 +6,29 @@ import get from 'lodash-es/get' // should immediately go away and be removed after finalizing the update // to apply the patch to a route add meta.patchCleanPath = true to it // to patch needs to be enabled on a route level, to do so add meta.patchCleanPath = true property to the route -// c.f. https://github.com/vuejs/router/issues/ 1638 +// c.f. https://github.com/vuejs/router/issues/1638 export const patchRouter = (router: Router) => { - const bindResolver = router.resolve.bind(router) const cleanPath = (route) => [ ['%2F', '/'], ['//', '/'] ].reduce((path, rule) => path.replaceAll(rule[0], rule[1]), route || '') - router.resolve = ( - raw: RouteLocationRaw, - currentLocation?: RouteLocationNormalizedLoaded - ): RouteLocation & { - href: string - } => { - const bindResolve = bindResolver(raw, currentLocation) - - if (!get(bindResolve, 'meta.patchCleanPath', false)) { - return bindResolve + const routerMethodFactory = (method) => (to) => { + const resolved = router.resolve(to) + if (resolved.meta?.patchCleanPath !== true) { + return method(to) } - return { - ...bindResolve, - href: cleanPath(bindResolve.href), - path: cleanPath(bindResolve.path), - fullPath: cleanPath(bindResolve.fullPath) - } + const { fullPath, query } = resolved + return method({ + path: cleanPath(fullPath), + query + }) } + router.push = routerMethodFactory(router.push.bind(router)) + router.replace = routerMethodFactory(router.replace.bind(router)) + return router }