开发者

expires headers? How do I implement them and what are they?

I have tried searching how to implement them but it makes no sense to me. It talks about apache and Django and Lighttpd, but I have no idea what those are.

I used ySlow on my webpage and it told me I n开发者_如何学运维eed an expires header.

Apparently it really helps with performance though and that's what I really need.


It has nothing to do with jQuery. Your server's response should set the appropriate headers like Expires, E-Tag etc.

What language are you using on the server side?

If you're using PHP, an example of sending the Expires header would be:

header("Expires: Tue, 31 Aug 2010 02:30:00 GMT");


Expires is an HTTP header field that cannot be set by jQuery.

But you could try to add an HTTP equivalent META element:

$("head").append('<META http-equiv="Expires" content="Tue, 20 Aug 1996 14:25:27 GMT">')

But it depends on the user agent if such an HTTP equivalent META element is recognized. The better way would be to set that in the HTTP header.

By the way: In HTTP 1.1 the Cache-Control header field with max-age parameter is preferred over Expires (see Modifications of the Basic Expiration Mechanism).


The Expires header is outside of the actual page itself, it's meta-data which the server adds to responses to indicate to the client how long the content is valid for.

Ultimately, it's up to the server to set the response headers. This can be done globally, so that all content sent from the server has the same values. Or, if you're using a server-side platform (like ASP.NET, PHP, etc) then you can set the Expires header programatically and on a per-resource (page) basis.

However, it sounds like you just need to set them globally--which normally is done with a server-setting.

For Apache take a look at mod_expires

For IIS7, take a look at this

For other server platforms, just try googling "Howto set expires header {server}" with {server} being whatever platform/version you need.

And to clarify what they are If a browser has previously retrieved a resource (say, myPage.html), and that resource has an expiration of 24h, then the browser is essentially being told "if you try to load this page again in the next 24h, you can just show the version you previously retrieved rather than requesting a new copy from the server".

For static pages this can be ideal--a longer expiration can result in faster page-loads for your users (the browser saves trips to the server) and the server has to handle fewer requests.

However; for dynamic pages having a long-expiration can be detrimental. Imagine a page which just tells the time like <h1>1:01PM EST</h1> (where the server generates the HTML). If the expires header is set to something like 1h, then it the browser may show the user "1:01 EST" when it shoudl be "1:45 EST", etc.

If you need to explicitly disable browser-caching (different browsers use different defaults, ie: IE8 is very aggressive about caching), then you can set Expires=-1 which is essentially saying the page expires immediately.


An Expires header is used for HTTP caching and indicates the next date/time at which the current version of the object is no longer current. This can be used by browsers and HTTP caches to reduce the load on source web servers.

There are some other headers involved in caching that you should also investigate like ETag and Cache-Control.

See: http://www.w3.org/Protocols/rfc2616/rfc2616-sec13.html#sec13

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜