angular JS, CSS Version update

Problem Statement

A few days ago, we encountered problems related to the browser cache in our Angular application. We want to find solutions to address this issue so that we don't have to request our customers to perform a manual browser refresh after each deployment. In this post, we will explore potential solutions and strategies to tackle this problem.

Browser caching occasionally leads to issues in displaying updated content following a deployment. Here, we'll explore several typical strategies to address this problem.

Why do we use Cache Busting?

You can implement cache-busting techniques to ensure that new versions of your application are loaded by the browser. This can be done by appending a unique version number or a hash to the filenames of your assets (JavaScript, CSS, etc.). This way, when the files change, the URL changes, and the browser will fetch the updated files.

Let's dive into an in-depth discussion of the cache-busting technique.

What is Cache Busting?

Cache busting is a technique used to ensure that the latest version of your web application is loaded by the user's browser, even when it has previously cached older versions of the assets. The main idea behind cache busting is to change the URL of the assets so that the browser doesn't recognize them as cached resources. Here's how it works:

  1. File Renaming: In this approach, you can rename your asset files (JavaScript, CSS, images, etc.) by adding a unique identifier to their filenames. This identifier can be a version number, a timestamp, or a content-based hash. For example, instead of naming your JavaScript file "app.js," you can name it "app_v1.2.3.js" or "app_hash123456.js."

  2. Query Parameters: Another common technique is to add query parameters to the URLs of your assets. For example, you can load your JavaScript file as "app.js?v=123456" or "app.js?version=1.2.3." These query parameters can be set to a unique value or the application's version number.

Certainly, here are the steps to implement the cache-busting technique for your web application.

Without cache busting,

Without cache busting

After running the command "ng build --configuration=production && node ./versioning.build.js".

Code with cache busting