Skip to content

Commit effb437

Browse files
susnuxSebastianKrupinski
authored andcommitted
fix(files_sharing): ignore duplicated navigation when replacing current route
Missing piece from #50669 as we also need to adjust the files sharing router otherwiese we can get those exceptions. Signed-off-by: Ferdinand Thiessen <opensource@fthiessen.de>
1 parent 8e81920 commit effb437

File tree

1 file changed

+28
-6
lines changed

1 file changed

+28
-6
lines changed

apps/files_sharing/src/router/index.ts

Lines changed: 28 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,24 +3,46 @@
33
* SPDX-License-Identifier: AGPL-3.0-or-later
44
*/
55
import type { RawLocation, Route } from 'vue-router'
6-
import type { ErrorHandler } from 'vue-router/types/router.d.ts'
76

87
import { loadState } from '@nextcloud/initial-state'
98
import { generateUrl } from '@nextcloud/router'
109
import queryString from 'query-string'
11-
import Router from 'vue-router'
10+
import Router, { isNavigationFailure, NavigationFailureType } from 'vue-router'
1211
import Vue from 'vue'
12+
import logger from '../services/logger'
1313

1414
const view = loadState<string>('files_sharing', 'view')
1515
const sharingToken = loadState<string>('files_sharing', 'sharingToken')
1616

1717
Vue.use(Router)
1818

1919
// Prevent router from throwing errors when we're already on the page we're trying to go to
20-
const originalPush = Router.prototype.push as (to, onComplete?, onAbort?) => Promise<Route>
21-
Router.prototype.push = function push(to: RawLocation, onComplete?: ((route: Route) => void) | undefined, onAbort?: ErrorHandler | undefined): Promise<Route> {
22-
if (onComplete || onAbort) return originalPush.call(this, to, onComplete, onAbort)
23-
return originalPush.call(this, to).catch(err => err)
20+
const originalPush = Router.prototype.push
21+
Router.prototype.push = (function(this: Router, ...args: Parameters<typeof originalPush>) {
22+
if (args.length > 1) {
23+
return originalPush.call(this, ...args)
24+
}
25+
return originalPush.call<Router, [RawLocation], Promise<Route>>(this, args[0]).catch(ignoreDuplicateNavigation)
26+
}) as typeof originalPush
27+
28+
const originalReplace = Router.prototype.replace
29+
Router.prototype.replace = (function(this: Router, ...args: Parameters<typeof originalReplace>) {
30+
if (args.length > 1) {
31+
return originalReplace.call(this, ...args)
32+
}
33+
return originalReplace.call<Router, [RawLocation], Promise<Route>>(this, args[0]).catch(ignoreDuplicateNavigation)
34+
}) as typeof originalReplace
35+
36+
/**
37+
* Ignore duplicated-navigation error but forward real exceptions
38+
* @param error The thrown error
39+
*/
40+
function ignoreDuplicateNavigation(error: unknown): void {
41+
if (isNavigationFailure(error, NavigationFailureType.duplicated)) {
42+
logger.debug('Ignoring duplicated navigation from vue-router', { error })
43+
} else {
44+
throw error
45+
}
2446
}
2547

2648
const router = new Router({

0 commit comments

Comments
 (0)