Cache on Web Systems

Asked

Viewed 340 times

3

I have a web system using Ajax that is constantly updated. I need to implement a way to update the user cache of files. js and . css when these files are modified.

I tried to solve it in two ways. The first is the use of variable url to call these files forcing the browser to always search the resources and not use the cache, but this way the system was slow because it did not count on the benefits of the cache.

The other way was with the Html5 manifest cache with scripts to dynamically create the manifest. But as the system has constant image upload, each time an image is uploaded the manifest is updated to a new version, requiring users to renew the cache for all files.

There is a way to use the cache normally as it works, but when there is change of files . js and . css, that the browser be informed the files to be renewed in the cache, from a version control? Does anyone know an approach that works more or less like this?

  • Hello Rafael Pedroso, the stackoverflow community seeks to answer specific questions. We do not respond based on our opinions. I ask you to format your question better so that it becomes specific with a problem you are having.

2 answers

0

A simple approach is to convert js and css according to the deploy version. Ex.:

<?php 
    define( 'VERSAO', '1.0.0' );    
    echo '<script src="https://example.com/arquivo.js?ver='.VERSAO.'" />';
?>

So it will always cache with the versioning tag. As long as the tag remains the same the browser will not fetch the file again. When you need to search the files again you update the constant VERSAO and all the files will be searched and curled again.

Ex.:

screenshot desta tela mostrando um arquivo CSS com uma varável GET sendo buscado no cache do navegador

  • Hi Ricardo, if I pass a GET variable (ver='.VERSAO.') in the request of . js, the browser will not cache the file as it understands that it is a dynamic file.

  • @Rafaelpedroso, you are mistaken. See the print here the console of the OS with a GET variable in the css file being searched in my browser cache: https://www.dropbox.com/s/zsyf57e1ewoy5y7/Captura%20de%20tela%202017-02-21%2012.26.17_Ink_LI-cache.jpg? dl=0

  • suggest reading this article https://www.html5rocks.com/pt/tutorials/appcache/beginner/ maybe it helps

  • Got it @Ricardomoraleida. I don’t know why in my system it doesn’t behave like this. I’ll investigate it. Thanks.

0

Use the time() variable as a parameter to not need to change the code each time you update CSS/JS

<?php
    echo "<script src='seuJavascript.js?time=".time()."'/>"
?>

Browser other questions tagged

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