-
-
Notifications
You must be signed in to change notification settings - Fork 632
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Added non-digested version for assets (images, fonts, svg) #413
Added non-digested version for assets (images, fonts, svg) #413
Conversation
Let's look at this sample: https://github.com/shakacode/react-webpack-rails-tutorial/blob/master/lib/tasks/assets.rake I like the idea of putting these in standard tasks for the gem. However, we need to consider the upgrade path for existing ReactOnRails apps. For example, somebody's app might already be doing this, so we need the ability to configure if this done. Next, we should consider these below (from the link above). Maybe we can prefix the tasks we offer with the namespace This is super awesome! Let's do this once. We'll also need to update the docs. That's actually as or more important than the generator. CC: @jbhatab @robwise @aaronvb Rake::Task["assets:precompile"]
.clear_prerequisites
.enhance(["assets:compile_environment"])
namespace :assets do
# In this task, set prerequisites for the assets:precompile task
task compile_environment: :webpack do
Rake::Task["assets:environment"].invoke
end
desc "Compile assets with webpack"
task :webpack do
sh "cd client && npm run build:production:client"
sh "cd client && npm run build:production:server"
sh "mkdir -p public/assets"
# Critical to manually copy non js/css assets to public/assets as we don't want to fingerprint them
sh "cp -rf app/assets/webpack/*.woff* app/assets/webpack/*.svg app/assets/webpack/*.ttf "\
"app/assets/webpack/*.eot* public/assets"
# TODO: We should be gzipping these
end
task :clobber do
rm_rf "#{Rails.application.config.root}/app/assets/webpack"
end
end Review status: 0 of 3 files reviewed at latest revision, all discussions resolved. Comments from Reviewable |
@justin808 I refactored it to make a hook inside React-on-rails gem. Now we don't have these problems. Also added docs and changelog Review status: 0 of 5 files reviewed at latest revision, all discussions resolved. Comments from Reviewable |
Added more comments. We need to test this as a migration to https://github.com/shakacode/react-webpack-rails-tutorial/ Review status: 0 of 5 files reviewed at latest revision, 4 unresolved discussions. docs/additional-reading/rails-assets.md, line 19 [r2] (raw file): lib/react_on_rails/configuration.rb, line 60 [r1] (raw file): lib/tasks/assets.rake, line 2 [r2] (raw file): lib/generators/react_on_rails/templates/base/base/lib/tasks/assets.rake.tt, line 27 [r1] (raw file): Comments from Reviewable |
Review status: 0 of 5 files reviewed at latest revision, 4 unresolved discussions. docs/additional-reading/rails-assets.md, line 19 [r2] (raw file): lib/react_on_rails/configuration.rb, line 60 [r1] (raw file): lib/tasks/assets.rake, line 2 [r2] (raw file): lib/generators/react_on_rails/templates/base/base/lib/tasks/assets.rake.tt, line 27 [r1] (raw file): Comments from Reviewable |
Review status: 0 of 5 files reviewed at latest revision, 4 unresolved discussions. lib/generators/react_on_rails/templates/base/base/lib/tasks/assets.rake.tt, line 27 [r1] (raw file): Comments from Reviewable |
Review status: 0 of 5 files reviewed at latest revision, 1 unresolved discussion. lib/generators/react_on_rails/templates/base/base/lib/tasks/assets.rake.tt, line 27 [r1] (raw file): Comments from Reviewable |
Reviewed 4 of 5 files at r2, 2 of 2 files at r3. lib/react_on_rails/configuration.rb, line 60 [r3] (raw file): lib/tasks/assets.rake, line 11 [r3] (raw file): lib/tasks/assets.rake, line 15 [r3] (raw file): lib/tasks/assets.rake, line 17 [r3] (raw file): Like what if:
would the non-digested symlink we made now be invalid and pointing to nowhere? Or will it have gotten deleted? Comments from Reviewable |
Review status: 4 of 5 files reviewed at latest revision, 5 unresolved discussions. lib/react_on_rails/configuration.rb, line 60 [r3] (raw file): Anyway the comment was useful, thanks! I updated the method to be more cautious and overwrite only symlinks lib/tasks/assets.rake, line 11 [r3] (raw file): lib/tasks/assets.rake, line 15 [r3] (raw file): lib/tasks/assets.rake, line 17 [r3] (raw file): Comments from Reviewable |
Review status: 4 of 5 files reviewed at latest revision, 5 unresolved discussions. lib/tasks/assets.rake, line 11 [r3] (raw file): Comments from Reviewable |
@justin808 I checked this PR with react-webpack-rails-tutorial and it totally works. Though we should correct it a bit to remove clutter after we merge this PR Review status: 4 of 5 files reviewed at latest revision, 5 unresolved discussions. Comments from Reviewable |
@alleycat-at-git make sure you check with an image that is large enough that webpack won't just inline it, that's how we never caught this before Reviewed 1 of 1 files at r4. lib/react_on_rails/configuration.rb, line 60 [r3] (raw file): Comments from Reviewable |
@robwise Thanks! ) Review status: all files reviewed at latest revision, 1 unresolved discussion. Comments from Reviewable |
I had a suggestion and question. Looks AMAZING! Review status: all files reviewed at latest revision, 3 unresolved discussions. docs/additional-reading/rails-assets.md, line 19 [r5] (raw file): lib/tasks/assets.rake, line 22 [r5] (raw file): if ReactOnRails.configuration.symlink_non_digested_assets_regex.present? otherwise, if this is set to a "", then that's truthy lib/generators/react_on_rails/templates/base/base/lib/tasks/assets.rake.tt, line 27 [r1] (raw file): Otherwise, this is just extra stuff that @jbhatab will have to explain when he creates a video of how the generators work. Comments from Reviewable |
Review status: all files reviewed at latest revision, 3 unresolved discussions. docs/additional-reading/rails-assets.md, line 19 [r5] (raw file): lib/tasks/assets.rake, line 22 [r5] (raw file): lib/generators/react_on_rails/templates/base/base/lib/tasks/assets.rake.tt, line 27 [r1] (raw file): Comments from Reviewable |
Review status: all files reviewed at latest revision, 3 unresolved discussions. lib/generators/react_on_rails/templates/base/base/lib/tasks/assets.rake.tt, line 27 [r1] (raw file): Comments from Reviewable |
f70503b
to
95b64d7
Compare
95b64d7
to
2a36ac5
Compare
Reviewed 3 of 3 files at r6. Comments from Reviewable |
Changes Unknown when pulling 2a36ac5 on alleycat-at-git:feature/asset_pipeline_digests into * on shakacode:master*. |
Changes Unknown when pulling 2a36ac5 on alleycat-at-git:feature/asset_pipeline_digests into * on shakacode:master*. |
Problem
When client js uses images in render methods, e.g.
<img src='...' />
or in css, e.g.background-image: url(...)
these assets fails to load. This happens because rails adds digest hashes to filenames when compiling assets. e.g.img1.jpg
becomesimg1-dbu097452jf2v2.jpg
. When compiling its native css rails transforms all urls and links to work on digested versions, i.e.background-image: url(img1.jpg)
becomesbackground-image: url(img1-dbu097452jf2v2.jpg)
. However this doesn't happen for js and css files compiled by webpack on the client side and assets fail to loadSolution
Create symlinks of non-digested versions to digested versions when rails assets compile
This change is![Reviewable](https://camo.githubusercontent.com/1541c4039185914e83657d3683ec25920c672c6c5c7ab4240ee7bff601adec0b/68747470733a2f2f72657669657761626c652e696f2f7265766965775f627574746f6e2e737667)