fix(router): don't flash inactive on the first render of RouterLinkActive - #70828
Open
aminesbdev wants to merge 1 commit into
Open
aminesbdev wants to merge 1 commit into
aminesbdev wants to merge 1 commit into
Conversation
…tive RouterLinkActive kept a link's active class withheld until the router's first navigation had fully resolved, even when the URL the app booted with already matched that link. In practice this meant every navbar built with routerLinkActive would render its active link as inactive for a moment and then jump to active once the initial navigation finished, which is a visible flash on every page load. The guard was checking `router.navigated`, a boolean that only flips to true after the first NavigationEnd. Nothing before that point ever got a chance to compute whether a link matched the URL, so the flash happened regardless of how fast the navigation actually was. Instead of waiting on `navigated`, compare the link against whatever navigation is currently in flight once one has started. The navigation's target URL is already known as soon as NavigationStart fires, well before guards, resolvers or NavigationEnd, so a link that already matches can pick up its active state immediately.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.


RouterLinkActive holds back the active class until
router.navigatedis true,which only happens after the first NavigationEnd. If the app boots on a route
that a link already points to, that link renders as inactive and then jumps to
active once the initial navigation finishes. On a navbar this shows up as a
small flash on every page load.
I changed it so the directive no longer waits for
navigated. While the routerhasn't finished a navigation yet, it compares the link against the navigation
currently in flight instead (
finalUrl, falling back toextractedUrl). ThatURL is already known when NavigationStart fires, so a matching link gets its
class right away. The directive now also re-runs its update on NavigationStart
during that first navigation. Once
navigatedis true, it behaves exactly asbefore.
One case is unchanged on purpose: if no navigation has started at all, there is
nothing to compare against, so the link stays inactive.
For tests, I renamed the existing "should not set the class until the first
navigation succeeds" test, since it only covers that no-navigation case. I
added one that keeps the initial navigation pending on a guard I resolve by
hand. It checks that the class is applied while
router.navigatedis stillfalse, and that it stays applied once the navigation completes. That test fails
without the change. The router test suite passes locally.