Question

I've been implementing HTTP Cache on my latest rails 3.2 app (soon 4) using the helper methods like fresh_when. I was now using the same approach on index actions. Something similar to:

...

def index
  @resources = Resource.all
  fresh_when @resources
end
...

It has been working quite well for HTML requests (it responds either with 304 code or 200 plus the index.html template). But, I can't make it work for asynchronous js requests. On the client I am periodically sending a 'script' request to the same endpoint. I was expecting that it would have a similar behaviour (respond with 304 or 200 with index.js template). Thing is, it's not. It's always 200 OK and index.js. Now, I've checked the request headers, and even though the last-modified-since header is not present, the etag one is. It should work, right?

Was it helpful?

Solution

Ok, found it and it was easy: the problem was exactly the lack of the If-Modified header. I changed the $.getScript calls to $.ajax with the ifModified option set to true and it started working.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top