How to disable caching with HttpClient get in Angular 6

Gerry picture Gerry · Nov 9, 2018 · Viewed 29.1k times · Source

I'm writing an Angular SPA app, that uses HttpClient to get values from my backend.

What is the easy way to tell it not to cache? The first time I ask it gets the value, then it refuses to make subsequent queries.

Thanks, Gerry

Answer

Mahi picture Mahi · Nov 9, 2018

Using meta HTML tags, Disable browser caching:-

<meta http-equiv="cache-control" content="no-cache, must-revalidate, post-check=0, pre-check=0">
<meta http-equiv="expires" content="0">
<meta http-equiv="pragma" content="no-cache">

or,

Add headers in http request as:-

headers = new Headers({
        'Cache-Control':  'no-cache, no-store, must-revalidate, post- 
                            check=0, pre-check=0',
        'Pragma': 'no-cache',
        'Expires': '0'
    });