Clear the browser cache after uploading version

4

I have the following scenario:
Each month we release new versions for our customers, and most of the time our customers call telling us that X functionality is not working the way it should. Because it is usually running a function that is from the previous version exactly because of the browser cache, in that case the client has to click CTRL + F5 or even clear all its cache for the X functionality to function properly.

I would like to know if there is any class in php or another language also that can be implemented in php to perform the cleaning of this cache. Home because for such a situation I can use the database to check if we are uploading version for the client or not and to perform cleaning of that browser.

Well today we cache all JS to gain a bit more in performance, we use the following:

header("Pragma: public");
header("Expires: 31536000");
header("Cache-Control: must-revalidate, post-check=900, pre-check=3600");
header("Cache-Control: public");
header("Content-Type: application/javascript");
header("Cache-Control: public, max-age=31536000");

I searched the internet a lot, and everyone I looked for did not succeed in what I was really wanting to do, finally I tried php's own function

  

Clearstatcache ()

    
asked by anonymous 26.02.2016 / 16:00

4 answers

4

Simply name the JS and CSS files or have a variable name. For example:

  • What you should have now: omeucss.css || omeujs.js
  • What should happen next: omeucssv12 || omeujs45.js

This can be done automatically, it depends on your system .. I here together all the js in a file (such as css) and in html I call the url: meusite.pt/js/ when it calls the file / js /omeujs.js At this point it sees if the file has already been generated, if it has not been generated (taking into account the variable) and displays the result js:)

EDIT

I forgot the part of htaccess:

RewriteRule ^js/(.+).js$ /js/ [NC,L]

Hugs and good luck!

    
26.02.2016 / 16:36
4

Solution

  • Rename extensions of all JS files (which I believe are files that are giving cache problem) to .php
  • Set the content type of the JS page using header("Content-type: text/javascript");
  • Create a PHP script responsible for validating whether or not it is necessary to clear the cache.

To validate whether or not it is necessary to clear the cache, you can use cookies.

<?php

$versaoAtual = 1;

if (!isset($_COOKIE['versao'])) {
    setcookie('versao', $versaoAtual, (time() + (60 * 24 * 3600)));
} else {
    $versao = (int) $_COOKIE['versao'];

    if ($versao < $versaoAtual) {
        header("Pragma: no-cache");
        header("Expires: Mon, 20 Jul 2000 03:00:00 GMT");
        header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT");
        header("Cache-Control: no-cache, cachehack=" . time());
        header("Cache-Control: no-store, must-revalidate");
        header("Cache-Control: post-check=-1, pre-check=-1", false);

        setcookie('versao', $versaoAtual, (time() + (60 * 24 * 3600)));
    }
}

Okay, so I think you can solve your problem. Remember to always update the value of versaoAtual because it is based on it that PHP will decide if the client needs to clear the cache.

Extra

I believe (not sure) that you could also use CloudFlare for this. In their panel you have a purge cache option, where you define which files you want to remove from the cache.

This would remove the cache from the CloudFlare server, I just do not know if it would remove from the client as well, I have not yet had time to analyze how this purge cache works.

That's it, I hope I contributed somehow.

    
26.02.2016 / 16:37
3

No header combination is 100% guaranteed.

The most guaranteed is to add a parameter as a url query.

Example: /js/arquivo.js?20160227 .

This ensures that when you modify, the new version will always be downloaded. Just change the number. In this case we use date.

Suppose you modify it again tomorrow, then modify it to /js/arquivo.js?20160228 .

Browser caching exists to reduce data traffic, saving bandwidth. In this scheme, using a new parameter with each new version, will ensure that the cache maintains the current version and is always sure that a new version will always be updated, regardless of the browser cache.

Instead of using current date, you can also use the version of the file.

Example: /js/arquivo.js?1.0.0.2 .

You can also use anything random, as long as you do not repeat a previously used one.

Example: /js/arquivo.js?qualquer_coisa_serve .

    
26.02.2016 / 17:51
1

I believe adding the header to Last-Modified will solve the problem.

This will tell the browser which date the file was changed, which in your case would be the date the new version became available.

Therefore, the browser would not use the cached version, but the server version. The cached version would be replaced by the server version. Changing the code entered in the question:

//mktime(hora, minuto, segundos, mes, dia, ano)
//Abaixo indicamos que houve uma alteração no ficheiro a meia noite do dia 26/02/2016 
$newVersion = gmdate("M d Y H:i:s", mktime(0, 0, 0, 2, 26, 2016));

header("Pragma: public");
header("Expires: 31536000");
header("Last-Modified: " . $newVersion . " GMT");
header("Cache-Control: must-revalidate, post-check=900, pre-check=3600");
header("Cache-Control: public");
header("Content-Type: application/javascript");
header("Cache-Control: public, max-age=31536000");
    
26.02.2016 / 16:54