Update page automatically?

Asked

Viewed 2,171 times

0

I have a web site, and I constantly update.

However after these updates on the server, the site does not update alone on devices/desktop, 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 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.

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">
  • 1

    What is called this kind of problem? these metatags have solved my problem, thank you

  • 1

    @Nicolasguilhermematheus is just Cache. Not necessarily a problem!

1

If you cannot avoid the CACHE you can do with PHP (I imagine it 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.

Harvest 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/db text files (*.TXT, *.LOG, *.JSON) or images directly typed in the browser bar will only see the first version of the file until the CACHE expires.

In this case I advise you to modify the HTACESS of your hosting, it 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

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

On your ready Document 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 "c" class to the links you need, and if you need to use in script, simply change from href to src in the script.

  • Thanks for the solution Jorge, but would not let my site slow? keep checking at all times :

  • @Nicolasguilhermematheus this would be a more specific solution for certain links you cite, relative to the slowness: as it would be done only every 10s and when the user was on the page, there would be no problem, in case you do not want to store the cache, regardless of the file, the use of the goals tag cited by Amadeu is the most appropriate.

  • aaa, I get it, but anyway, I appreciate you trying to help me somehow ^^.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.