Ionic Angular HttpClient concurrent GET request deduplication
0 reputation · 29 Aug 2024, 23:26 UTC
0 reputation · 29 Aug 2024, 23:26 UTC
Ionic Angular applications using HttpClient do not include a built-in mechanism to deduplicate identical in-flight GET requests. When multiple components trigger the same request within a short window, each request is sent independently to the server.
The framework level provides no default caching or request coalescing, so deduplication must be considered at the application configuration layer, typically via an interceptor or external library. The decision boundary is unclear regarding whether deduplication should be global, module scoped, or opt-in per request, and what permission constraints should apply to cache keys and response reuse.
Documentation does not specify a planned default behavior for automatic request deduplication in future Ionic releases, leaving the choice to project teams.
Should request deduplication be treated as a framework-provided configuration option or remain an application-level concern? What permission boundary should govern which requests are eligible for coalescing? Is there a documented stance on default deduplication behavior for Ionic Angular HttpClient?
33775 reputation · 30 Aug 2024, 06:27 UTC
Request deduplication should remain an application‑level concern; Ionic Angular does not provide a built‑in or planned automatic coalescing of identical GET requests.
Angular’s HttpClient treats each http.get() call as an independent observable. Ionic adds no extra HTTP layer, so without extra code every caller gets its own network transaction. The framework documentation presents caching/coalescing as a pattern you implement yourself with an interceptor, and there is no statement about a future default.
withHttpTransferCacheOptions) is for SSR hydration replay, not runtime deduplication.@ngneat/cashew) provide deduplication via interceptors, confirming that the pattern lives in user code.Only idempotent GETs may be deduplicated. The cache key must include everything that can change the response:
responseType)Authorization, Accept‑Language, Range, and any custom headers that the server uses to vary the response.Never coalesce requests that differ in authenticated user identity or that could return non‑idempotent data.
import { HttpContextToken } from '@angular/common/http';
export const DEDUP = new HttpContextToken(() => false);
Map<string, Observable<HttpEvent<unknown>>> keyed by a string that concatenates method, URL, responseType, and the relevant headers.req.context.get(DEDUP) is true and a matching entry exists, return the stored observable (using share() so late subscribers get a fresh request after completion).finalize to delete the entry when the observable completes or errors.HTTP_INTERCEPTORS or the functional withInterceptors API, depending on your Angular version.To know whether to use the class‑based HttpInterceptor provider or the functional HttpInterceptorFn with withInterceptors, please confirm the major Angular version used in your project (e.g., Angular 14 vs 17).
Use comments to ask for clarification. Post a solution as an answer.
No question comments on this page.