Friday, June 21, 2024

Angular browser cache clean

This week, had an interesting learning whilst web product launch using latest Angular framework

If the browser cache is not cleared after deploying a new version of your Angular application, users might experience issues such as:

  • Stale Content: Users may see old versions of your application, causing inconsistencies if the new version contains critical updates.
  • Missing Files: The new deployment might reference files (like JavaScript or CSS) that have changed or no longer exist, causing 404 errors.
  • Unexpected Behavior: JavaScript and CSS changes might not reflect, leading to broken functionality or layout issues.


There are Top-5 technical solutions to avoid browser cache clearance
  1. Cache Busting: Angular CLI automatically handles cache busting by appending a unique hash to the filenames of built assets. Ensure production build as ng build --prod
  2. Service Worker: Implement Angular PWA (Progressive Web App) with service workers to manage cache effectively. ng add @angular/pwa Configure the ngsw-config.json file to control caching strategies.
  3. HTTP Headers: Configure your server to set appropriate caching headers. This can force the browser to always fetch the latest version of your files.{   add_header Cache-Control "max-age=31536000, public"; }
  4. Versioning: Use versioning in your file names or paths to ensure that users receive the updated files.<script src="main.v1.2.3.js"></script>
  5. Client-Side Cache Control: Programmatically clear the cache on the client side using JavaScript when a new version is detected.
    if ('serviceWorker' in navigator) Unknown macro: {  navigator.serviceWorker.getRegistrations().then((registrations) =>Unknown macro}); }

No comments:

Post a Comment