$router.push() followed by redirect in a route's beforeEnter hook causes uncaught promise rejection
See original GitHub issueVersion
3.1.3
Reproduction link
https://jsfiddle.net/asu3qnry/
Steps to reproduce
To trigger the uncaught promise rejection, click on “/home” and then the button.
What is expected?
No uncaught promise rejection because there are actually no errors generated by the application. It’s simply a navigation ($router.push
) that generates a redirect (next('/foo')
) in a beforeEnter
hook.
What is actually happening?
Console reports: Uncaught (in promise) undefined
Using a router-link
to /bar
instead of the initial $router.push('/bar')
does not produce the error.
Issue Analytics
- State:
- Created 4 years ago
- Reactions:23
- Comments:18 (3 by maintainers)
Top Results From Across the Web
vue-router — Uncaught (in promise) Error: Redirected from ...
This error is a way for letting that goToB() function know that the router ... The router.push function is returning a Promise (as...
Read more >Waiting for the result of a Navigation - Vue Router
When using router-link , Vue Router calls router.push to trigger a navigation. ... A navigation guard redirects somewhere else by returning a new...
Read more >Protecting Vue Routes with Navigation Guards - CSS-Tricks
A common practice that applications use to protect content is to house them under specific routes and build redirect rules that navigate users ......
Read more >Authentication best practices for Vue - Sqreen Blog
How do I redirect the user after authentication actions (login/logout)? ... reject) => { // The Promise used for router redirect in login....
Read more >Advanced Routing with Vue and Vue Router: Redirects & Nav ...
One way this could be useful is to ask a user if they meant to navigate away, and cancel the navigation by passing...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
@posva Thanks for the clarification! However, I would argue that a redirection that cancels a navigation should not be a Promise rejection. Since rejection becomes a throw in the async/await model, it should only occur on actual errors. In my opinion, an expected control flow with a redirection should not have to add error handlers to avoid errors. While reading #2881 and the relevant documentation again, I could not find a description of what
$router.push()
actually resolves to; only that it should resolve (or reject) when navigation is complete.The example code you gave in #2881 inspired me to create this global
push()
override:Since the precise resolve/reject behavior of
push()
doesn’t seem set in stone, would it be possible to resolve to the final route (orfalse
if navigation aborted) instead of rejecting when there is no error?I already answered this at https://github.com/vuejs/vue-router/issues/2833#issuecomment-532158403
Please, check the related issues, the reasoning has been verbosely explained there already