rails 3.1 asset pipeline css caching in developmen

2020-02-17 01:29发布

问题:

I'm a bit confused as it seems like the application.css is including itself twice, once when it lists the resources from the manifest and then a cache of that. So when I delete an individual file it still seems to stay alive inside the application.css file.

application.css (source)

/*
*= require twitter/bootstrap
*= require_self
*= require_tree ./common
*= require_tree ./helpers
*/

Which works as expected and outputs in dev mode all the relevant individual files

development.rb

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

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

output

<link href="/assets/twitter/bootstrap.css?body=1" media="screen" rel="stylesheet" type="text/css" />
<link href="/assets/application.css?body=1" media="screen" rel="stylesheet" type="text/css" />
<link href="/assets/common/announcement.css?body=1" media="screen" rel="stylesheet" type="text/css" />
<link href="/assets/common/button.css?body=1" media="screen" rel="stylesheet" type="text/css" />
<Blah blah>

application.css (output)

This should be blank? Since all I have in my application.css file is the manifest and no actual css but instead i get all my concatenated code 106kb long.

IE if I remove a file in the common directory, it doesn't go away. It is no longer listed in the output but the css still appears from the application.css

回答1:

I had a problem like this before. It was caused after I had precompiled the assets it was going after the applcation.css inside the public folder as well as in the apps directory. I'm not sure how to fix it so that it doesn't keep happening while in dev mode but if you delete your /public/assets directory it should fix it.

Check and see if you have a public/assets folder, if you do and it's full, it's probably why you're seeing double.



回答2:

There is currently (2012-09-24) a bug in rails/sprockets causing it to not properly detect imported files.

This should be fixed in rails 3.2.9 and later but in the mean time, you can work around it as follows:

  1. Kill the rails instance
  2. rm -rf tmp/cache
  3. Start the rails instance

You should now be seeing the correct css.



回答3:

You might want to look at

https://stackoverflow.com/a/7854902/686460

"Adding config.serve_static_assets = false to development.rb will prevent loading files from /public/assets"

That did it for me.



回答4:

@Agustin's solution does it for me but here are a few things you need to do:

  1. Delete everything in /tmp/cache/assets

  2. Add config.serve_static_assets = false to development.rb or test.rb (credits to @Agustin)

  3. Restart your server.

  4. Make sure your application.js / .css is not cached in your browser. Go to http://localhost:3000/assets/application.js?body=1 and hit ctrl+f5 to force refresh (you can also try appending appending a randomizer param at the end: http://localhost:3000/assets/application.js?body=1&rnd=12343 If you get something else and ctrl+f5 still hasn't helped, you need to clear your browser's cache.

Skipping either of these steps returned a cached application.js / .css conflicting with my updates in single files.



回答5:

The best way, that worked for me is to delete content of tmp/cache/* directory...



回答6:

The thing that worked for us was setting 'config.assets.debug = false'

This no longer set the HTML included CSS as href="/assets/bootstrap-new.css?body=1", instead set it up as href="/assets/bootstrap-new.css", which I think was the issue.



回答7:

I had the same problem. Despite clearing tmp/cache and public/assets the minified application.css was still cached and served up from somewhere and my changes to individual css files were not getting served.

This worked for me: in application.css remove the line *= require_self

restart server

that seems to remove the cache and if you click on application.css in your browser source you will no longer see the minified version

replace the *= require_self back into the file and continue developing.



回答8:

There was one last step required for me, but I got it fixed. Here's what I did:

  1. shut down the rails server
  2. rake assets:clean
  3. rake tmp:clear
  4. restart the rails server

Then, I refreshed my screen in Google Chrome, and IT STILL DID NOT WORK. So, I launched Firefox and voila, it was actually working. This means that Chrome was caching the old files within the browser. So, I then cleared out the browser cache in Chrome, and it worked!



回答9:

I know this is an old question, but one fix that worked for me is that I had nginx proxying to my development environment and it had a location ~ ^/(assets)/ block in the configuration. Either comment it out, or try restarting nginx to invalidate the cache. If you're developing, you'll likely just want to comment it out entirely.

I lost way too much time troubleshooting this until I remembered that.



回答10:

The assets do their job better when running the app in production environment then you'll have loading only the application.css with all the files included, and compressed, there to reduce the server request and this application.css with the compressed styles will be cached.

http://guides.rubyonrails.org/asset_pipeline.html