Answer the question
In order to leave comments, you need to log in
How to update the cache on the client with Nginx?
I have enabled caching on the site user side. Those. Now if I change the styles in the external CSS file, then the user will naturally have the old version of the styles (Before he deletes the cache for example with Shift + F5). How to fix this problem?
Answer the question
In order to leave comments, you need to log in
As Dmitry wrote , you can update the cached CSS file by adding a get request:
<link rel="stylesheet" href="/style.css?v=1">
In the case above, this is
v=1
. In this case, the value of the v parameter must necessarily change after changes in the file. I preferred in my case that the value of the v parameter (or any other) would be the date/time of the last change.
Code in a separate PHP file:
<?php
function autov($name) {
chdir($_SERVER['DOCUMENT_ROOT']);
$filedir = (getcwd() . '/' . $name);
echo $name . '?v=' . strval(filemtime($filedir));
};
?>
<link rel="stylesheet" href="<?php autov('style.css'); ?>">
Add a random parameter to the css and js files, which you modify after making changes to the files themselves. This will reset the user's browser cache. For example:
After changing the style.css file, increment the counter by one:
<link rel="stylesheet" href="/style.css?v=1">
<link rel="stylesheet" href="/style.css?v=2">
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question