SunnyAdventurer
SunnyAdventurer

Reputation: 103

How to not cache an http response with angular httpClient.subcribe()

i am using ionic/angular for my app. In the starting screen (not splashscreen) I get a .json file from my server with

this.apiService.getEvents().subscribe res => {
                    console.log(res);
});


getEvents(): Observable<any> {
    let headers = new HttpHeaders({
      'Content-Type': 'application/json; charset=utf-8'
    });
    return this.httpClient.get("https:www.someURL.com/somejson.json", { headers: headers });
  }

but the Observable thingy is caching the whole response. Our file "somejson.json" is manually changed and the ionic app should reload its content at every appstart.

So, how do you NOT cache HTTP GET ? thanks in advance ;)

Upvotes: 1

Views: 1559

Answers (2)

Pankaj Parkar
Pankaj Parkar

Reputation: 136144

The request is not cached by Angular, it is cached by the browser. You can avoid caching by generating unique URL each time

getEvents(): Observable<any> {
    let headers = new HttpHeaders({
      'Content-Type': 'application/json; charset=utf-8'
    });
    return this.httpClient.get(`https:www.someURL.com/somejson.json?q=${Date.now()}`, { 
      headers: headers
    });
}

Upvotes: 2

Ritu Riya
Ritu Riya

Reputation: 49

Please use destroy before subscribing to avoid caching.

private readonly _destroy$: Subject<void> = new Subject<void>();
this.apiService.getEvents().pipe(takeUntil(this._destroy$)).subscribe res => {
                console.log(res);
});

Upvotes: 1

Related Questions