Rails 3.1 on Apache, PageSpeed says “Specify Cache Validator” when using Asset Pipeline apache config

别等时光非礼了梦想. 提交于 2019-12-23 18:07:51

问题


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?


回答1:


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!



来源:https://stackoverflow.com/questions/8344186/rails-3-1-on-apache-pagespeed-says-specify-cache-validator-when-using-asset-p

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!