Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rails 4 asset pipeline doesn't overwrite assets with asset_path references to other assets

So we have a small problem with asset pipeline digests in that we have a number of HTML templates as assets (for AngularJS) and we references the paths for these assets in a Javascript file with the asset_path helper. Unfortunately because of how the asset precompile step (and Heroku) check for changes, making changes to a template does not cause the javascript file to be recompiled.

So basically in a file like application.coffee.erb we might have something like:

url = '<%= asset_path('views/template.html') %>'

And when we run rake assets:precompile that will get turned into /assets/views/template-1023911231.html which has a digest value but if template.html.slim is changed the digest will change but since application.coffee.erb has not fundamentally changed it isn't recompiled and continues to point to the old digest.

Thoughts? Solutions? This seems like a bug to me actually.

Update

Currently the best solution I have is to update the asset version in application.rb if we need to change our HTML template assets but none of the javascript has changed.

like image 942
Chris Nicola Avatar asked Oct 25 '13 20:10

Chris Nicola


People also ask

How does Rails asset pipeline work?

The asset pipeline provides a framework to concatenate and minify or compress JavaScript and CSS assets. It also adds the ability to write these assets in other languages such as CoffeeScript, Sass and ERB. Prior to Rails 3.1 these features were added through third-party Ruby libraries such as Jammit and Sprockets.

How do you Precompile Rails assets?

To compile your assets locally, run the assets:precompile task locally on your app. Make sure to use the production environment so that the production version of your assets are generated. A public/assets directory will be created. Inside this directory you'll find a manifest.

What does rake assets Clean do?

rake assets:clean removes compiled assets. It is run by cap deploy:assets:clean to remove compiled assets, generally from a remote server.


2 Answers

A solution was suggested here which involves using the depend_on or depend_on_asset directive. In my case adding this to the .js.erb file works:

//= depend_on_asset views/template.html

url = '<%= asset_path('views/template.html') %>'

This will recompile this file any time there is a change to views/template.html as desired.

like image 189
Chris Nicola Avatar answered Sep 30 '22 07:09

Chris Nicola


Have you tried running rake assets:clean then rake assets:precompile to recompile?

UPDATE Not sure if this is the same issue you are facing but it does sound similar. Take a look at this Heroku issue on github.

If it is the same (or similar), Schneems mentions it takes 3 changes to an asset file for sprockets to clear out the files.

The recommended steps is to run the following 4 times and compare output:

echo "body {background-color: red}">> app/assets/stylesheets/application.css
git add .; git commit -m "assets changed 1"
git push heroku master
heroku run ls public/assets | awk /application/

It's probably best you modify the above to suit your situation.

like image 26
8bithero Avatar answered Sep 30 '22 09:09

8bithero