DEV Community

Bharathvaj
Bharathvaj

Posted on • Originally published at bharathvaj.me on

3 2

How to fix NavigationDuplicated Vue Router error

Since version 3.3.0, Vue Router throws a couple of errors as a part of its promise API. “NavigationDuplicated” is one such error being thrown explicitly when the user clicks <router-link> or ( router.push() / router.replace() called programmatically) to view a page component that is already in current view.

Major router errors introduced includes,

  • NavigationDuplicated
  • NavigationCancelled
  • NavigationRedirected
  • NavigationAborted

To solve this error, we basically need to catch the error being thrown by the API methods. But adding catch statements to all the push and replace methods scattered across the codebase is really painful and time-consuming.

Roll-Safe-Think-About-It

There are a couple of solutions to solve this issue, but the most optimized solution is to modify the router’s methods with our own custom methods by adding that catch statement in one place as follows.

Note: Make sure to include this script at the root of the project along with other polyfills



// polyfills/router.js

*
Wraps Vue Router - push() and replace()
*/
import Router from 'vue-router';

['push','replace'].forEach(method => {
const originalMethod = Router.prototype[method];
Router.prototype[method] = function m(location) {
return originalMethod.call(this, location).catch(error => {
if (error.name !== 'NavigationDuplicated') {
// capture exception
}
})
};
});

Enter fullscreen mode Exit fullscreen mode




Reference

Sentry blog image

How I fixed 20 seconds of lag for every user in just 20 minutes.

Our AI agent was running 10-20 seconds slower than it should, impacting both our own developers and our early adopters. See how I used Sentry Profiling to fix it in record time.

Read more

Top comments (0)

SurveyJS custom survey software

JavaScript UI Libraries for Surveys and Forms

SurveyJS lets you build a JSON-based form management system that integrates with any backend, giving you full control over your data and no user limits. Includes support for custom question types, skip logic, integrated CCS editor, PDF export, real-time analytics & more.

Learn more

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay