Accessing fingerprinted Ember CLI assets in Rails


#1

I’ve recently moved my Ember app out of the Rails asset pipeline and into a separate Ember CLI app. I still have some pages I want Rails to render. I would like Rails to be able to use Ember’s CSS and image assets rather than duplicating them. In development I could just reference them with something like /assets/app.css (my Rail’s assets start with /rails-assets), but in production the files will be fingerprinted. I know that broccoli-asset-rev produces a manifest.json that can be used by Rails but I don’t know how to configure Rails to use it. Anybody mange to get this working?


#2

When I looked into this briefly a few months ago, one solution I read was to create a helper function like asset_path that reads in the manifest.json file and gives you the fingerprinted url. I only had 2 assets I needed to share so I ended up just duplicating them but if I you have a lot this would probably make sense.

Another idea I had was If you’re using ember-cli-delpoy, you could probably modify it to push the manifest contents into redis.


#3

one solution I read was to create a helper function like asset_path that reads in the manifest.json file and gives you the fingerprinted url

Thanks for that. That’s probably the path I’m going to have to go down.

In case it might be helpful to others this is what I’ve figured out so far. It presumes your file structure is like:

ember
rails

ember/Brocfile.js:

var app = new EmberApp({
  fingerprint: {
    generateRailsManifest: true,
    exclude: ['manifest.json']
  }
});

To generate fingerprinted assets:

ember build --environment production

rails/config/environment/production.rb:

config.assets.paths << "#{Rails.root}/../ember/dist/assets"

And then copy ember/dist/assets/manifest.json to rails/public/assets deleting the one that’s already there as it picks up the first one it finds.

rails server -e production

Get Nginx to serve the assets from ember’s dist directory rather than Rails:

location ~ ^/assets/ {
  root /path/to/ember/dist;
  break;
}

In asset helpers like image_tag you will need to refer to the image as “images/image.png” rather than “image.png” i.e.

<%= image_tag("images/image.png") %>

In Rail’s manifest the key for the files is just the filename but in broccoli-asset-rev the key includes the directory.