Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[full-ci] Vue 3: Fix encoded slashes in urls #8269

Merged
merged 1 commit into from
Jan 19, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 24 additions & 11 deletions packages/web-runtime/src/router/patchCleanPath.ts
Original file line number Diff line number Diff line change
@@ -1,40 +1,53 @@
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
// 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 || '')

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
}