Skip to content

Instantly share code, notes, and snippets.

@elia
Last active October 31, 2020 15:34
Show Gist options
  • Save elia/8d78d1b1a52cc2ed88c665b32a139182 to your computer and use it in GitHub Desktop.
Save elia/8d78d1b1a52cc2ed88c665b32a139182 to your computer and use it in GitHub Desktop.
# Remove when https://github.com/rails/sprockets/issues/242 is resolved
unless Rails.env.development?
module SprocketsCachedLoader
def initialize(*)
super
@expanded_assets_cache = {}
end
def asset_from_cache(key)
return @expanded_assets_cache[key] if @expanded_assets_cache.key?(key)
asset = cache.get(key, true)
if asset
asset[:uri] = expand_from_root(asset[:uri])
asset[:load_path] = expand_from_root(asset[:load_path])
asset[:filename] = expand_from_root(asset[:filename])
asset[:metadata][:included] = asset[:metadata][:included].map { |uri| expand_from_root(uri) } if asset[:metadata][:included]
asset[:metadata][:links] = asset[:metadata][:links].map { |uri| expand_from_root(uri) } if asset[:metadata][:links]
asset[:metadata][:stubbed] = asset[:metadata][:stubbed].map { |uri| expand_from_root(uri) } if asset[:metadata][:stubbed]
asset[:metadata][:required] = asset[:metadata][:required].map { |uri| expand_from_root(uri) } if asset[:metadata][:required]
asset[:metadata][:dependencies] = asset[:metadata][:dependencies].map { |uri| uri.start_with?("file-digest://") ? expand_from_root(uri) : uri } if asset[:metadata][:dependencies]
asset[:metadata].each_key do |k|
next unless k =~ /_dependencies\z/
asset[:metadata][k] = asset[:metadata][k].map { |uri| expand_from_root(uri) }
end
asset
end
@expanded_assets_cache[key] = asset
end
Sprockets::CachedEnvironment.prepend self
end
end
@e3matheus
Copy link

On line 10, I believe it is @expanded_assets_cache.has_key?(key)

@elia
Copy link
Author

elia commented Oct 31, 2020

@e3matheus thanks! updated 👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment