6

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.

2 Answers 2

5

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.

4
  • Hi. In which file did you add the depend_on directive? Was it in your angular routing file? Nov 25, 2013 at 19:32
  • Add depend_on (or its cousin depend_on_asset) in the same file that is referencing the asset it depends on. Remember to put the dependency declaration at the top of the file (before the first non-whitespace line of code).
    – Domi
    Dec 11, 2013 at 10:15
  • @ChrisNicola I am in a similar situation (see my SO post here), except that my calls to asset_path don't return a fingerprint at all. Adding //= depend_on_asset didn't help. Did you have to add any special configuration to get this to use fingerprints in the first place?
    – hunteros
    Apr 14, 2014 at 22:55
  • No assuming your assets are fingerprinted in production asset_path should result in the fingerprinted URL in the result of your compiled js.erb (or whatever you are compiling). depends_on_asset is important because otherwise Rails won't recompile the JS file when the fingerprint changes. If your not getting any fingerprint at all something else must be wrong. Apr 15, 2014 at 20:24
2

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.

5
  • Interestingly enough, no it doesn't seem to have any effect. Oct 25, 2013 at 22:27
  • I came across a Heroku issue that sounds similar to what you are facing. Please take a look at my updated post.
    – 8bithero
    Oct 26, 2013 at 20:53
  • Sort of, not quite though but the fact that rake assets:clean doesn't seem to do anything is interesting. This is actually that a new digest and compiled version isn't created for assets that have not changed but have asset_url references to ones that have. Does that make sense? Oct 28, 2013 at 5:40
  • Hmmm, yeah that is strange. If you're using Rails4, you should use rake assets:clobber instead of clean... So you tried running the above and it had no effect? The only other solution I can think of is to try manually deleting all the compiled assets and make sure you've cleared your browser cache. Other than that perhaps you should report this as an issue?
    – 8bithero
    Oct 28, 2013 at 7:08
  • Yeah I've opened an issue on this. Doesn't seem to be any good solution other than incrementing the asset version github.com/rails/sprockets-rails/issues/95 Oct 28, 2013 at 21:58

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.