Why isn't my javascript & css caching?

随声附和 提交于 2019-11-28 23:43:22

Please, consider disabling ETag's!

Consider the following settings:

Header unset ETag
FileETag None
Header set Cache-Control "max-age=2678400"

The first two rules disable ETag's completely, so the browser is somewhat forced to listen to the Cache-Control header. The last rule tells the browser to cache the file 2678400 seconds, or 1 month. Tweak the settings to what suits you the most. And apply this configuration on your dir which contains the static files (by, for example, placing an .htaccess file in that dir)

Optional, if your using multiple servers to serve static content, and/or are not sure about the last-modified times those servers report, consider using:

Header unset Last-Modified

It tells Apache to not serve any Last-Modified headers, so browsers can only listen to the Cache-Control max-age header.

This settings are used by myself on lots of hightraffic websites, and disabling of ETag's and Last-Modified headers sure helped to drive traffic down to one fifth of what it used to be. Especially Internet Explorer is very sensitive to those settings.

Be warned: Disabling Last-Modified will stop browsers from asking 304 Content Not Modified requests. In my experience this is positive, because the webserver has less requests to process, and browsers rely more on the Cache-Control settings you serve. But it may or may not suit you. Some browsers will try to validate assets every few minutes if you serve them a "Last-Modified" header, and that's why I would advice to disable the use of it completly.

Oh, and if you're not sure about your caching; use http://www.redbot.org/ to test your assets, it tells you quickly what your headers mean to a browser, and how to interpret different cache-control settings you use.

YSlow reports misconfigured etags if they don't adhere to a certain pattern. Since you're compressing the css and js, the etags are being output something like this:

Etag "1e10-4889909861a80"-gzip

See the -gzip at the end? It's put there by apache (version 2 only). That is what's causing the "error". YSlow expects to see something like this:

Etag "xxxx-xxxxxxxxxxxxx"

Bascially, you can't fix this because it's not broken. So don't go crazy trying to get a perfect score if you don't know what your doing. Even that yahoo home page only gets a 90.

Kip

This YSlow error message is extremely misleading!

YSlow is actually complaining that you are using ETags at all!

YSlow runs in your browser--it has no way of knowing if ETags are configured correctly or not. As a rule of thumb, it is saying that you shouldn't use ETags because you are more likely to have them misconfigured than properly configured in a multi-server environment. (And YSlow is aimed at users with large, multi-server websites.)

Of course, if you're on a single-server setup, or if you're on a distributed server setup but know what you're doing, then ETags are just fine. But YSlow has no way of knowing this.

There is lots of discussion of this in the comments of the error description page that you should check out: http://developer.yahoo.net/blog/archives/2007/07/high_performanc_11.html

Also I found this answer on ServerFault that reiterates the point: https://serverfault.com/questions/55919/yslow-says-etags-are-misconfigured-how-to-configure-etags-properly-on-iis7

I experiences the same problem as you. Removing the etag will work.

Add the following in the config file: FileETag none

stepancheg

Yes, it is correct and well-known behavior (maybe not really needed).

Read http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html about ETag.

Probably you want to just disable ETag on server.

Edit: Also, use LiveHTTPHeaders addon to understand, what your browser does. It works better than FireBug for this task.

Hi I had the same trouble. But just putting in FileETag none didn't work

The way I fixed it (and I don't know if this is correct - but it works) was I put the

FileETag none

at the bottom of my htaccess file.

Then ySlow was happy.

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