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

Fix the redundant navigation error #2286

Merged
merged 5 commits into from
Jun 21, 2022
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
38 changes: 35 additions & 3 deletions src/renderer/router/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,41 @@ import Playlist from '../views/Playlist/Playlist.vue'
import Channel from '../views/Channel/Channel.vue'
import Watch from '../views/Watch/Watch.vue'

Vue.use(Router)
class CustomRouter extends Router {
push(location) {
// only navigates if the location is not identical to the current location

const router = new Router({
const currentQueryUSP = new URLSearchParams(router.currentRoute.query)
let newPath = ''
let newQueryUSP = new URLSearchParams()

if (typeof location === 'string') {
if (location.includes('?')) {
const urlParts = location.split('?')
newPath = urlParts[0]
newQueryUSP = new URLSearchParams(urlParts[1])
} else {
newPath = location
// newQueryUSP already empty
}
} else {
newPath = location.path
newQueryUSP = new URLSearchParams(location.query)
}

const pathsAreDiff = router.currentRoute.path !== newPath
// Comparing `URLSearchParams` objects directly will always be different
const queriesAreDiff = newQueryUSP.toString() !== currentQueryUSP.toString()

if (pathsAreDiff || queriesAreDiff) {
return super.push(location)
}
}
}

Vue.use(CustomRouter)

const router = new CustomRouter({
routes: [
{
path: '/',
Expand Down Expand Up @@ -142,7 +174,7 @@ const router = new Router({
component: Watch
}
],
scrollBehavior (to, from, savedPosition) {
scrollBehavior(to, from, savedPosition) {
return new Promise((resolve, reject) => {
setTimeout(() => {
if (savedPosition !== null) {
Expand Down