izaurio / js_assets

Javascript helper in rails projects
MIT License
33 stars 16 forks source link

app_assets.js not updated when a new file is added #3

Open coli opened 10 years ago

coli commented 10 years ago

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

izaurio commented 10 years ago

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.

artempartos commented 10 years ago

I have same problem

izaurio commented 10 years ago

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.

jeremyhaile commented 9 years ago

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

mgenereu commented 9 years ago

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

mgenereu commented 9 years ago

@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 commented 9 years ago

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];
};
CyborgMaster commented 8 years ago

@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 } %>

menisy commented 3 years ago

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

menisy commented 3 years ago

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.