From 0cc0bcda1b9b1085d58931808ba2a6d78854f7d2 Mon Sep 17 00:00:00 2001 From: John Royall Date: Mon, 1 Feb 2016 13:17:02 -0500 Subject: [PATCH 1/6] initial work adding asset version back so cache busts --- lib/sprockets/base.rb | 21 +++++++++++++++++++++ lib/sprockets/cached_environment.rb | 3 +++ lib/sprockets/path_digest_utils.rb | 2 +- lib/sprockets/unloaded_asset.rb | 5 +++-- 4 files changed, 28 insertions(+), 3 deletions(-) diff --git a/lib/sprockets/base.rb b/lib/sprockets/base.rb index e5b61d7c9..4ff7dba9b 100644 --- a/lib/sprockets/base.rb +++ b/lib/sprockets/base.rb @@ -24,6 +24,17 @@ class Base # Get persistent cache store attr_reader :cache + attr_reader :cached_version_digest + + attr_reader :version + + # Assign an environment version. + # + # environment.version = '2.0' + # + def version=(version) + @version = version + end # Set persistent cache store # @@ -40,6 +51,16 @@ def cached end alias_method :index, :cached + def cached_version_digest + # 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.to_s) + + # Returned a dupped copy so the caller can safely mutate it with `.update` + @cached_version_digest.dup + end + # Internal: Compute digest for path. # # path - String filename or directory path. diff --git a/lib/sprockets/cached_environment.rb b/lib/sprockets/cached_environment.rb index a6ac3690f..271d9e0c9 100644 --- a/lib/sprockets/cached_environment.rb +++ b/lib/sprockets/cached_environment.rb @@ -14,7 +14,10 @@ class CachedEnvironment < Base def initialize(environment) initialize_configuration(environment) + @cached_version_digest = environment.cached_version_digest + @cache = environment.cache + @version = environment.version @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) } diff --git a/lib/sprockets/path_digest_utils.rb b/lib/sprockets/path_digest_utils.rb index ee40d2e4b..6a0943dc0 100644 --- a/lib/sprockets/path_digest_utils.rb +++ b/lib/sprockets/path_digest_utils.rb @@ -18,7 +18,7 @@ def stat_digest(path, stat) digest_class.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 diff --git a/lib/sprockets/unloaded_asset.rb b/lib/sprockets/unloaded_asset.rb index fb1c2db25..c5fef7d7e 100644 --- a/lib/sprockets/unloaded_asset.rb +++ b/lib/sprockets/unloaded_asset.rb @@ -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 # @@ -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 From 0d4767796d50c6e96c8925ab102a20dd8dab42e4 Mon Sep 17 00:00:00 2001 From: John Royall Date: Wed, 3 Feb 2016 12:51:54 -0500 Subject: [PATCH 2/6] refactor --- lib/sprockets/base.rb | 21 +-------------------- lib/sprockets/cached_digest_utils.rb | 20 ++++++++++++++++++++ lib/sprockets/cached_environment.rb | 4 ++-- lib/sprockets/path_digest_utils.rb | 4 +++- lib/sprockets/unloaded_asset.rb | 1 + 5 files changed, 27 insertions(+), 23 deletions(-) create mode 100644 lib/sprockets/cached_digest_utils.rb diff --git a/lib/sprockets/base.rb b/lib/sprockets/base.rb index 4ff7dba9b..a9dee8a8c 100644 --- a/lib/sprockets/base.rb +++ b/lib/sprockets/base.rb @@ -16,7 +16,7 @@ module Sprockets # `Base` class for `Environment` and `Cached`. class Base - include PathUtils, PathDependencyUtils, PathDigestUtils, DigestUtils + include PathUtils, PathDependencyUtils, PathDigestUtils, DigestUtils, CachedDigestUtils include Configuration include Server include Resolve, Loader @@ -24,17 +24,6 @@ class Base # Get persistent cache store attr_reader :cache - attr_reader :cached_version_digest - - attr_reader :version - - # Assign an environment version. - # - # environment.version = '2.0' - # - def version=(version) - @version = version - end # Set persistent cache store # @@ -51,15 +40,7 @@ def cached end alias_method :index, :cached - def cached_version_digest - # 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.to_s) - # Returned a dupped copy so the caller can safely mutate it with `.update` - @cached_version_digest.dup - end # Internal: Compute digest for path. # diff --git a/lib/sprockets/cached_digest_utils.rb b/lib/sprockets/cached_digest_utils.rb new file mode 100644 index 000000000..bebb8268d --- /dev/null +++ b/lib/sprockets/cached_digest_utils.rb @@ -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 diff --git a/lib/sprockets/cached_environment.rb b/lib/sprockets/cached_environment.rb index 271d9e0c9..14ed5f9bd 100644 --- a/lib/sprockets/cached_environment.rb +++ b/lib/sprockets/cached_environment.rb @@ -14,10 +14,10 @@ class CachedEnvironment < Base def initialize(environment) initialize_configuration(environment) - @cached_version_digest = environment.cached_version_digest - @cache = environment.cache @version = environment.version + @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) } diff --git a/lib/sprockets/path_digest_utils.rb b/lib/sprockets/path_digest_utils.rb index 6a0943dc0..d82bcce19 100644 --- a/lib/sprockets/path_digest_utils.rb +++ b/lib/sprockets/path_digest_utils.rb @@ -1,10 +1,12 @@ require 'sprockets/digest_utils' require 'sprockets/path_utils' +require 'sprockets/cached_digest_utils' module Sprockets # Internal: Crossover of path and digest utilities functions. module PathDigestUtils include DigestUtils, PathUtils + include CachedDigestUtils # Internal: Compute digest for file stat. # @@ -15,7 +17,7 @@ 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 cached_version_digest.file(path.to_s).digest diff --git a/lib/sprockets/unloaded_asset.rb b/lib/sprockets/unloaded_asset.rb index c5fef7d7e..138a2dae7 100644 --- a/lib/sprockets/unloaded_asset.rb +++ b/lib/sprockets/unloaded_asset.rb @@ -1,5 +1,6 @@ require 'sprockets/uri_utils' require 'sprockets/uri_tar' +require 'sprockets/cached_digest_utils' module Sprockets # Internal: Used to parse and store the URI to an unloaded asset From ef8f92f60edbd62717f7f26ee25260ab1d6517fc Mon Sep 17 00:00:00 2001 From: John Royall Date: Wed, 3 Feb 2016 13:06:19 -0500 Subject: [PATCH 3/6] move CachedDigestUtils include to DigestUtils --- lib/sprockets/base.rb | 2 +- lib/sprockets/digest_utils.rb | 3 +++ lib/sprockets/path_digest_utils.rb | 2 -- 3 files changed, 4 insertions(+), 3 deletions(-) diff --git a/lib/sprockets/base.rb b/lib/sprockets/base.rb index a9dee8a8c..c4cc24a2d 100644 --- a/lib/sprockets/base.rb +++ b/lib/sprockets/base.rb @@ -16,7 +16,7 @@ module Sprockets # `Base` class for `Environment` and `Cached`. class Base - include PathUtils, PathDependencyUtils, PathDigestUtils, DigestUtils, CachedDigestUtils + include PathUtils, PathDependencyUtils, PathDigestUtils, DigestUtils include Configuration include Server include Resolve, Loader diff --git a/lib/sprockets/digest_utils.rb b/lib/sprockets/digest_utils.rb index 1195e2a8b..74d509c4e 100644 --- a/lib/sprockets/digest_utils.rb +++ b/lib/sprockets/digest_utils.rb @@ -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. diff --git a/lib/sprockets/path_digest_utils.rb b/lib/sprockets/path_digest_utils.rb index d82bcce19..2fd5a945f 100644 --- a/lib/sprockets/path_digest_utils.rb +++ b/lib/sprockets/path_digest_utils.rb @@ -1,12 +1,10 @@ require 'sprockets/digest_utils' require 'sprockets/path_utils' -require 'sprockets/cached_digest_utils' module Sprockets # Internal: Crossover of path and digest utilities functions. module PathDigestUtils include DigestUtils, PathUtils - include CachedDigestUtils # Internal: Compute digest for file stat. # From 3e45eb379b61d6990d4ac43a27dd35e5602ebe78 Mon Sep 17 00:00:00 2001 From: John Royall Date: Wed, 3 Feb 2016 15:17:11 -0500 Subject: [PATCH 4/6] further refactoring --- lib/sprockets/cached_environment.rb | 4 +++- lib/sprockets/unloaded_asset.rb | 1 - 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/lib/sprockets/cached_environment.rb b/lib/sprockets/cached_environment.rb index 14ed5f9bd..5402d39d8 100644 --- a/lib/sprockets/cached_environment.rb +++ b/lib/sprockets/cached_environment.rb @@ -15,7 +15,9 @@ def initialize(environment) initialize_configuration(environment) @cache = environment.cache - @version = environment.version + + # @version = environment.version + @cached_version_digest = environment.cached_version_digest @stats = Hash.new { |h, k| h[k] = _stat(k) } diff --git a/lib/sprockets/unloaded_asset.rb b/lib/sprockets/unloaded_asset.rb index 138a2dae7..c5fef7d7e 100644 --- a/lib/sprockets/unloaded_asset.rb +++ b/lib/sprockets/unloaded_asset.rb @@ -1,6 +1,5 @@ require 'sprockets/uri_utils' require 'sprockets/uri_tar' -require 'sprockets/cached_digest_utils' module Sprockets # Internal: Used to parse and store the URI to an unloaded asset From ea04f21cb4784829ec15cda3f199f7e56819771d Mon Sep 17 00:00:00 2001 From: John Royall Date: Wed, 3 Feb 2016 15:42:29 -0500 Subject: [PATCH 5/6] cleanup --- lib/sprockets/base.rb | 2 -- 1 file changed, 2 deletions(-) diff --git a/lib/sprockets/base.rb b/lib/sprockets/base.rb index c4cc24a2d..e5b61d7c9 100644 --- a/lib/sprockets/base.rb +++ b/lib/sprockets/base.rb @@ -40,8 +40,6 @@ def cached end alias_method :index, :cached - - # Internal: Compute digest for path. # # path - String filename or directory path. From 94e11ebdcd0d88b9df11d865ea132f9a15ac30eb Mon Sep 17 00:00:00 2001 From: John Royall Date: Wed, 3 Feb 2016 15:43:23 -0500 Subject: [PATCH 6/6] remove @version --- lib/sprockets/cached_environment.rb | 3 --- 1 file changed, 3 deletions(-) diff --git a/lib/sprockets/cached_environment.rb b/lib/sprockets/cached_environment.rb index 5402d39d8..e9c1c5eb8 100644 --- a/lib/sprockets/cached_environment.rb +++ b/lib/sprockets/cached_environment.rb @@ -15,9 +15,6 @@ def initialize(environment) initialize_configuration(environment) @cache = environment.cache - - # @version = environment.version - @cached_version_digest = environment.cached_version_digest @stats = Hash.new { |h, k| h[k] = _stat(k) }