-
Notifications
You must be signed in to change notification settings - Fork 159
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
This is a follow up to #8215 where we introduced a regression, so that slashes in file paths were encoded in urls.
- Loading branch information
Showing
1 changed file
with
14 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,40 +1,34 @@ | ||
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 | ||
// this takes care that routes like 'foo/bar/baz' which by default would be converted to 'foo%2Fbar%2Fbaz' stay as they are | ||
// 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 | ||
} |