Why won't the browser cache my http response?

I have a REST api developed with django-tastypie. I have a couple of resources that are quite heavy but are not mutable, so I would like the browser to cache them to avoid unnecesary requests.

I've set the HTTP Expire header to a date far two years in the future, this is what the browser gets:

HTTP/1.1 200 OK

Date: Wed, 16 May 2012 17:29:33 GMT
Server: Apache/2.2.14 (Ubuntu)
Vary: Cookie,Accept-Encoding,User-Agent
Expires: Tue, 06 May 2014 17:29:33 GMT
Cache-Control: no-cache, public
Content-Encoding: gzip
Access-Control-Allow-Origin: *
Content-Length: 1051
Keep-Alive: timeout=15, max=82
Connection: Keep-Alive
Content-Type: application/json; charset=utf-8

I'm using jQuery.ajax to issue the request. The expires header looks good, but the request is made each time I refresh the page.

Upvotes: 0

Views: 228

Answers (3)

pilavdzice
pilavdzice

Reputation: 958

In your .ajax call, set the cache: attribute to true, like this:

$.ajax({ url: postUrl, type: 'POST', cache: true, /* this should be true by default, but in your case I would check this*/ data: stuff });

Upvotes: 0

Tiago Peczenyj
Tiago Peczenyj

Reputation: 4623

If this content can change, try to use ifModified: true in the jQuery.ajax

Upvotes: 0

m.edmondson
m.edmondson

Reputation: 30882

This is your problem:

Cache-Control: no-cache

from the spec:

This allows an origin server to prevent caching even by caches that have been configured to return stale responses to client requests.

Upvotes: 1

Related Questions