Rui Jiang
Rui Jiang

Reputation: 1672

Rails using cached application.css despite changes

I have a Rails 3.1 application that uses SASS. The application.css.scss file looks like:

@import 'reset.css';
@import '960.css';
@import 'pages/master.css.scss';

I have a watchr script that touches application.css.scss whenever one of the @imported files is changed.

For a while this setup worked fine. Ever since last week (and I'm not sure why), Rails has been pulling a cached version of application.css for the webpages despite all my attempts at restarting the app, re-touching application.css.scss, etc. I've also deleted .sass-cache to no effect.

Any ideas?

Upvotes: 23

Views: 13359

Answers (9)

Greg Dan
Greg Dan

Reputation: 6298

I had the same issue. I stopped the server, executed rm -fr tmp/cache, and my css files were finally rebuilt.

Upvotes: 15

cianmce
cianmce

Reputation: 494

If you replace @import 'file.css'; with @import 'file'; in your application.scss that should allow it to be auto-refreshed in development mode

I had been using bin/rails tmp:clear until I removed the file extension

Upvotes: 9

user10375
user10375

Reputation: 635

I had a similar issue with Rails 6.0.3.1 today. I had to re-touch application.scss so that rails didn't use the cached version.

Running rake tmp:cache:clear helped, but it's annoying because I had to run the command every time I updated any partial scss file. :(

Upvotes: 0

Hari Honor
Hari Honor

Reputation: 8914

You can invoke Rails automagic cache busting by doing the following:

  1. Rename the SASS file as a partial, i.e. with an underscore prefix. E.g. _master.css.scss
  2. Remove the extension from the import. You can keep the paths, but exclude the underscore. E.g. @import 'pages/master';

Now you can make changes in master and have them reflected without messing with the cache manually.

(Note: I'm on Rails 4.2)

Upvotes: 2

Christopher Davies
Christopher Davies

Reputation: 4551

I was having this problem, but I was actually putting css into application.css, rather than into files that it included. I cleared out application.css to simply include all of my css. So, my application.css now looks like this:

/*
 * This is a manifest file that'll be compiled into application.css, which will include all the files
 * listed below.
 *
 * Any CSS and SCSS file within this directory, lib/assets/stylesheets, vendor/assets/stylesheets,
 * or vendor/assets/stylesheets of plugins, if any, can be referenced here using a relative path.
 *
 * You're free to add application-wide styles to this file and they'll appear at the top of the
 * compiled file, but it's generally better to create a new file per style scope.
 *
 *= require_self
 *= require 'bootstrap.min'
 *= require 'global'
 */

My changes are reflected any time I change one of the included files (e.g. global.css)

Upvotes: -1

Andrei Nistor
Andrei Nistor

Reputation: 535

I've had a similiar issue after running rake assets:precompile in development. Maybe Rails is serving precompiled assets from public/assets? Try cleaning that up.

You shouldn't need to touch aplication.css.scss in development, rails should serve the new content whenever one of the @included files changes.

Also, make sure you have the following in config/environments/development.rb

# Do not compress assets
config.assets.compress = false

# Expands the lines which load the assets
config.assets.debug = true

Upvotes: 8

Abram
Abram

Reputation: 41874

To wipe out the asset pipeline cache, a brute force rm -rf tmp/* will suffice. This has certainly fixed a few otherwise inexplicable CSS and JavaScript glitches in my experience. As a preventative measure, it might also be a good idea to clear the cache after upgrading gems or changing the asset pipeline configuration, although this may just be superstition.

Finally, if you are experimenting with rake assets:precompile in your development environment (more on this in a later article), you’ll also want to rm -rf public/assets/* afterwards to clean that up.

http://blog.55minutes.com/2012/02/untangling-the-rails-asset-pipeline-part-1-caches-and-compass/

Upvotes: 3

yuvilio
yuvilio

Reputation: 4185

First, the usual cache clearing sanity checks might help. Clear browser cache. Clear server file cache (if you're in dev/test or can afford to in production) and sass-cache :

rake tmp:cache:clear
rm -fr tmp/sass-cache #or 'compass clean' if using compass

If that doesn't help, maybe Rails compiled ANOTHER application.css elsewhere (that didn't get removed by the cache clearing)?

For example, I ran compass watch app/assets/stylesheets/application.css.scss for debugging purposes and it created a public/assets/application.css file which, by virtue of it's location in public/, prevented any new application.css.scss stylesheet changes from being noticed by Rails. Once I removed it, the application again pulled from the .scss stylesheets. This is just one example of accidental overriding file creation. Try running a find on the entire application directory looking for any generated application.css files, doing this after the cache clearing to avoid those showing up in your results.

(FYI, to avoid my specific issue, I now run compass watch with --css-dir pointed at the cache to prevent my issue

$ compass watch app/assets/stylesheets/application.css.scss  --css-dir tmp/cache/

)

Upvotes: 8

hajpoj
hajpoj

Reputation: 13379

Random I was having a caching issue related to using twitter bootstrap and application.css.scss. Basically I changed application.css.scss to just plain application.css and fixed my problem. Maybe this can help you? If you haven't figured it out already.

Upvotes: -1

Related Questions