I created Angular 6 app and hosting it using Apache on a remote server.
Created a build with ng build --prod
.
I noticed that when making changes and updating an html file – the page is being loaded from a cache and not from a new version of a file that is placed in Apache folder (using default configuration in Apache and nothing in Meta tags in HTML pages yet).
How to force reloading page on a client browser but only when there is a new version of the same page? (new changes to an existing site)
What are the best practices?
5
Answers
When you build do this:
This should prevent changed files from caching.
You can use AOT Production build, by default have hashing enabled.
When using a production build
ng build --prod
, the cli will create a bundle with a hash value attached to each of your angular files. So if you ensure your index is not cached, it will refer to the files with the hashes. If the files have not changed, their hashes will be the same, and so it will load them from the cache. If they change, it will attempt to load the new files.The remaining thing is to ensure that the index.html file is not cached, and this can be done by sending the no-cache headers, or using a dynamic file (e.g. php, jsp, …) instead of *.html.
I guess you are asking this question for the production environment not for development purpose.
If this is the case, then you can check the following:
index.html file is not getting updated.
check what cache expiration is getting set on the browser. It must be
no-cache
.How are you generating the building the code. If using angular-cli then the
ng build
will create a the chunk names with hash and it is different each time when the content is changed.check the cache header for lazy loaded module
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:
NB: Make sure that the mod_headers are enabled in your main configuration file. Following line should be uncommented (without the #).
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.