Sub topic 14
Showing progress during slow navigations
This chapter assumes you know how to write a resolver with ResolveFn and that you have read the chapters on guards and the router lifecycle. You should be comfortable with Angular signals and basic RxJS.
Why the screen seems frozen
A resolver or an async guard holds the navigation open until it finishes. During that time the router keeps the old page on screen and the URL does not change. If the request takes two seconds, the user sees nothing happen after a click. They may click again or assume the app is broken.
The router already reports what it is doing through Router.events. You can listen to those events and show a progress indicator.
The events that matter
| Event | Meaning |
|---|---|
| `NavigationStart` | A navigation began. Guards and resolvers have not run yet. |
| `NavigationEnd` | The navigation succeeded. |
| `NavigationCancel` | A guard returned `false` or a `UrlTree`, or a newer navigation replaced this one. |
| `NavigationError` | Something threw, for example a resolver that failed. |
A loading indicator must turn on at NavigationStart and turn off on any of the last three events. If you handle only NavigationEnd, the indicator stays on forever after a redirect or an error.
A loading signal from router events
The component below converts the event stream into a boolean signal. It uses toSignal so the subscription is cleaned up with the component.
import { Component, inject } from '@angular/core';
import { toSignal } from '@angular/core/rxjs-interop';
import {
NavigationCancel,
NavigationEnd,
NavigationError,
NavigationStart,
Router,
RouterOutlet,
} from '@angular/router';
import { filter, map } from 'rxjs';
@Component({
selector: 'app-root',
standalone: true,
imports: [RouterOutlet],
template: `
@if (loading()) {
<div role="progressbar" aria-label="Loading page">Loading…</div>
}
<router-outlet />
`,
})
export class AppComponent {
private router = inject(Router);
loading = toSignal(
this.router.events.pipe(
filter(
(e) =>
e instanceof NavigationStart ||
e instanceof NavigationEnd ||
e instanceof NavigationCancel ||
e instanceof NavigationError,
),
map((e) => e instanceof NavigationStart),
),
{ initialValue: false },
);
}The filter keeps only the four events that change the state. The map turns each one into true for a start and false for any of the three endings.
Avoiding flicker
Fast navigations make a spinner flash for a few milliseconds, which looks worse than no spinner. Two common remedies:
- Delay showing the indicator, for example by 150 ms, and cancel the delay if the navigation ends first.
- Keep the indicator on for a minimum time once it appears.
In RxJS you can apply the first remedy with switchMap on the start event and timer. Keep the logic in one place, such as a small service, so every page shares it.
Other ways to give feedback
- Skeleton placeholders. If the page can render without the data, skip the resolver. Activate the route at once and let the component show placeholders while it loads. This is often a better experience than blocking.
- Resolve only what is essential. Resolve the data the page cannot render without, and load secondary data inside the component.
- Set a timeout. Add the RxJS
timeoutoperator in the resolver so a stalled request ends in an error that you can handle, as covered in the chapter on resolver failures.
Accessibility
Give the indicator role="progressbar" and a label, as in the example, so screen readers can announce that something is loading.
Exercise
A user clicks a link to a route whose canActivate guard returns a UrlTree pointing to /login. Which router events fire for the original navigation, and why does a loading indicator that listens only to NavigationStart and NavigationEnd stay visible?
Show answer
NavigationStart fires, then NavigationCancel fires because the guard redirected (a new navigation to /login then starts and ends on its own). The indicator that listens only for NavigationEnd misses the cancel event of the original navigation. In this case the redirect's own NavigationEnd may still clear it, but with a plain false return there is no redirect and no NavigationEnd, so the indicator stays on. Handle NavigationCancel and NavigationError as well.