Is there any way to clear cache in the client browser? [duplicate]

0

For example, I have my site and I made a modification, but it has not yet been applied in the client browser. Is there any way I can run this cache cleanup on the client browser? With PHP, maybe JavaScript?

In a site I'm working on, it has the CDN cache, but I realized that even though I cleaned the CDN's cache of the site, "no use", put the change will only be seen on the client screen if it clears the browser cache of it.

    
asked by anonymous 23.06.2017 / 15:30

2 answers

1

You can create dynamic names for the files, so the browser will force the download.

  

Example:

<script src="https://cdn.meucdn.com/js/meu-arquivo.js?v=<?phpecho$version_js;?>"></script>
<link href="https://cdn.meucdn.com/css/meu-arquivo.css?v=<?php echo $version_css; ?>" rel="stylesheet" type="text/css"/>

A good practice is you version your JS file and download it only when there are changes, then just change the value of the $version variable that will force the file to download, but if you want to force the download always you can define the variable $version = time(); , however I think it will force your server a lot in case of many hits.

    
23.06.2017 / 15:35
0

If the server that hosts your site is linux, you can set the cache time in the .htaccess configuration file:

Header unset Pragma
FileETag None
Header unset ETag

# 1 Ano (limitado a ficheiro media)
<FilesMatch "\.(ico|pdf|flv|jpg|jpeg|png|gif|swf|mp3|mp4)$">
Header set Cache-Control "public"
Header set Expires "Thu, 15 Apr 2010 20:00:00 GMT"
Header unset Last-Modified 
</FilesMatch>

# 2 Horas (limitado a ficheiro conteúdos)
<FilesMatch "\.(html|htm|xml|txt|xsl)$">
Header set Cache-Control "max-age=7200, must-revalidate"
</FilesMatch>

# Em cache para sempre (scripts e folhas de estilo)
<FilesMatch "\.(js|css)$">
Header set Cache-Control "public"
Header set Expires "Thu, 15 Apr 2010 20:00:00 GMT"
Header unset Last-Modified
</FilesMatch>

I removed this post How to force cache cleaning in my visitors' browsers

    
23.06.2017 / 16:02