M
M
MaksSmag2020-07-22 19:54:12
Nginx
MaksSmag, 2020-07-22 19:54:12

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

2 answer(s)
M
MaksSmag, 2020-07-23
@MaksSmag

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));
    };
?>

Code for including CSS (or any other cached file):
<link rel="stylesheet" href="<?php autov('style.css'); ?>">

I hope nothing more needs to be explained. There will be questions - I'll try to answer.

D
Dmitry, 2020-07-22
@gebrak

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 question

Ask a Question

731 491 924 answers to any question