-
-
Notifications
You must be signed in to change notification settings - Fork 631
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #413 from alleycat-at-git/feature/asset_pipeline_d…
…igests Added non-digested version for assets (images, fonts, svg)
- Loading branch information
Showing
5 changed files
with
110 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
## Rails assets | ||
|
||
### Problem | ||
When client js uses images in render methods, e.g. `<img src='...' />` or in css, e.g. `background-image: url(...)` | ||
these assets fail to load. This happens because rails adds digest hashes to filenames | ||
when compiling assets, e.g. `img1.jpg` becomes `img1-dbu097452jf2v2.jpg`. | ||
|
||
When compiling its native css Rails transforms all urls and links to digested | ||
versions, i.e. `background-image: image-url(img1.jpg)` becomes | ||
`background-image: url(img1-dbu097452jf2v2.jpg)`. However this doesn't happen for js and | ||
css files compiled by webpack on the client side, because they don't use | ||
`image-url` and `asset-url` and therefore assets fail to load. | ||
|
||
### Solution | ||
|
||
Create symlinks of non-digested versions to digested versions when Rails assets compile. | ||
The solution is implemented using `assets:precompile` after-hook. The assets for symlinking | ||
are defined by `config.symlink_non_digested_assets_regex` in `config/initializers/react_on_rails.rb`. | ||
To disable symlinks set this parameter to `nil`. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
module ReactOnRails | ||
class << self | ||
def assets_path | ||
dir = File.join(Rails.configuration.paths['public'].first, | ||
Rails.configuration.assets.prefix) | ||
Pathname.new(dir) | ||
end | ||
|
||
def symlink_file(target, symlink) | ||
if not File.exist?(symlink) or File.lstat(symlink).symlink? | ||
if File.exist?(target) | ||
puts "React On Rails: Symlinking #{target} to #{symlink}" | ||
FileUtils.ln_s target, symlink, force: true | ||
end | ||
else | ||
puts "React On Rails: File #{symlink} already exists. Failed to symlink #{target}" | ||
end | ||
end | ||
end | ||
end | ||
|
||
if ReactOnRails.configuration.symlink_non_digested_assets_regex | ||
Rake::Task["assets:precompile"].enhance do | ||
Rake::Task["react_on_rails:assets:symlink_non_digested_assets"].invoke | ||
Rake::Task["react_on_rails:assets:delete_broken_symlinks"].invoke | ||
end | ||
end | ||
|
||
namespace :react_on_rails do | ||
namespace :assets do | ||
|
||
desc "Creates non-digested symlinks for the assets in the public asset dir" | ||
task symlink_non_digested_assets: :"assets:environment" do | ||
manifest_path = Dir.glob(ReactOnRails::assets_path.join(".sprockets-manifest-*.json")) | ||
.first | ||
manifest_data = JSON.load(File.new(manifest_path)) | ||
|
||
manifest_data["assets"].each do |logical_path, digested_path| | ||
regex = ReactOnRails.configuration.symlink_non_digested_assets_regex | ||
if logical_path =~ regex | ||
full_digested_path = ReactOnRails::assets_path.join(digested_path) | ||
full_nondigested_path = ReactOnRails::assets_path.join(logical_path) | ||
extension = full_digested_path.extname | ||
full_digested_gz_path = full_digested_path.sub_ext("#{extension}.gz") | ||
full_nondigested_gz_path = full_nondigested_path.sub_ext("#{extension}.gz") | ||
ReactOnRails::symlink_file(full_digested_path, full_nondigested_path) | ||
ReactOnRails::symlink_file(full_digested_gz_path, full_nondigested_gz_path) | ||
end | ||
end | ||
end | ||
|
||
desc "Cleans all broken symlinks for the assets in the public asset dir" | ||
task delete_broken_symlinks: :"assets:environment" do | ||
Dir.glob(ReactOnRails::assets_path.join("*")).each do |filename| | ||
if File.lstat(filename).symlink? | ||
begin | ||
target = File.readlink(filename) | ||
rescue | ||
puts "React on Rails: Warning: your platform doesn't support File::readlink method."/ | ||
"Skipping broken link check." | ||
return | ||
end | ||
path = Pathname.new(File.dirname(filename)) | ||
target_path = path.join(target) | ||
unless File.exist?(target_path) | ||
puts "React on Rails: Deleting broken link: #{filename}" | ||
File.delete(filename) | ||
end | ||
end | ||
end | ||
end | ||
|
||
end | ||
end | ||
|