0

First things first I should say I spend my time to read all topics in this case but no success, recently I faced with a problem with client/browser cache on my website, It occurred without any change. Server side cache is good but client side working so bad, I should press CTRL+F5 everytime, I don't want this because it is bad for users, I know I can disable cache when devTool is open, but I'm talking about user not just myself. This happen on desktop and mobile device too. In mobile device I should go to setting/privacy/clear cache.

Here is my website codes relate to cache:

htaccess:mod_expires

ExpiresByType text/css "access plus 1 month"

I removed css from gzip, but no success. Also changing 1 month to 1 second.

mod_gzip_item_include file .(html?|txt|css|js|php|pl)$

PHP header:

header("Cache-Control: no-store, no-cache, must-revalidate, max-age=0");
header("Cache-Control: post-check=0, pre-check=0", false);
header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT");
header("Pragma: no-cache");
header("Vary: Accept-Encoding");
header("Expires: Sat, 26 Jul 1997 05:00:00 GMT");

HTML meta:

<meta http-equiv="Cache-Control" content="no-cache" />
<meta http-equiv="Pragma" content="no-cache" />
<meta http-equiv="Expires" content="0" />

As you see I tried all possible way to fight with annoying cache but no success. I know I can add version at the end of css or js but all know this is a bad habit to clear cache:

Remove query strings from static resources

Resources with a "?" in the URL are not cached by some proxy caching servers. Remove the query string and encode the parameters into the URL for the following resources:

source

So, what is the best to remove css and js heavy cache in the right direction?

Jack The Baker
  • 1,781
  • 1
  • 20
  • 51
  • Possible duplicate of [How to force browser to reload cached CSS/JS files?](https://stackoverflow.com/questions/118884/how-to-force-browser-to-reload-cached-css-js-files) – holden Mar 15 '18 at 10:11

3 Answers3

1
<?php $filename "path/to/file.css";  ?>
<script src="<?php echo $filename; ?>?ver=<?php echo filemtime($filename); ?>"> 

This will cache in the browser but when you make changes in the file filetime will be updated and client will get fresh copy of the file.

Vasudevan
  • 11
  • 1
0

Try using HTTP Etags. This will allow the client to deduce if the resource needs to be reloaded from the server.

Laurent P
  • 36
  • 2
0

This works every time : add ? + random stuff at the end of your files. For instance :

<script src="path/to/file.css?8768316833"></script>

In PHP, I believe this should be done this way (I haven't written any PHP in years, but here goes :)

<script src="path/to/file.css?<?php echo rand() ?>"></script>"

This will force the browser to download a fresh copy of the files every time.

Jeremy Thille
  • 26,047
  • 12
  • 43
  • 63
  • Not really efficient as a solution, the file would be re-loaded anyway from the server regardless if changed or not... – holden Mar 14 '18 at 16:17
  • Unless I'm mistaken, isn't that what OP is asking for? Or maybe I misunderstood – Jeremy Thille Mar 14 '18 at 16:19
  • 1
    He simply wrote he doesn't want the "versioning" solution in query string; anyway i thing that both versioning and rand() parameter are, for different reasons, not effricient. Anyway, i did not dare "downvote" your answer cause, alla in all, it solves the opener's problem. – holden Mar 14 '18 at 16:24
  • 1
    At least you would have said why you downvoted, and that's rare enough :) – Jeremy Thille Mar 14 '18 at 16:32
  • Thanks for your answer, but as I said I don't want to use version, because it's bad for SEO thing. I want to do this via right way, not a trick. I more want to know what is the problem exactly. – Jack The Baker Mar 14 '18 at 17:20