Refresh page automatically?

-1

I have a web site, and I make constant updates.

But after these updates on the server, the site does not update itself on the devices / desktop, I need to press CTRL + F5 to update.

The problem is that not everyone will give CTRL + F5 before using the site, I wonder if there is any way to fix this problem.

Example:

I have a GREEN layout on the web.

I switch in css (local) to RED and send this update to the server.

If I do NOT press CTRL + F5 after performing the change, my web site will continue with the GREEN layout.

    
asked by anonymous 05.02.2018 / 15:59

3 answers

3
<meta http-equiv="Cache-Control" content="no-cache, no-store, must-revalidate">
<meta http-equiv="Pragma" content="no-cache">
<meta http-equiv="Expires" content="0">
    
05.02.2018 / 16:04
1

At one time I had created a function that basically takes local time on the machine and updates the / css script to use the version of that time.

Follow the function

    function getStringDate() {
    var data = (+new Date());

        horario= ('?_='+ data),
        str = $('.c').attr('href'),
        fimstr = str+horario;
        console.log(fimstr);
        $('.s').attr('href',fimstr);
}

In your document ready, call

setInterval(getStringDate,10000);

In this case the script checks every 10 seconds and adds the time at the end of the href of your css. To use, add the class "c" in the links you need, and if you need to use in script, just change from href to src in the script.

    
05.02.2018 / 16:06
1

If you can not avoid CACHE you can do with PHP (I imagine it is what you use in your hosting) to generate a number that never repeats itself, I advise you to use DATE and TIME for the browser to interpret as different URLs through a variable.

The format will not be noticed as date and time, I put in the example only for minutes and not seconds to not generate an unnecessary load of access to your hosting.

Paste this at the beginning of the code.

<?php
$nc = "?".date('YmdHi'); //Exemplo do resultado 201802061040
?>

Examples:

<script src="seuscript.js<?php echo $nc ?>"></script>

<img src="imagem.jpg<?php echo $nc ?>" />

This will only affect content placed in HTML if the visitor accesses urls of text / db files (* .TXT, * .LOG, * .JSON) or directly typed images in the browser bar you will see only the first version of the file until the CACHE expires.

In this case I advise you to modify the HTACESS of your hosting, this will make any code expendable:

<filesMatch "\.(html|htm|js|css|png|gif|jpg|jpeg|json)$">
FileETag None
<ifModule mod_headers.c>
Header unset ETag
Header set Cache-Control "max-age=0, no-cache, no-store, must-revalidate"
Header set Pragma "no-cache"
Header set Expires "Wed, 11 Jan 1984 05:00:00 GMT"
</ifModule>
</filesMatch>
    
06.02.2018 / 13:58