Question

I am seeing a conflict between what Rails 3.1 guides suggest, and what Google PageSpeed suggest for managing of cached assets.

Rails 3.1 guides for asset pipeline offer a solution Apache server configuration here

The salient bit of apache config is:

<LocationMatch "^/assets/.*$">
  # Some browsers still send conditional-GET requests if there's a
  # Last-Modified header or an ETag header even if they haven't
  # reached the expiry date sent in the Expires header.
  Header unset Last-Modified
  Header unset ETag
  FileETag None
  # RFC says only cache for 1 year
  ExpiresActive On
  ExpiresDefault "access plus 1 year"
</LocationMatch>

Everything works, but when I run Google PageSpeed, it complains that I should "Specify Cache Validator" by setting either Last-Modified or ETag, listing all of the files in the assets folder.

Commenting out the Header unset Last-Modified satisfies PageSpeed.

I did a quick test of the various pageloads, and at least in Chrome, it didn't seem to make a difference -- assets were cached either way.

Does anyone know which is appropriate?

Was it helpful?

Solution

That code was cut and pasted from the original Rails helper code for reasons of consistency. (It was me that copied it).

I have checked with Steve Souders (thanks, Steve) on this, and he said the following:

You should not unset Last-Modified. I'm betting the person who wrote that was click "Reload" to do their testing. When you click "Reload" you're explicitly telling the browser to send a conditional GET. (see this)

ETag has the same issue (if you hit "Reload" it'll send a conditional GET). But it has a different issue that probably means you should unset ETag. The issue is that the default ETag syntax on Apache and IIS is causes unnecessary validation failures and wasted bytes transferred. You should either configure the syntax to workaround these problems, but most people find it easier to just unset ETags. (see this)

I'll get the Rails docs updated!

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top