Search code examples
javascriptgoogle-chromecachingbrowser-cache

When Chrome clears disk cache?


I have one website which serves listener.js on the main page. I want to update this javascript file with some extra codes. But browsers (especially chrome) has memory and disk cache. Also HTTP cache of course. I tried something about that state. I tried just F5, the file loaded from memory cache. Then I killed chrome and opened the website again, javascript file loaded from the disk cache. So I have 2 questions;

  1. When chrome clears disk cache?
  2. How can I say to my visitors don't use any cache and get the new javascript file from my server?

Update:

  1. Can I do this with no-cache Http header?

Solution

  • Removing temporarily cached file known as cache busting. It is useful because browser doesn't have to download these files again.

    If it is causing issues, developers can force browsers to download new files. This is performed by re-naming file but there is a better way

    src="js/listener.js" => src="js/listener.js?v=2"
    

    Update:

    Or hash like this => ?v=c298c7f8233d which is better than ?v=2 (comment by Tech Guy)

    (Credits: 30-seconds)