Skip to content

Commit

Permalink
Vue 3: Fix encoded slashes in urls
Browse files Browse the repository at this point in the history
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
dschmidt committed Jan 18, 2023
1 parent 7fa787c commit faba482
Showing 1 changed file with 14 additions and 20 deletions.
34 changes: 14 additions & 20 deletions packages/web-runtime/src/router/patchCleanPath.ts
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
}

0 comments on commit faba482

Please sign in to comment.