How to Simulate “Empty Cache and Hard Reload” Feature in Browser Using Angular 17 Code?

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.

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code> 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;
}
</code>
<code> 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; } </code>
  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:

  1. 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
  1. 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.

  2. 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

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>Cache-Control: no-store, no-cache, must-revalidate
</code>
<code>Cache-Control: no-store, no-cache, must-revalidate </code>
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

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật