Skip to content
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

initial work adding asset version back so cache busts #1

Open
wants to merge 6 commits into
base: 3.x
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions lib/sprockets/cached_digest_utils.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
module Sprockets
# Internal:

module CachedDigestUtils
extend self

def cached_version_digest
version_string = defined?(version) ? version : ''

# Compute the initial digest using the implementation class. The
# Sprockets release version and custom environment version are
# mixed in. So any new releases will affect all your assets.
@cached_version_digest ||= digest_class.new.update(version_string.to_s)

# Returned a dupped copy so the caller can safely mutate it with `.update`
@cached_version_digest.dup
end

end
end
2 changes: 2 additions & 0 deletions lib/sprockets/cached_environment.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ def initialize(environment)
initialize_configuration(environment)

@cache = environment.cache
@cached_version_digest = environment.cached_version_digest

@stats = Hash.new { |h, k| h[k] = _stat(k) }
@entries = Hash.new { |h, k| h[k] = _entries(k) }
@uris = Hash.new { |h, k| h[k] = _load(k) }
Expand Down
3 changes: 3 additions & 0 deletions lib/sprockets/digest_utils.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,14 @@
require 'digest/sha1'
require 'digest/sha2'
require 'set'
require 'sprockets/cached_digest_utils'

module Sprockets
# Internal: Hash functions and digest related utilities. Mixed into
# Environment.
module DigestUtils
include CachedDigestUtils

extend self

# Internal: Default digest class.
Expand Down
4 changes: 2 additions & 2 deletions lib/sprockets/path_digest_utils.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,10 @@ module PathDigestUtils
def stat_digest(path, stat)
if stat.directory?
# If its a directive, digest the list of filenames
digest_class.digest(self.entries(path).join(','))
cached_version_digest.digest(self.entries(path).join(','))
elsif stat.file?
# If its a file, digest the contents
digest_class.file(path.to_s).digest
cached_version_digest.file(path.to_s).digest
else
raise TypeError, "stat was not a directory or file: #{stat.ftype}"
end
Expand Down
5 changes: 3 additions & 2 deletions lib/sprockets/unloaded_asset.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,12 @@ class UnloadedAsset
def initialize(uri, env)
@uri = uri.to_s
@env = env
@version = env.version
@compressed_path = URITar.new(uri, env).compressed_path
@params = nil # lazy loaded
@filename = nil # lazy loaded
end
attr_reader :compressed_path, :uri
attr_reader :compressed_path, :uri, :version

# Internal: Full file path without schema
#
Expand Down Expand Up @@ -123,7 +124,7 @@ def digest_key(digest)
#
# Returns a String.
def file_digest_key(stat)
"file_digest:#{compressed_path}:#{stat}"
"file_digest:#{compressed_path}:#{stat}:#{version}"
end

private
Expand Down