From bbc458a430c193ca227f464088c1a21ddbdf1785 Mon Sep 17 00:00:00 2001 From: Dominik Schmidt Date: Thu, 19 Jan 2023 00:16:34 +0100 Subject: [PATCH] Vue 3: Fix encoded slashes in urls This is a follow up to https://github.com/owncloud/web/pull/8215 where we introduced a regression, so that slashes in file paths were encoded in urls. --- .../web-runtime/src/router/patchCleanPath.ts | 35 +++++++++++++------ 1 file changed, 24 insertions(+), 11 deletions(-) diff --git a/packages/web-runtime/src/router/patchCleanPath.ts b/packages/web-runtime/src/router/patchCleanPath.ts index 4c811370cfe..e50bf0e1265 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' // type: patch // temporary patch till we have upgraded web to the latest vue router which make this obsolete @@ -7,34 +6,48 @@ 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 || '') + const bindResolve = router.resolve.bind(router) router.resolve = ( raw: RouteLocationRaw, currentLocation?: RouteLocationNormalizedLoaded ): RouteLocation & { href: string } => { - const bindResolve = bindResolver(raw, currentLocation) - - if (!get(bindResolve, 'meta.patchCleanPath', false)) { - return bindResolve + const resolved = bindResolve(raw, currentLocation) + if (resolved.meta?.patchCleanPath !== true) { + return resolved } return { - ...bindResolve, - href: cleanPath(bindResolve.href), - path: cleanPath(bindResolve.path), - fullPath: cleanPath(bindResolve.fullPath) + ...resolved, + href: cleanPath(resolved.href), + path: cleanPath(resolved.path), + fullPath: cleanPath(resolved.fullPath) + } + } + + const routerMethodFactory = (method) => (to) => { + const resolved = router.resolve(to) + if (resolved.meta?.patchCleanPath !== true) { + return method(to) } + + return method({ + path: cleanPath(resolved.fullPath), + query: resolved.query + }) } + router.push = routerMethodFactory(router.push.bind(router)) + router.replace = routerMethodFactory(router.replace.bind(router)) + return router }