leshill / handlebars_assets

Use handlebars.js templates with the Rails asset pipeline.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Info about precompile assets on Heroku with custom config

wyaeld opened this issue · comments

Hi Les, it turns out the quirks of the assets precompile stage on Heroku can make things a little tricky if you are using HandlebarsAssets to customize the compiler. I thought I'd share the findings, and maybe you could include a note in the docs to make life easier for others.

This is not a bug of any sort, just sharing info gained.

The issue is while in "development" putting the following in an initialiser or config

HandlebarsAssets::Config.options = { data: true }

works perfectly fine, but they won't compile that way on Heroku if you aren't precompiling prior to pushing, because Heroku's compile step runs prior, and usually most people have

config.assets.initialize_on_precompile = false

and HandlebarsAssets is usually in the "assets" group in Gemfile, so not available in Production

My solution is to add a form of the following:

if "assets" == ENV["RAILS_GROUPS"] || ["development", "test"].include?(ENV["RAILS_ENV"])
  HandlebarsAssets::Config.options = { data: true }
end

Now assets:precompile under every scenario I've encountered compiles with the custom options. Hope this is useful.

Thanks for posting this! Helped me out big time!

Hi @wyaeld,

Added a link here in the README.

Thanks!

The explanation here is unclear.

if "assets" == ENV["RAILS_GROUPS"] || ["development", "test"].include?(ENV["RAILS_ENV"])
  HandlebarsAssets::Config.options = { data: true }
  HandlebarsAssets::Config.template_namespace = 'JST'
end

Adding the code on top to config/initializers/ or config/enviroments/production.rb does not seem to work. I finally got it working on Heroku after I added the snippet to to application.rb

Hi Eddie, sorry it wasn't clear enough.

The way that Heroku interacts with the assets pipeline means the compile step is actually happening before the app is aware that its it production, so as you will have noticed, adding it to the production.rb isn't enough.

Your solution is correct, its in my config/application.rb

Unfortunately we might need an alternative for Rails4. According to Ryan here
http://railscasts.com/episodes/415-upgrading-to-rails-4
The assets gems no longer have to be in the "assets" group, which may interfere with it. Right now I'm not working on a Rails4 app and don't really have the time to test that theory

Thanks for posting! Works great. I'm using handlebars with ember right now, so I had to add one more line:
if "assets" == ENV["RAILS_GROUPS"] || ["development", "test"].include?(ENV["RAILS_ENV"])
HandlebarsAssets::Config.options = { data: true }
HandlebarsAssets::Config.ember = true
end