Is there any way to clear cache in the client’s browser?

Asked

Viewed 1,743 times

0

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

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

  • 1

    There is a way that I use and it works, at the time of loading the JS or CSS, at the end of the file put a ? + time(), will look like this: meuarquivo.css? 10312000114

  • I don’t understand Everson, I could explain?

  • Depending on the CDN it can be cached for months. Usually in the DOC there are explanations about the file parameters to avoid caching. You’ve already taken a look?

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=<?php echo $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 to select your JS file and download it only when there are changes, then just change the value of the variable $version which will force the download of the file, but if you want to force the download you can always set the variable $version = time();, but I think it will force your server too much in case of many accesses.

  • For example, can I do this with CSS? force the client to "download it only when there are changes"

  • Yes following the same scheme.

  • I edited the answer see if it helps you

  • "but I think it will force your server too much in case of many accesses." I think then better not use this method, has 1 million and 200 thousand hits per month, maybe it will harm the site.

  • For example, it is Magento, it has the button in the "CLEAR CDN CACHE" panel, I clear this cache, but it seems that the changes with CSS are only applied in the CLIENT’s browser when: Or the same never visited the site or if it clears the browser’s cache.

  • I believe the file is generated again, but you can probably test the js and css file or have a versioning or it generates a file with another name.. In your case the clear/Cdn cache button could update a column and your ? v= could recover this value.

  • 1

    You can use this method yes, just do not use the time() method in the version control variable ... implement a control to change the version when you want... this can be done by creating a column in the bank ai you redeem this value and when you want to clean gives an update changing the version

  • True @HENRIQUELOBO but remember that it is a specification of HMTL 5.

  • Using the CACHE MANIFEST you can solve your problem, take a look at this question: https://answall.com/questions/9009/howit works

Show 4 more comments

0

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

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 from this post How to force cache cleanup on my visitors' browsers

Browser other questions tagged

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