Clear browser cache with query string approach

2019-08-12 23:43发布

问题:

I need to clear browser cache when I push an updated javascript file on server. A simple answer would be to use below technique of query string.

<script type="text/javascript" src="/js/myjsfile.js?{my file version}"></script>

It would work but

  1. Do I need to do this on every single script tag of every single page of my application?
  2. Can I do this at main screen like login which loads at the beginning and I assume that would clear the cached file with new one, would it work?

回答1:

"Do I need to do this on every single script tag of every single page of my application?"

Yes you do. The cache is based on the file's url, including those extra parameters.
Adding those parameters doesn't actually remove the file from the browser's cache, it more or less sees it as a new, different file.

This also answers point 2, since having to do it on all pages means you can't do it on only one page.