Why does a rails app on heroku serve assets via all.css and locally via individual files

|▌冷眼眸甩不掉的悲伤 提交于 2019-11-30 11:58:30

This is the result of calling :cache => true on your stylesheet link tag.

:cache => true takes all of the stylesheets provided and concatenates them into one file called all.css.

The reason you're only seeing this on your Heroku deployment is because it calls the concatenated all.css only when the Rails application is running in production mode.

So for example let's say I have three stylesheets and I include them in my header:

= stylesheet_link_tag "application", "jquery-ui", "style", :cache => true

When in development, this will include application.css, jquery-ui.css, and style.css (in that order).

In production, it will concatenate all of the CSS from the three files (in the order provided) into one single file called "all.css", which will be the only CSS file included.

The benefit is making fewer HTTP requests in production and ideally a smaller file size for your included CSS, which should hopefully speed up page load.

Edit As Casper points out in the comments, Heroku has a read-only filesystem. You might want to look at Heroku Asset Packager for a Heroku-specific solution.

Tested this and it did not work for me (adding config.serve_static_assets = true to production.rb)

Setting :cache => true causes my requests to fail outright.

My solution for the short term is to add the following to my config/environments/prodcution.rb

config.serve_static_assets = true

I'm slightly less worried about the performance being behind Cloudflare. Finding a way to serve my css and js files concatenated is on my to-do list.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!