vuejs / router

🚦 The official router for Vue.js

Home Page:https://router.vuejs.org/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Parent navigation guard inconsistently re-run when redirecting between child routes

pm0u opened this issue · comments

Reproduction

https://codesandbox.io/p/sandbox/vue-3-router-forked-ns47pv

Steps to reproduce the bug

On the homepage, click the link labeled "start the redirects (hard nav)"
Observe the console
After clicking this 3-4 times you will get the beforeEnter for home logged in-between a child redirect. eg

home beforeEnter
child1 beforeEnter
child2 beforeEnter
home beforeEnter
child3 beforeEnter
child4 beforeEnter

Expected behavior

Parent beforeEnter would only ever run once when changing between child routes. Eg logs matching

home beforeEnter
child1 beforeEnter
child2 beforeEnter
child3 beforeEnter
child4 beforeEnter

Actual behavior

Parent beforeEnter is re-run in an inconsistent/hard to predict manner.

Additional information

Initially found on vue-router v4.0.12 but was able to repro on latest per sandbox.

per the repro, seems to be isolated to when the redirect chain starts on initial "load". Notice it does not happen if the redirect chain is started from the router-link. Only the hard nav version

Since you are redirecting while entering on the first time, it has to trigger again each time you redirect. This is the correct behavior. After the first navigation to one of the children, it doesn’t re enter the parent so it doesn’t trigger again.

how is this the correct behavior? we are not re-entering the parent. We are entering a separate child

Also, it doesn't always happen on the first redirect. Sometimes it happens on the 2nd or third which would not be "while entering."

Additionally, sometimes it doesn't happen at all. It also doesn't happen when the redirects are preceded by a router navigation. Is this difference expected?

See below for clarifications on the weird behavior

@posva

If that is the correct behavior, then this series of events shouldn't be possible, correct?

home beforeEnter
child1 beforeEnter
child2 beforeEnter
child3 beforeEnter
child4 beforeEnter

This is also reproducible randomly at the repro I shared.

Seems to perhaps be an issue with the sandbox env. Opening in a new window, the logs I get are:

home beforeEnter
child1 beforeEnter
home beforeEnter
child2 beforeEnter
home beforeEnter
child3 beforeEnter
home beforeEnter
child4 beforeEnter

So, seems the inconsistency at least is due to the environment. But regardless, is this confirmed to be expected behavior that the parent nav guard would trigger before every single redirect?

If it is, then is it possible to accomplish a redirect from child to child without re-running the parent beforeEnter? If not, I think the docs should be updated as they seem to suggest that this would not be how the navigation guards would be handled.

image

Following a router link to any of the "redirected to" examples does not trigger the parent before enter so this is an inconsistency with the handling of a router-link vs a redirect when going between siblings. I think at least the docs should be updated to clarify how these situations are handled differently if this is in fact the desired behavior