Visitar URL original
fix(router): don't flash inactive on the first render of RouterLinkActive by aminesbdev · Pull Request #70828 · angular/angular · GitHub
The Wayback Machine - https://web.archive.org/web/20260922203635/https://github.com/angular/angular/pull/70828
Skip to content

fix(router): don't flash inactive on the first render of RouterLinkActive - #70828

Open
aminesbdev wants to merge 1 commit into
angular:mainfrom
aminesbdev:fix/router-link-active-initial-flash
Open

aminesbdev wants to merge 1 commit into
angular:mainfrom
aminesbdev:fix/router-link-active-initial-flash

Conversation

@aminesbdev

@aminesbdev aminesbdev commented Sep 20, 2026 •

Copy link
Copy Markdown
Contributor

RouterLinkActive holds back the active class until router.navigated is 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 router
hasn't finished a navigation yet, it compares the link against the navigation
currently in flight instead (finalUrl, falling back to extractedUrl). That
URL 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 navigated is true, it behaves exactly as
before.

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.navigated is still
false, and that it stays applied once the navigation completes. That test fails
without the change. The router test suite passes locally.

…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.
@pullapprove
pullapprove Bot requested a review from crisbeto September 20, 2026 09:43
@ngbot ngbot Bot added this to the Backlog milestone Sep 20, 2026
@JeanMeche
JeanMeche requested review from atscott and removed request for crisbeto September 20, 2026 14:18
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant