Search code examples
ruby-on-railscsscachingdeploymentstylesheet

How do i update the CSS that is cached by my server?


I have just launched my new rails site and there are come CSS things that I need to change but i think that the CSS caching is keeping my changes from working. Is there a way to clear out the cache? What am i doing wrong? Let me know if you need more information (i couldn't think of any code that would help you) and thank you very much.


Solution

  • If you're on Rails 3.1+, you can use the Asset Pipeline, it takes care of it for you.

    If you are using Asset Pipeline and still get old files, that's likely because you forgot to recompile assets when deploying. Use rake assets:clean && rake assets:precompile on the server, it'll update the assets.

    If you are not using Asset Pipeline, then there are some old tricks you can use. For example, you can rename the file. That'll certainly make clients get the fresh version.

    Also you could add a query parameter. This very site (stackoverflow) does this, look:

    <link rel="stylesheet" type="text/css" 
          href="http://cdn.sstatic.net/stackoverflow/all.css?v=bd28d262b63e">
    

    That is, when you deploy a new version of stylesheet, update this link and increment version number. Some caching proxies might still ignore this parameter, though.