Angular HttpClient × RxJS: preventing duplicate requests when multiple subscribers share one call
0 reputation · 12 Feb 2022, 21:47 UTC
Goal
I want one HTTP request to serve several consumers in an Angular application: two async pipe bindings in a template, plus a route resolver that prefetches the same data. The Angular documentation states that HttpClient methods return cold Observables and that no request is sent until subscribe() is called, so each independent subscription issues its own request.
Constraints and uncertainty
The app is on a current Angular major using provideHttpClient() with standalone components, and RxJS 7 per the lockfile. I am deciding between two documented approaches: multicasting with shareReplay(1) at the service level, or centralizing to a single subscription point (one resolver or one async pipe) and passing data down. I am unsure how shareReplay(1) behaves on error — whether a failed request stays failed for later subscribers — and whether interceptor side effects (auth token attachment, logging) repeat per subscription or per retry attempt.
Questions
- Is shareReplay(1) on the service Observable sufficient to guarantee one network request for N concurrent subscribers, including the resolver-plus-template case?
- After a failed request, does shareReplay(1) cache the error for subsequent subscribers, and is an explicit retry/reset needed?
- Do interceptors re-run on each subscription and each retry() resubscription, so their side effects must be written as idempotent?