# Caches\_page caching a javascript response

**URL:** <https://www.ruby-forum.com/t/caches-page-caching-a-javascript-response/167679>\
**Category:** Rails\
**Created:** [May 11, 2009, 12:41pm UTC](https://www.ruby-forum.com/t/caches-page-caching-a-javascript-response/167679 "2009-05-11T12:41:06Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![diarmuid](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/46a35a/32.png) [@diarmuid](https://www.ruby-forum.com/u/diarmuid)\
**Post date:** [May 11, 2009, 12:41pm UTC](https://www.ruby-forum.com/t/caches-page-caching-a-javascript-response/167679/1 "2009-05-11T12:41:06Z")

</div>

I am using caches\_page to cache a controller action which returns  
javascript

caches\_page :show\_county

def show\_county  
@county = County.find(params[:id])

```
respond_to do |format|
  format.js
end

```

However when I hit the page, the server caches it as a html:

Cached page: /home/show\_county/22.html (1.1ms)

This is obviously causing problems on subsequent hits.  
I am using rails 2.2.2

Any ideas on why the page is cached as .html and how do I rectify  
this?

Diarmuid

---

<div class="post-metadata">

**Author:** ![diarmuid](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/46a35a/32.png) [@diarmuid](https://www.ruby-forum.com/u/diarmuid)\
**Post date:** [May 11, 2009, 1:26pm UTC](https://www.ruby-forum.com/t/caches-page-caching-a-javascript-response/167679/2 "2009-05-11T13:26:58Z")

</div>

On Mon, May 11, 2009 at 2:36 AM, Diarmuid  
\<removed\_email\_address@domain.invalid

> wrote:

> ```
> format.js
> 
> ```
> 
> Any ideas on why the page is cached as .html and how do I rectify  
> this?
> 
> Diarmuid

Diamuid, this is the standard behavior of caches\_page. If you’re  
looking to cache only HTML, I would recommend the following:

caches\_page :show\_county, :if =\> Proc.new { |c| c.request.format.html? }

Good luck,

-Conrad

---

<div class="post-metadata">

**Author:** ![diarmuid](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/46a35a/32.png) [@diarmuid](https://www.ruby-forum.com/u/diarmuid)\
**Post date:** [May 11, 2009, 1:46pm UTC](https://www.ruby-forum.com/t/caches-page-caching-a-javascript-response/167679/3 "2009-05-11T13:46:23Z")

</div>

Conrad

Thanks for the response.

However my problem is that I _want_ it to cache the response as a _js_  
file (ie 22.js) and then on subsequent hits the server will return  
this file.

I tried :

caches\_page :show\_county, :if =\> Proc.new {|c|  
c.request.format.js?  
}

but that didn’t help matters.

Even in a Railscast I watched ([#89 Page Caching - RailsCasts](http://railscasts.com/episodes/89-page-)  
caching) I can see the correct behaviour but for some reason I cannot  
replicate that behaviour

---

<div class="post-metadata">

**Author:** ![diarmuid](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/46a35a/32.png) [@diarmuid](https://www.ruby-forum.com/u/diarmuid)\
**Post date:** [May 11, 2009, 2:49pm UTC](https://www.ruby-forum.com/t/caches-page-caching-a-javascript-response/167679/4 "2009-05-11T14:49:47Z")

</div>

OK

I think I have solved this issue. Adding the solution to help others.  
I also have one question that I don’t undestand below, maybe someone  
could enlighten me

The problem is that by default the caching mechanism will use the same  
extension of the request to store the cached file. If no extension  
exists (/home/show\_county/22) then the default is .html

Next was the figure out how to get the request to have the js  
extension.

This is done by setting an argument to url\_for :format =\> :js This  
doesn’t seem to be well documented ([Ruby on Rails - APIdock](http://apidock.com/rails/)  
ActionController/Base/url\_for only in the comments!) but it did work.

_However as a consequence of this I had to rename some views from  
xxx.html.erb to xxx.erb, I don’t know why? Anyone?_

Once I did this, the request was sent as a .js which was then cached  
as a 22.js and on subsequent hits was returned as a javascript file.

Done

Diarmuid

On May 11, 1:45 pm, Diarmuid [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)
