1) Hash Angular files while building
Angular has a built in hashing mechanism to ensure updated files are not cached. To activate this functionality you have to add the property "outputHasing": "all"
to the build configurations in your angular.json file.
Alternatively you can build your project with the command: ng build --output-hashing=all
2) Add server side Cache-Control headers
As @Khalid mentionned, Angular does not ensure the index.html file isn't cached. Server-side response headers can take up this task. Cache-Control is a header that you can configure on your web server to add to all outgoing requests, which will tell the browser and CDNs how to cache your content.
On Apache you should set these cache control headers in your main configuration file 'httpd.conf'. In case you can't access this file due to hosting limitations you can set it in your '.htaccess' file. To not cache the index.html file use following cache control headers:
#Initialize mod_rewrite
<FilesMatch "\.(html|htm)$">
FileETag None
<IfModule mod_headers.c>
Header unset ETag
Header set Cache-Control "max-age=0, no-cache, no-store, must-revalidate"
Header set Pragma "no-cache"
Header set Expires "Wed, 12 Jan 1980 05:00:00 GMT"
</IfModule>
</FilesMatch>
NB: Make sure that the mod_headers are enabled in your main configuration file. Following line should be uncommented (without the #).
LoadModule headers_module modules/mod_headers.so
While calling the latest version of your page (f.ex. by refreshing without using cached content CTRL+SHIFT+F5
) you should now see these cache control headers in your response headers. You can verify these headers in the Inspect > Network tab in your browser.
3) Handle previously cached files
All versions of your index.html file that were cached in your clients browser -before you added the new cache control headers- will still be cached. To overcome this issue you should use different URL's. This can be done by using a new domain name (as long as you do not care about SEO) or by adding a URL parameter (without touching SEO).
After building your Angular project as described above and adding this configuration on your Apache web server, users will always get the newest version of your page.