js_assets icon indicating copy to clipboard operation
js_assets copied to clipboard

app_assets.js not updated when a new file is added

Open coli opened this issue 11 years ago • 10 comments

A restart of the server is needed to get the new file detected. I believe this is due to the erb file being cached.

coli avatar Apr 29 '14 21:04 coli

Yes, there is a problem: when you add a new file app_assets.js not updated. It is not associated with the cache Sprockets. Forced can reset it like this:

$ rm -rf tmp/cache

But the correct solution I have not found.

izaurio avatar Apr 30 '14 05:04 izaurio

I have same problem

artempartos avatar Jul 04 '14 08:07 artempartos

To automatically update app_assets.js when adding new files in app/assets, do the following steps. Add to Gemfile:

group :development do
  gem 'guard'
  gem 'guard-shell'
end

Add to Guardfile:

guard :shell do
    watch(%r{^app/assets/.*}) { `rm -rf tmp/cache` }
end

Run the command bundle exec to install the gems. Before starting to develop run guard:

$ bundle exec guard

Warning! This may adversely affect the rate of return assets list in the development environment. Since they will be compiled at each change.

izaurio avatar Sep 19 '14 06:09 izaurio

@kavkaz that fix doesn't work when you are deploying to a production environment (e.g. Heroku)

jeremyhaile avatar Mar 03 '15 16:03 jeremyhaile

@jeremyhaile Production rails doesn't support changing assets without a reboot. Am I not understanding?

mgenereu avatar Apr 08 '15 16:04 mgenereu

@kavkaz Could you review the following:

<%# Overrides app_assets.js.erb in js_assets gem %>
<% assets = JsAssets::List.fetch %>
<% assets.keys.map { |asset| depend_on asset } %>
window.project_assets = <%= assets.to_json %>;
window.asset_path = function(logical_path) {
return window.project_assets[logical_path];
};

That solved updates and deletes but I can't get Sprockets 2.x (Rails version) to monitor whole directories for adds. May have been fixed in Sprockets 3.0 by @josh.

mgenereu avatar Apr 08 '15 16:04 mgenereu

This is what I really wanted:

<% assets = JsAssets::List.fetch %>
<% assets.keys.map { |asset| Pathname.new( asset ).dirname }.uniq.each { |path| depend_on path  } %>
window.project_assets = <%= assets.to_json %>;
window.asset_path = function(logical_path) {
return window.project_assets[logical_path];
};

mgenereu avatar Apr 08 '15 16:04 mgenereu

@mgenereu I tried your patch, but I'm getting the following crash:

couldn't find file '.' under '/Users/jeremy/projects/allynova/app/assets/javascripts'

crashing on this line:

<% assets.keys.map { |asset| Pathname.new( asset ).dirname }.uniq.each { |path| depend_on path } %>

CyborgMaster avatar Sep 14 '16 19:09 CyborgMaster

Any updates on this bug? Has someone found a way to make it work on production environments such as Heroku?

menisy avatar Nov 25 '20 08:11 menisy

Seems like purging the build cache does the trick for Heroku. You can do this using Heroku CLI from your terminal as indicated here:

$ heroku plugins:install heroku-builds
$ heroku builds:cache:purge -a appname

You then need to make an empty commit and push it to invoke a new deployment.

menisy avatar Nov 25 '20 16:11 menisy