alexspeller / non-stupid-digest-assets Goto Github PK
View Code? Open in Web Editor NEWFix the Rails 4 asset pipeline to generate non-digest along with digest assets
License: MIT License
Fix the Rails 4 asset pipeline to generate non-digest along with digest assets
License: MIT License
Hi Alex,
I've also expericend the "Uninitilaized constant" error:
/config/initializers/non_digest_assets.rb:2:in `<top (required)>': uninitialized constant NonStupidDigestAssets (NameError)
My problem seemed to be that I got your excellent fix via rubygems.org in version 1.0.2.
Within the gem the file "/gems/non-stupid-digest-assets-ffcd833bc92f/lib/non-stupid-digest-assets.rb" does only includes the "module Sprockets" part BUT not "module NonStupidDigestAssets" part ... for whatever reason.
I do not even know if that has to be fixed on your side or not ... I just wanted to let you know.
Best,
Ben.
The README says:
This rake task will solve this problem, but requires an extra rake task. It won't work by default with things like capistrano / heroku. And it requires you to manage the code in your app.
In fact, the rake task linked to doesn't really solve the problem, because in many/most/recommended deploy scenarios, there will be several older versions of the digest-named assets around (to support still extant older versions of your app cached somewhere), and the Rake task is non-deterministic as to which one will end up with non-digested name. (Note the keep param in sprockets task definition).
As I explore my options for doing this in a reliable way (yes, very frustrated that it's not a configurable option by default), it's tricky to figure out what's really going on and what the implications of the options are. I'd just go ahead and use this gem, but the reports it's not working in Rails 4.2 beta have me worried.
It would also be useful if you could provide more info (perhaps just links to someone else describing it) as to the downsides to the rake task approach with regard to "won't work by default with things like capistrano / heroku. And it requires you to manage the code in your app." I'm not quite sure what you mean there. I am considering pursuing a rake task that somehow does manage to identify the current/latest digest-named file to copy (although the sprockets api ain't too helpful here), but want to try and take account of potential problems in making my plans. (I've already run into SO many unexpected problems with various approaches, like the multiple-digest-named-versions one mentioned at the top here).
Thanks for this gem, and for any info you can provide!
Actually gem working well, created non digested assets in the path.
Problem comes when asset_sync gem reading rails 4 manifest file which is not include "non-digested" stuff.
solved easily with setting manifest to false in the AssetSync configuration (so it read files after precompile), eg:
AssetSync.configure do |config|
config.manifest = false
end
Does the whitelist support regular expression?
File.exists?
was removed in Ruby 3.2, causing CompileWithNonDigest.compile
to fail with NoMethodError: undefined method `exists?' for File:Class
.
This gem will not work correctly in the context of parallelizing compilation, i.e.
Even when setting
config.assets.compress = true
the non-manifested versions of an asset are never compressed
We've been having a problem with an app where sometimes it would seem like assets weren't recompiling when they should. Almost like sprockets didn't 'see' that they had changed. The weird part was that the digest version of the asset would have the change, but the non-digest version would not.
I think I've finally tracked this down to being a bug with non-stupid-digest-assets
. Here's a series of steps that I just used to verify the behavior in a new rails app.
$ rails new asset-test
$ cd asset-test
$ git init .
$ git add .
$ git commit -m "new rails"
$ echo 'gem "non-stupid-digest-assets"' >> Gemfile
$ bundle install
$ git add .
$ git commit -m "add non-stupid-digest-assets"
$ rake assets:precompile
I, [2016-11-14T11:37:06.085170 #44963] INFO -- : Writing /Users/jgreen/projects/clickfunnels/asset-precompile-test/public/assets/application-5c8884cc03b1598ffec78532914008dbc272b7bea6b8fc8d1e5301c82b001d0c.js
I, [2016-11-14T11:37:06.091784 #44963] INFO -- : Writing /Users/jgreen/projects/clickfunnels/asset-precompile-test/public/assets/application-5c8884cc03b1598ffec78532914008dbc272b7bea6b8fc8d1e5301c82b001d0c.js.gz
I, [2016-11-14T11:37:06.096176 #44963] INFO -- : Writing /Users/jgreen/projects/clickfunnels/asset-precompile-test/public/assets/application-e80e8f2318043e8af94dddc2adad5a4f09739a8ebb323b3ab31cd71d45fd9113.css
I, [2016-11-14T11:37:06.096365 #44963] INFO -- : Writing /Users/jgreen/projects/clickfunnels/asset-precompile-test/public/assets/application-e80e8f2318043e8af94dddc2adad5a4f09739a8ebb323b3ab31cd71d45fd9113.css.gz
$ ls public/assets/
application-5c8884cc03b1598ffec78532914008dbc272b7bea6b8fc8d1e5301c82b001d0c.js
application-5c8884cc03b1598ffec78532914008dbc272b7bea6b8fc8d1e5301c82b001d0c.js.gz
application-e80e8f2318043e8af94dddc2adad5a4f09739a8ebb323b3ab31cd71d45fd9113.css
application-e80e8f2318043e8af94dddc2adad5a4f09739a8ebb323b3ab31cd71d45fd9113.css.gz
application.css
application.css.gz
application.js
application.js.gz
$ echo 'alert("hello");' >> app/assets/javascripts/application.js
$ rake assets:precompile
I, [2016-11-14T11:40:10.828850 #45388] INFO -- : Writing /Users/jgreen/projects/clickfunnels/asset-precompile-test/public/assets/application-1581d4bcb794e954f986c800d8fe207217efbdd704e70f9cedb8adfc2cbe0e1b.js
I, [2016-11-14T11:40:10.835785 #45388] INFO -- : Writing /Users/jgreen/projects/clickfunnels/asset-precompile-test/public/assets/application-1581d4bcb794e954f986c800d8fe207217efbdd704e70f9cedb8adfc2cbe0e1b.js.gz
$ ls public/assets/
application-1581d4bcb794e954f986c800d8fe207217efbdd704e70f9cedb8adfc2cbe0e1b.js
application-1581d4bcb794e954f986c800d8fe207217efbdd704e70f9cedb8adfc2cbe0e1b.js.gz
application-5c8884cc03b1598ffec78532914008dbc272b7bea6b8fc8d1e5301c82b001d0c.js
application-5c8884cc03b1598ffec78532914008dbc272b7bea6b8fc8d1e5301c82b001d0c.js.gz
application-e80e8f2318043e8af94dddc2adad5a4f09739a8ebb323b3ab31cd71d45fd9113.css
application-e80e8f2318043e8af94dddc2adad5a4f09739a8ebb323b3ab31cd71d45fd9113.css.gz
application.css
application.css.gz
application.js
application.js.gz
$ grep hello public/assets/application.js
alert("hello");
$ git checkout app/assets/javascripts/application.js
$ rake assets:precompile
$ grep hello public/assets/application.js
alert("hello");
$ grep hello app/assets/javascripts/application.js
<no output, indicating no match>
$ grep hello public/assets/application-5c8884cc03b1598ffec78532914008dbc272b7bea6b8fc8d1e5301c82b001d0c.js
<no output, indicating no match>
$ grep hello public/assets/application-1581d4bcb794e954f986c800d8fe207217efbdd704e70f9cedb8adfc2cbe0e1b.js
alert("hello");
I think what's happening is this:
On the first pass (before adding alert('hello');
) sprockets
is generating the file with a digest of 5c8884...
. And then non-stupid-digest-assets
is copying that fingerprinted file to the non-digest location.
Then on the second compilation (with the alert added), sprockets
is generating a digest of e80e8f...
, and non-stupid-digest-assets
is then copying that file to the non-digest location.
On the 3rd run (after removing the previously added line), sprockets
is again generating a digest of 5c8884...
, which is the same as from the first run. Using this digest it looks in public/assets
and sees that a file with that fingerprint already exists, so it does not recompile the file, and does not generate any output. I'm guessing that the lack of output or action from sprockets
then causes non-stupid-digest-assets
to think that nothing has happened, so then non-stupid-digest-assets
doesn't take any action, which then leaves the non-digest version of application.js
in an inconsistent state. At the end of this application.js
reflects the state of the previous compilation, instead of the most recent one.
$ RAILS_ENV=production bundle exec rake assets:precompile --trace
** Invoke assets:precompile (first_time)
** Invoke assets:environment (first_time)
** Execute assets:environment
** Invoke environment (first_time)
** Execute environment
** Execute assets:precompile
rake aborted!
end of file reached
/home/mkalita/4jobs/vendor/bundle/ruby/2.0.0/gems/activesupport-4.0.4/lib/active_support/core_ext/marshal.rb:6:in `load'
/home/mkalita/4jobs/vendor/bundle/ruby/2.0.0/gems/activesupport-4.0.4/lib/active_support/core_ext/marshal.rb:6:in `load_with_autoloading'
/home/mkalita/4jobs/vendor/bundle/ruby/2.0.0/gems/sprockets-2.11.0/lib/sprockets/cache/file_store.rb:19:in `block in []'
/home/mkalita/4jobs/vendor/bundle/ruby/2.0.0/gems/sprockets-2.11.0/lib/sprockets/cache/file_store.rb:19:in `open'
/home/mkalita/4jobs/vendor/bundle/ruby/2.0.0/gems/sprockets-2.11.0/lib/sprockets/cache/file_store.rb:19:in `open'
/home/mkalita/4jobs/vendor/bundle/ruby/2.0.0/gems/sprockets-2.11.0/lib/sprockets/cache/file_store.rb:19:in `[]'
/home/mkalita/4jobs/vendor/bundle/ruby/2.0.0/gems/sprockets-2.11.0/lib/sprockets/caching.rb:14:in `cache_get'
/home/mkalita/4jobs/vendor/bundle/ruby/2.0.0/gems/sprockets-2.11.0/lib/sprockets/caching.rb:84:in `cache_get_hash'
/home/mkalita/4jobs/vendor/bundle/ruby/2.0.0/gems/sprockets-2.11.0/lib/sprockets/caching.rb:54:in `cache_asset'
/home/mkalita/4jobs/vendor/bundle/ruby/2.0.0/gems/sprockets-2.11.0/lib/sprockets/index.rb:93:in `build_asset'
/home/mkalita/4jobs/vendor/bundle/ruby/2.0.0/gems/sprockets-2.11.0/lib/sprockets/base.rb:287:in `find_asset'
/home/mkalita/4jobs/vendor/bundle/ruby/2.0.0/gems/sprockets-2.11.0/lib/sprockets/index.rb:61:in `find_asset'
/home/mkalita/4jobs/vendor/bundle/ruby/2.0.0/gems/sprockets-2.11.0/lib/sprockets/manifest.rb:211:in `block in find_asset'
/home/mkalita/4jobs/vendor/bundle/ruby/2.0.0/gems/sprockets-2.11.0/lib/sprockets/manifest.rb:257:in `benchmark'
/home/mkalita/4jobs/vendor/bundle/ruby/2.0.0/gems/sprockets-2.11.0/lib/sprockets/manifest.rb:210:in `find_asset'
/home/mkalita/4jobs/vendor/bundle/ruby/2.0.0/gems/sprockets-2.11.0/lib/sprockets/manifest.rb:119:in `block in compile'
/home/mkalita/4jobs/vendor/bundle/ruby/2.0.0/gems/sprockets-2.11.0/lib/sprockets/manifest.rb:118:in `each'
/home/mkalita/4jobs/vendor/bundle/ruby/2.0.0/gems/sprockets-2.11.0/lib/sprockets/manifest.rb:118:in `compile'
/home/mkalita/4jobs/vendor/bundle/ruby/2.0.0/gems/non-stupid-digest-assets-1.0.4/lib/non-stupid-digest-assets.rb:31:in `compile_with_non_digest'
/home/mkalita/4jobs/vendor/bundle/ruby/2.0.0/gems/sprockets-rails-2.0.1/lib/sprockets/rails/task.rb:60:in `block (3 levels) in define'
/home/mkalita/4jobs/vendor/bundle/ruby/2.0.0/gems/sprockets-2.11.0/lib/rake/sprocketstask.rb:146:in `with_logger'
/home/mkalita/4jobs/vendor/bundle/ruby/2.0.0/gems/sprockets-rails-2.0.1/lib/sprockets/rails/task.rb:59:in `block (2 levels) in define'
/home/mkalita/4jobs/vendor/bundle/ruby/2.0.0/gems/rake-10.1.1/lib/rake/task.rb:236:in `call'
/home/mkalita/4jobs/vendor/bundle/ruby/2.0.0/gems/rake-10.1.1/lib/rake/task.rb:236:in `block in execute'
/home/mkalita/4jobs/vendor/bundle/ruby/2.0.0/gems/rake-10.1.1/lib/rake/task.rb:231:in `each'
/home/mkalita/4jobs/vendor/bundle/ruby/2.0.0/gems/rake-10.1.1/lib/rake/task.rb:231:in `execute'
/home/mkalita/4jobs/vendor/bundle/ruby/2.0.0/gems/rake-10.1.1/lib/rake/task.rb:175:in `block in invoke_with_call_chain'
/home/mkalita/.rvm/rubies/ruby-2.0.0-p247/lib/ruby/2.0.0/monitor.rb:211:in `mon_synchronize'
/home/mkalita/4jobs/vendor/bundle/ruby/2.0.0/gems/rake-10.1.1/lib/rake/task.rb:168:in `invoke_with_call_chain'
/home/mkalita/4jobs/vendor/bundle/ruby/2.0.0/gems/rake-10.1.1/lib/rake/task.rb:161:in `invoke'
/home/mkalita/4jobs/vendor/bundle/ruby/2.0.0/gems/rake-10.1.1/lib/rake/application.rb:149:in `invoke_task'
/home/mkalita/4jobs/vendor/bundle/ruby/2.0.0/gems/rake-10.1.1/lib/rake/application.rb:106:in `block (2 levels) in top_level'
/home/mkalita/4jobs/vendor/bundle/ruby/2.0.0/gems/rake-10.1.1/lib/rake/application.rb:106:in `each'
/home/mkalita/4jobs/vendor/bundle/ruby/2.0.0/gems/rake-10.1.1/lib/rake/application.rb:106:in `block in top_level'
/home/mkalita/4jobs/vendor/bundle/ruby/2.0.0/gems/rake-10.1.1/lib/rake/application.rb:115:in `run_with_threads'
/home/mkalita/4jobs/vendor/bundle/ruby/2.0.0/gems/rake-10.1.1/lib/rake/application.rb:100:in `top_level'
/home/mkalita/4jobs/vendor/bundle/ruby/2.0.0/gems/rake-10.1.1/lib/rake/application.rb:78:in `block in run'
/home/mkalita/4jobs/vendor/bundle/ruby/2.0.0/gems/rake-10.1.1/lib/rake/application.rb:165:in `standard_exception_handling'
/home/mkalita/4jobs/vendor/bundle/ruby/2.0.0/gems/rake-10.1.1/lib/rake/application.rb:75:in `run'
/home/mkalita/4jobs/vendor/bundle/ruby/2.0.0/gems/rake-10.1.1/bin/rake:33:in `<top (required)>'
/home/mkalita/4jobs/vendor/bundle/ruby/2.0.0/bin/rake:23:in `load'
/home/mkalita/4jobs/vendor/bundle/ruby/2.0.0/bin/rake:23:in `<main>'
Tasks: TOP => assets:precompile
rails (4.0.4)
(...)
sprockets (2.11.0)
hike (~> 1.2)
multi_json (~> 1.0)
rack (~> 1.0)
tilt (~> 1.1, != 1.3.0)
sprockets-rails (2.0.1)
actionpack (>= 3.0)
activesupport (>= 3.0)
sprockets (~> 2.8)
therubyracer (0.12.1)
libv8 (~> 3.16.14.0)
ref
When having hundreds of files this matters in terms of speed and hard disk activity, plus, it would save some disk space.
Just wanted to say thanks a bunch for making this. You saved me so much time. ๐
Copy gzip assets too:
....
theme_base-a562f51530269c0ab134bd43dddcab28.css
theme_base-a562f51530269c0ab134bd43dddcab28.css.gz
....
to:
....
theme_base-a562f51530269c0ab134bd43dddcab28.css
theme_base-a562f51530269c0ab134bd43dddcab28.css.gz
theme_base.css
theme_base.css.gz
....
Hi, is this gem still required for Rails 5? I didn't find any info if it is possible to compile both assets versions in Rails 5 without this gem, so I assume it is still required. It would be nice to add some info to the README.md.
After migrating to this gem my deploy time increased from 2 minutes to 20 (!). It stays 18 minutes to run the precompile rake, even if I have only few files. Is this "normal"?
For various reasons we've gone for a setup where cache busting is provided at a higher level (root of the path) so digesting of any sort isn't useful (actually just gets in the way). Using this gem solves the problem but leaves our asset bundle double the size it really needs to be.
I'm going to patch a fork to include a non_digest_asset_mode
or similar setting. Default being copy
but optionally move
.
Would this be something you'd be interested in?
I am using this NonStupidDigestAssets.whitelist = [/images/.*/], and i checked, it should work, I have files there, but nothing happens, like the gem didn't even affect sprockets.
rails (4.0.4)
(...)
sprockets (2.11.0)
hike (~> 1.2)
multi_json (~> 1.0)
rack (~> 1.0)
tilt (~> 1.1, != 1.3.0)
sprockets-rails (2.0.1)
actionpack (>= 3.0)
activesupport (>= 3.0)
sprockets (~> 2.8)
therubyracer (0.12.1)
libv8 (~> 3.16.14.0)
ref
I tried this rake task:
https://github.com/rails/sprockets-rails/issues/49#issuecomment-20535134
which is great, but it gem like this is nicer.
The README says this doesn't work with 4.2, but it seems fine (see discussion there).
I came up with this rather simple solution: add a controller action that proxies the file. Seems to work great! Obviously it only works with precompiled files so run rake assets:precompile
to test in development.
# config/routes.rb
get "integrations/assets/:id", to: "assets#show"
# app/controllers/assets_controller.rb
class AssetsController < ApplicationController
skip_before_filter :verify_authenticity_token, only: [:show]
def show
file_name = "#{params[:id]}.#{params[:format]}"
asset_path = ActionController::Base.helpers.asset_path(file_name)
file_path = "#{Rails.root}/public#{asset_path}"
mime_type = Mime::Type.lookup_by_extension(params[:format])
send_file(file_path, type: mime_type, disposition: "inline")
end
end
Posting here as a different solution to rails/sprockets-rails#49 because that thread is locked.
NonStupidDigestAssets.whitelist = [/.jpg/,/.jpeg/, /.png/]
What settings should be put in my staging.rb/production.rb?
Im not really sure how to debug this correctly but there are no non digested versions of any assets generated.
I installed the gem, bundle install, commits, etc then deployed
any thoughts on how to better debug it?
it seems the method is not aliased.
I then tried adding it as initialiser with some modifications:
as config/initializers/sprockets.rb:
module Sprockets
class Manifest
def compile_with_non_digest *args
compile_without_non_digest *args
files.each do |(digest_path, info)|
full_digest_path = File.join dir, digest_path
full_non_digest_path = File.join dir, info['logical_path']
pp "NON DIGESTED CREATED! #{ full_digest_path} to #{ full_non_digest_path }"
logger.info "NON DIGESTED CREATED! #{ full_digest_path} to #{ full_non_digest_path }"
logger.info "Writing #{full_non_digest_path}"
FileUtils.cp full_digest_path, full_non_digest_path
end
end
alias_method_chain :compile, :non_digest
end
end
any thoughts? thank you
When I run this the first time everything works OK, however on subsequent runs the assets all have a 0 file size.
This is using Rails 5
Hi @alexspeller are you still maintaining this repo? Looks like there are quite a few opened PRs I could use...
Saw the discussion on this subject what a stupid choice rails 4 does not let us endusers decide how to arrange this, anyway:
I only need certain files to be copied over, to be precise a logo.png/logo2.png and some .css files. Would it be possible to supply a list of files to be copied over like a whitelist and only process these files?
It looks like non-digest files simply get copied into the output directory but nothing about them is added to the manifest file. In my particular case it helps to have non-digest assets added to the body of the manifest file.
Do you think that may be something you could add to the gem?
Weirdly enough, my asset is missing on heroku.
I followed the usage instructions, and it does work on localhost, but doesn't work on heroku.
I'm using puma on production, if that helps.
Allowed me to use esbuild + chunk splitting with rails. Thanks!
It seems a bit dangerous to have a monkeypatch gem that doesn't pin the version of Sprockets that it's monkeypatching. Is there a reason it was not included in the gemspec
?
After adding the gem and restarting the server, I was surprised to not find undigested assets in public/assets. Please add the instruction to run rake assets:precompile for coders who don't drink coffee. Thanks.
I just tried this gem to include AmCharts but couldn't manage to get it running. Once I downgraded from rails version 4.2.0.beta1 to 4.1.5 everything worked just fine. Apart from downgrading rails I didn't have to change anything. Is there any known compatibility issue with the latest rails version?
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.