rails 3.1 asset pipeline css caching in developmen

2020-02-17 01:44发布

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

10条回答
走好不送
2楼-- · 2020-02-17 01:54

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.

查看更多
Luminary・发光体
3楼-- · 2020-02-17 01:56

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!

查看更多
Deceive 欺骗
4楼-- · 2020-02-17 02:00

@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.

查看更多
【Aperson】
5楼-- · 2020-02-17 02:06

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.

查看更多
贪生不怕死
6楼-- · 2020-02-17 02:08

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

查看更多
我欲成王,谁敢阻挡
7楼-- · 2020-02-17 02:09

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.

查看更多
登录 后发表回答