146

I am developing a single page Javascript application in MAMP. My JavaScript and HTML template files are getting cached between requests.

Is there a simple way to indicate in MAMP that I want to prevent http file caching? Possibly with a .htaccess file? Where do I place the .htaccess or modify the virtual host for MAMP on Mac?

Rishabh
  • 3,312
  • 4
  • 42
  • 69
dmck
  • 7,561
  • 7
  • 41
  • 76

5 Answers5

328

Tried this? Should work in both .htaccess, httpd.conf and in a VirtualHost (usually placed in httpd-vhosts.conf if you have included it from your httpd.conf)

<filesMatch "\.(html|htm|js|css)$">
  FileETag None
  <ifModule mod_headers.c>
     Header unset ETag
     Header set Cache-Control "max-age=0, no-cache, no-store, must-revalidate"
     Header set Pragma "no-cache"
     Header set Expires "Wed, 11 Jan 1984 05:00:00 GMT"
  </ifModule>
</filesMatch>

100% Prevent Files from being cached

This is similar to how google ads employ the header Cache-Control: private, x-gzip-ok="" > to prevent caching of ads by proxies and clients.

From http://www.askapache.com/htaccess/using-http-headers-with-htaccess.html

And optionally add the extension for the template files you are retrieving if you are using an extension other than .html for those.

Community
  • 1
  • 1
Charlie Rudenstål
  • 4,188
  • 1
  • 11
  • 15
  • 2
    Thank you, this works very well in a .htaccess file. I can see the cache control options when I inspect the headers and my files are showing up with http 200 instead of 304 between requests, just what I needed. – dmck Jul 30 '12 at 22:26
  • 2
    Don't forget to add the LoadModule command. `LoadModule headers_module lib/modules/mod_headers.so` – Spenhouet Sep 22 '14 at 14:24
  • Can I apply those header to all file types, instead of defining extensions one by one? – Thariq Nugrohotomo Jan 13 '17 at 03:17
  • 1
    @ThariqNugrohotomo Yes it's possible! It's a regular expression, you can try `` or `` – Charlie Rudenstål Jan 25 '17 at 11:28
  • I'm not using MAMP but Apache on a web server. I had to also enable headers and expires modules on my Apache installation by typing "sudo a2enmod headers" and "sudo a2enmod expires", and then sudo service apache2 restart. The LoadModule and IfModule commands/tags weren't necessary -- just omit the bracketed IfModule tag parts above but still enter the Header settings between them. – – andruo11 Jun 10 '18 at 20:13
  • Doesn't seem to work. If I change a file on the server and refresh the page, nothing happens, but if I wait 5 minutes, it does change. There's some kind of server-side cache that persists even after restarting Apache. – Aaron Franke Jan 06 '19 at 18:14
  • For this peculiar Expires date see [Why does WordPress use 11 Jan 1984 as an anti-caching value for Expires headers?](//stackoverflow.com/a/54013458) and for a very similar date used in PHP cf. [Why is "Expires" 1981?](https://stackoverflow.com/q/8194481/) – Speravir Dec 01 '19 at 19:31
5

Based on the example here: http://drupal.org/node/550488

The following will probably work in .htaccess

 <IfModule mod_expires.c>
   # Enable expirations.
   ExpiresActive On

   # Cache all files for 2 weeks after access (A).
   ExpiresDefault A1209600

  <FilesMatch (\.js|\.html)$>
     ExpiresActive Off
  </FilesMatch>
 </IfModule>
Frank Farmer
  • 35,103
  • 11
  • 67
  • 86
  • Unfortunately this does not work when I put a .htaccess file in my project directory. I'm not sure if mod_expires is active. – dmck Jul 24 '12 at 00:37
  • 5
    @dmck: remove the `` and `` section .. if mod_expires isn't enabled, you'll get an error instead of those directives quietly being ignored. – Stennie Jul 28 '12 at 13:15
  • In a project directory any slight syntax error here can cause a 500 error. – SDsolar Oct 27 '17 at 15:21
3

I had the same issue, but I found a good solution here: Stop caching for PHP 5.5.3 in MAMP

Basically find the php.ini file and comment out the OPCache lines. I hope this alternative answer helps others else out as well.

Community
  • 1
  • 1
acarito
  • 616
  • 7
  • 15
  • 3
    that is about PHP caching on the server side, a.k.a OPCache. The question is about files being cached by the browser that are requested over HTTP. – Flion Jan 30 '16 at 17:56
2

Without mod_expires it will be harder to set expiration headers on your files. For anything generated you can certainly set some default headers on the answer, doing the job of mod_expires like that:

<?php header('Expires: '.gmdate('D, d M Y H:i:s \G\M\T', time() + 3600)); ?>

(taken from: Stack Overflow answer from @brianegge, where the mod_expires solution is also explained)

Now this won't work for static files, like your javascript files. As for static files there is only apache (without any expiration module) between the browser and the source file. To prevent caching of javascript files, which is done on your browser, you can use a random token at the end of the js url, something like ?rd=45642111, so the url looks like:

<script type="texte/javascript" src="my/url/myjs.js?rd=4221159546">

If this url on the page is generated by a PHP file you can simply add the random part with PHP. This way of randomizing url by simply appending random query string parameters is the base thing upôn no-cache setting of ajax jQuery request for example. The browser will never consider 2 url having different query strings to be the same, and will never use the cached version.

EDIT

Note that you should alos test mod_headers. If you have mod_headers you can maybe set the Expires headers directly with the Header keyword.

Community
  • 1
  • 1
regilero
  • 27,883
  • 6
  • 54
  • 94
1
<FilesMatch "\.(js|css)$">
  ExpiresActive On
  ExpiresDefault A1
  Header append Cache-Control must-revalidate
</FilesMatch>
Bhargav Rao
  • 41,091
  • 27
  • 112
  • 129
  • That's not a good quality answer. Try to explain in detail what are the steps for resolving the issue reported on the OP. – leopal Sep 03 '19 at 06:33