Created
March 14, 2019 07:00
-
-
Save meyt/1c625e962d5c665a4d547e5c1a23a7c2 to your computer and use it in GitHub Desktop.
Force refresh current route on vue-router
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
/* | |
Problem: When user click on a link that refers to current path, | |
nothing happen. in vue-router logic, same path must ignore. | |
Expected: As browsers UX on old-fashion websites, user expects | |
something happen when click on a link, like refresh the current | |
route. | |
Solution: We hack the vue-router push method | |
(thanks to vue-router clean code) to append a query-string | |
on same routes, its mean we have something new on for vue-router. | |
Note: be careful where using <router-link> `exact` attribute on | |
parent routes like '/'. | |
Note: to inform the nuxt `async-data` or `fetch` about changes, | |
use component level `watchQuery: ['ct']` on target page. | |
Note: This is a plugin for nuxt. | |
Warning: it depend on vue-router internal source code and may break | |
on next releases. | |
*/ | |
export default async ({ app }) => { | |
app.router.push = function (location, onComplete, onAbort) { | |
const current = app.router.history.current | |
const route = app.router.match(location, current) | |
if (current.path === route.path) { | |
// ClickTime | |
route.query.ct = parseInt(Math.random() * 999).toString() | |
} | |
app.router.history.push({ | |
path: route.path, | |
query: route.query, | |
params: route.params | |
}, onComplete, onAbort) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment