Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Should I set the Cache-Control header when serving up files? Or not?

I'm serving up some files via an HTTPModule in asp.net. I want to know if there are any benefits to setting, or not setting, the Cache-Control header to something (like no-cache)?

Edit: The reason I'm curious about this is because we ran in to a problem where serving up office documents over an SSL session in IE results in an error (with Cache Control set to no-cache). That is to say, you cannot download office docs over SSL in IE if you have set Cache-Control to no-cache.

Basically I want to NOT include the Cache-Control header, but wonder if it will cause problems?

Edit 2: Well, the Cache-Control header is out. I tried the suggestions below but had some problems. Any time I add an expires header, or change Cache-Control at all, when I try and open an Office 2007 document it tries to open it as a zip. (I know that they're really zip files under the covers) but when I don't use an expires header or cache-control IE opens them just fine as Office Documents. Unfortunately I don't have time to try and figure all this out - as code freeze is ten minutes from now :)

Thanks everyone for trying to help!

like image 387
Eric Avatar asked Mar 05 '10 19:03

Eric


3 Answers

According to Yahoo! and YSlow you should. See this article.

Update: Based on your comment it looks like you are trying to prevent caching. I would use:

Cache-Control: max-age=0 

To me, it's simpler and more explicit than using an Expires header.

Update 2: Sounds like you need to specify the Content-Type header for the office documents. Try using below:

Content-Type: application/octet-stream
like image 136
Taylor Leese Avatar answered Sep 28 '22 05:09

Taylor Leese


Instead of using Cache-Control you could try to set the Expires header to a past date/time.

like image 39
Josh Stodola Avatar answered Sep 28 '22 06:09

Josh Stodola


That is definitely an odd issue... but here is quick solution that could prevent your users from getting stale content:

If you append an additional querystring parameter to the end of your URL to make each request for the Office file unique, you can get away without setting the cache-control information in the header.

Your current URL may look like this:

http://mysite.com/filegetter?name=document.doc

With the additional "unique" parameter:

http://mysite.com/filegetter?name=document.doc&ts=

This will prevent the browser from giving your user a stale office file, and the method could be implemented in client or server code. The module that handles shipping the file back to your users simply ignores the portion of the URL that makes it unique to your user's browser.

like image 30
smencer Avatar answered Sep 28 '22 05:09

smencer