Bypass caching js/css

0 like 0 dislike
10 views
Hello! Want to ask a very simple question:

There are in-house site, which is also being constantly improved, therefore finalized files of scripts and styles.
Modern browsers are very well able to cache downloaded files and when a file changes on the server stubbornly continue to use what is in the cache.
How can I force the browser every time to download a file from the server new? I would like to see the most cross-browser solution.

Saved until manually rename files before each update, but continue to do really do not want...
Maybe there is some more automated way to specify the version of the script in the file name and the template titles?
by | 10 views

7 Answers

0 like 0 dislike
The easiest option for You, I think, of course, to use a GET parameter after the script:
\r
\r

But, not just a parameter and in this parameter to pass the modification time of the script file in unixtime format. In this situation, You file will update all users once it is changed on the server. A similar trick can be done with hesom file, such as MD5, it will avoid cases when the modification time is shifted without changing the content, and, in fact, the cached variant will suit all. It depends on Your method of deployment.
\r
Of course, such checks involve file system, interpreter, etc., which is not very fast in production, I would such a scheme is not advised, but for the realities of fast-changing project — a good shot in both birds.
by
0 like 0 dislike
main.js?123
main.css?123
If the file passes parameters (?123), the browser has to load again.
by
0 like 0 dislike
for nginx, something like that
\r
\r
location ~* /static/\\d+/ { alias /path/to/static; } 

\r
in the template when you modify scripts/styles increase it's value
\r
\r
by
0 like 0 dislike
And isn't it better to put all these anachronisms (al even marasmus) described above, and to use what actually was implemented by your task, that is the ETag. More read in Wikipedia, al anywhere else (quite a lot).
\r
+ that all happens automatically and the file is cached.
\r
An example of the use for ".htaccess":
\r
# Custom ETag to the last modified time of the file and its size
FileETag MTime Size
by
0 like 0 dislike
if you want to disable caching for a certain file generally, in pattern to end of file to append unixtime (time in seconds since the UNIX epoch)
\r
style.css?1292227150
\r
In Ruby this Time.now.to_i, there are similar functions in almost all languages.
by
0 like 0 dislike
There is a constant, naprimer, which is equal to the REVISION number the current revision of statics.
All connects as statics /css/REVISION/main.css /js/REVISION/core.css
\r
With nginx written as above:
by
0 like 0 dislike
If you want to disable caching at all, then in my opinion the most reliable way is to generate a random number as described above and add header Cache-Control: no-cache, no-store in the server's response.
by

Related questions

0 like 0 dislike
2 answers
asked Jun 9, 2019 by alexanev6
0 like 0 dislike
2 answers
0 like 0 dislike
1 answer
0 like 0 dislike
2 answers
0 like 0 dislike
2 answers
110,608 questions
257,186 answers
0 comments
32,718 users