0

My goal
I would like to let browsers cache my whole website, but only download the new information when I have changed one or more files.

My situation
After some research I have found a way to do this. That is to add a Far Future Expires Header to my htaccess file and add a querystring to my files using the filemtime() function.

The problem
When I click on the address bar and type in my website address in firefox, then Firebug displays
38.3 KB (36.4 KB from cache)

When I press F5 in firefox, then Firebug displays:
241.1 KB (10.9 KB from cache)

Now I have tried to do the same with Google and they are sending HTTP header 304 back. I have read a lot about ETag and the Last Modified header, but I have heard a lot of people saying that they are not really reliable.

My question
What would be the best solution if I would like to send HTTP header 304 back if the user presses on F5, like Google?


Update
It seems that Firefox is controlling the way the cache is used and I would like to use the cache also when a user presses F5.

sysadmin1138
  • 133,124
  • 18
  • 176
  • 300

3 Answers3

0

When you use Apache2 there is a module called "mod-expires" we use it here for caching. The module set the expire header for the requests.

René Höhle
  • 1,438
  • 3
  • 17
  • 26
  • Did you read my question? –  Mar 03 '11 at 13:00
  • Yes i have read it ;) and you want an answer. We use it here. Expire header and Etags. And when you use expire headers you get 304 not modified. @Z0q – René Höhle Mar 03 '11 at 13:08
  • When you use expire headers, the browser won't even send requests to the server, so they won't return a 304 header. That will only be sent when you are using ETags or the Last-Modified header. –  Mar 03 '11 at 13:59
0

Avoid conditional requests - unless you are serving up large volumes of very large files (such as videos) then they can actually make your site much slower.

that they are not really reliable.

IME, they are very reliable - but also very inefficient.

but only download the new information when I have changed one or more files.

Don't change files - create new URLs for the new content. You can't rescind caching information already issued.

That is to add a Far Future Expires Header to my htaccess file and add a querystring to my files using the filemtime() function

Oh - sorry, you're already doing that.

Pressing f5 in your browser is not a realistic way to measure cacheability.

Yes you can make a 304 response if the browser makes a conditional request and the conditions are met by the current content - but as above it's not a good way of dealing with the situation - consider you have a spot graphic appearing 4 times on a page. After the cache has expired, the browser will send 4 conditional requests to the origin each time this page is loaded. While you can send new caching instructions in a 304 response, it's usually not very easy to do this in practice. Remember latency is far more of a problem than bandwidth.

symcbean
  • 21,009
  • 1
  • 31
  • 52
  • Thank you for your very clear answer. So you would suggest to keep the Far Future Expires Header and the querystring with `filemtime()`, but to unset the `Last-Modified` header? –  Mar 03 '11 at 14:26
  • Yes - and (assuming this apache 2.0 or later) use mod_headers to remove if-modified-since and if-none-match from request – symcbean Mar 07 '11 at 09:15
0

As suggested @symcbean, and is the recommended practice to use unique URLs to avoid any problem with stale/new content. Anyway to eliminate the 304 requests remove the ETags and Last-Modified tag.

The URL below provides good tips for speeding/caching web pages.
http://www.askapache.com/htaccess/apache-speed-last-modified.html

Sameer
  • 4,118
  • 2
  • 17
  • 11