Refresh Page automatically?

I am with a website, and I do constant updates.

But after these updates on the server, the site does not update alone 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 would like to know if there is any way to fix this problem.

Example:

I have a green layout on the web.

Changed in css(local) to red and I send this update to the server.

If I don't press CTRL + F5 after making the change, my web site will continue with the green layout.

Author: Nicolas Guilherme Matheus, 2018-02-05

3 answers

<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">
 3
Author: Amadeu Antunes, 2018-02-05 15:04:19

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

Follows 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 make the call

setInterval(getStringDate,10000);

In this case the script is checking every 10 seconds and adding 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.

 1
Author: Jorge.M, 2018-02-05 15:06:32

If you can not avoid the CACHE you can do with PHP (I imagine that is what you use in your hosting) to generate a number that never repeats, I advise using date and time so that the browser interprets as different URLs through a variable.

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

Place 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 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 dispensable:

<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>
 1
Author: RpgBoss, 2018-02-06 13:04:34