For example, the browser, when first loaded, cached CSS and JS files. Later I changed the contents of these files and uploaded them to the server. How to tell the browser that the files have been updated so that it will overload and process them?

You can refresh the page using the Ctrl+F5 keys, but we will not hang ads on the site so that all users update the page in this way.

  • four
    <script type="text/javascript" src="myScript.js?v=1.2"></script> - Igor

1 answer 1

Traditionally, the URL of the file to which there is a link is added to some unique value in the query, for example, script.js?1337 . The request format does not matter, as web servers ignore it for files.

It should be noted that in most cases it is redundant. HTTP provides many options for configuring caching. Usually, a request for a static file always leaves, but the client (the browser that is) can report that the file has already been cached and indicate the version (date, label). If the file is not updated on the server, the server will give an empty response, telling the client to use the cached version. If the file is updated, the server will send a new file.

Unfortunately, there are all sorts of stupid caching proxies, crookedly configured browsers and other very smart software that interferes with the normal functioning of HTTP according to the standard. Therefore, such crutches are necessary.

  • Probably add information on how to automate all this; since at first I did everything manually (in principle, I didn’t need it often, but still: never do anything manually - we are developers). So, I found this method in the framework of Yii2 (although surely it is used a lot where) ... - Roman Grinyov
  • ... Its meaning is that we add the timestamp of the last change of the file as (as discussed) the GET parameter, for example: my.css?v=1461801600 , my.js?v=1461801600 . That is, changing the file, its version will change itself. You can achieve this with the help of the PHP function filemtime (although it may somehow be implemented differently, until you looked exactly, but I think you can do it with the help of it). - Roman Grinyov
  • ... Yes: in Yii2, this is done using the filemtime() function: source code . - Roman Grinyov