I am working on an Angular 17 project and need to simulate the “Empty Cache and Hard Reload” feature that browsers offer. Essentially, I want to force a full refresh of the application, clearing all cached files and fetching fresh resources from the server.
I understand that browsers have this feature built-in, but I need to implement this behavior programmatically within my Angular application without relying on browser settings or configurations.
Could someone provide guidance or code snippets on how to achieve this using Angular 17 code? I am looking for a solution that ensures all cached assets are cleared and the application is reloaded from scratch when this action is triggered. Thank you for your help.
emptyCacheAndHardReload(): void {
// // Clear Local Storage
localStorage.clear();
// // Clear Session Storage
sessionStorage.clear();
// // Clear all Cookies
document.cookie.split(";").forEach((cookie) => {
const cookieName = cookie.split("=")[0].trim();
document.cookie = `${cookieName}=;expires=Thu, 01 Jan 1970 00:00:00 UTC; path=/;`;
});
// // Force a Hard Reload by appending a random query parameter
const randomParam = `?cache_buster=${new Date().getTime()}`;
window.location.href = window.location.origin + "/logout" + randomParam;
}
I tried this function, but it doesn’t clear the cached files from the browser.
1
This is unfortunately not something you can do from the angular app itself (or from JavaScript in some cases).
The way the Browser cache works is, with a lot of simplifications:
- Navigation to the angular app url —> causes the “index.html” page to be downloaded
- as part of the loading process of “index.html” the browser will download the other files referenced by it (.css, .js, images …).
- all downloaded files will be stored in the browser’s cache
-
on a second visit to the same website, most market-standard browsers will not get the “index.html” (and dependencies) from the server again for a while. It is common practice for the Browsers to store the index.html and all its downloaded dependencies in browser caches.
-
steps 1 and 2 happen before any JavaScript function is executed.
So how do we tackle it?
Angular default build process will always generate a “index.html” file and (very important) its dependencies with different name (a hash from their contents)
Ex: Today we build it, and the following is generated
index.html, with refs:
- runtime.afc3546e59a9f.js
- styles.9bb622b0e988a.css
- main.03b09288b11.js
…
than we decide to change something on the app; when building it again the following is generated:
index.html, with refs:
- runtime.afc3546e59a9f.js
- styles.988accccbbba.css
- main.88c3543b092.js
…
(notice that “index.html” remains, and that the other file names have changed)
Here is the trick: As all request to an angular app are first done by having the webserver serving the “index.html”, we configure the server to always serve the “index.html” file indicating it should never be cached.
We do this with adding headers that tell the browsers not to cache it.
This can be done, for example, with the Cache-Control header sent on responses that server issues when serving the index.html file. For example
Cache-Control: no-store, no-cache, must-revalidate
this SO answer explains the Cache header in more details.
As I said in the beginning this is more of a getting started answer, I am over simplifying the cache flows.
Also it is worth to mention that Cookies might not be accessible by the JavaScript code, depending on how the server is configured to created them.
As you have figured, localStorage, sessionStorage and a other Browser caches can be accessed by JavaScript. Your application should be able to handle these though and figure what is valid or not and make a decision as to when these need to be cleared.
Alternatively, if what you need is to run tests on your app ensuring that no cache is present upon start, I would recommend using test tools like playwright, or a virtual machine that can be re-set after the test runs.
Hope this sets you on a wining path