How to deal with cached CSS, JS, and image files | Prevent the browser from caching files

Опубликовано: 04 Октябрь 2024
на канале: Adnan Afzal
14,961
134

In this tutorial, I am going to teach you, how you can update your cached files. Suppose, you have written a Javascript file named “script.js”. Now, you added some functionality code in that file, or change the algorithm of some function. You have updated the file on a live server, but your changes will not affect your user’s side if their browser has cached that JS file.

In that case, you have to forcefully tell the browser to fetch the updated file instead of serving the cached file. The browser cache works in such a manner that, whenever a CSS, JS, or image file is rendered in the client’s browser, the browser stores the URL of that file in its cache along with its content. When the user refreshes the page, all style, script, and img tags request the resources using the “href” and “src” attributes. The browser checks if the requested URL already exists in its cache database, if “yes” then it (browser) fetches its content from the cache database and serves it.

Source code:
https://adnan-tech.com/prevent-browse...