# Caching CSS Issue

**URL:** <https://www.ruby-forum.com/t/caching-css-issue/126747>\
**Category:** Rails\
**Created:** [January 21, 2008, 10:10pm UTC](https://www.ruby-forum.com/t/caching-css-issue/126747 "2008-01-21T22:10:59Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [January 21, 2008, 10:10pm UTC](https://www.ruby-forum.com/t/caching-css-issue/126747/1 "2008-01-21T22:10:59Z")

</div>

I have a problem I was hoping someone could shed some light on. I  
noticed that with every request to my server, the css file was being  
requested. I went ahead and made a static link to the css file so  
there is no query string at all, but still I see the requests coming  
in with each request. Do I need to do anything else (in the response  
header maybe?) to make sure that browsers will cache the css?

I am on Rails 2.0.2

Thanks,  
Tom

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [January 21, 2008, 11:41pm UTC](https://www.ruby-forum.com/t/caching-css-issue/126747/2 "2008-01-21T23:41:24Z")

</div>

Are you running in production mode? If so, then browsers should  
automatically cache the CSS file, afaik.

Because of how the request to load a page works it might always include  
the  
CSS file.

On Jan 22, 2008 7:40 AM, TomRossi7 [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Thanks,  
> Tom
> 
> >

–  
Ryan B.

> **[frozenplague.net](http://www.frozenplague.net)**
>
> This domain may be for sale!

Feel free to add me to MSN and/or GTalk as this email.

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [January 22, 2008, 1:58am UTC](https://www.ruby-forum.com/t/caching-css-issue/126747/3 "2008-01-22T01:58:23Z")

</div>

I am running the server in production mode and the css is actually  
being rendered by a controller (e.g. /css/my\_dynamic.css). I’ve  
noticed that each time the page is hit, I see a request for the CSS.

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [January 22, 2008, 2:04am UTC](https://www.ruby-forum.com/t/caching-css-issue/126747/4 "2008-01-22T02:04:51Z")

</div>

If you’re dynamically creating the CSS file every time, it will be  
requested by the browser every time. This may probably because the  
modification date changes every time you generate the file. (I don’t  
really know–I’m just guessing.) Anyway, Ryan B. has a screencast  
you should check out:

> **[Ruby on Rails Screencasts - RailsCasts](http://railscasts.com/)**
>
> Short Ruby on Rails screencasts containing tips, tricks and tutorials. Great for both novice and experienced web developers.

In episode #88 he creates a dynamic Javascript file. In episode #89 he  
caches that javascript file. Sounds like that might be just what  
you’re looking for.

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [January 22, 2008, 3:29am UTC](https://www.ruby-forum.com/t/caching-css-issue/126747/5 "2008-01-22T03:29:36Z")

</div>

The timestamp query string is only created if you use the rails helper  
to link to the css. I went ahead and created a static link with no  
query string, but the browser still won’t cache it! I’m wondering if  
I need to set something in the response header to tell the browser to  
cache.

Thanks for pointing out the screencast, I’ll definitely check them out  
but I would imagine he is talking about server-side caching…

–Tom
