Skip to content

Commit

Permalink
Add backward compatibility
Browse files Browse the repository at this point in the history
  • Loading branch information
ahangarha committed Oct 4, 2023
1 parent a03527f commit cb84653
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 3 deletions.
3 changes: 3 additions & 0 deletions lib/install/config/shakapacker.yml
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,9 @@ default: &default
# Select whether the compiler will use SHA digest ('digest' option) or most most recent modified timestamp ('mtime') to determine freshness
compiler_strategy: digest

# Set whether the compiler should be sensitive to the provided asset host
compiler_strategy_asset_host_sensitive: false

# Select whether the compiler will always use a content hash and not just in production
# Don't use contentHash except for production for performance
# https://webpack.js.org/guides/build-performance/#avoid-production-specific-tooling
Expand Down
5 changes: 4 additions & 1 deletion lib/shakapacker/digest_strategy.rb
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,10 @@ def record_compilation_digest
end

def compilation_digest_path
config.cache_path.join("last-compilation-digest-#{Shakapacker.env}-#{generate_host_hash}")
path = "last-compilation-digest-#{Shakapacker.env}"
path += "-#{generate_host_hash}" if Shakapacker.config.fetch(:compiler_strategy_asset_host_sensitive)

config.cache_path.join(path)
end

def generate_host_hash(*keys)
Expand Down
19 changes: 17 additions & 2 deletions spec/shakapacker/digest_strategy_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@ def remove_compilation_digest_path
end

it "is stale when host changes" do
allow(Shakapacker.config).to receive(:fetch).with(any_args).and_call_original
allow(Shakapacker.config).to receive(:fetch).with(:compiler_strategy_asset_host_sensitive).and_return(true)

ENV["SHAKAPACKER_ASSET_HOST"] = "the-host"

@digest_strategy.after_compile_hook
Expand All @@ -38,12 +41,24 @@ def remove_compilation_digest_path
expect(@digest_strategy.stale?).to be true
expect(@digest_strategy.fresh?).to be_falsey

ENV["SHAKAPACKER_ASSET_HOST"] = ""
ENV["SHAKAPACKER_ASSET_HOST"] = nil
end

it "generates correct compilation_digest_path" do
actual_path = @digest_strategy.send(:compilation_digest_path).basename.to_s
host_hash = Digest::SHA1.hexdigest("")
expected_path = "last-compilation-digest-#{Shakapacker.env}"

expect(actual_path).to eq expected_path
end

it "generates correct compilation_digest_path with " do
allow(Shakapacker.config).to receive(:fetch).with(any_args).and_call_original
allow(Shakapacker.config).to receive(:fetch).with(:compiler_strategy_asset_host_sensitive).and_return(true)

ENV["SHAKAPACKER_ASSET_HOST"] = "custom-path"

actual_path = @digest_strategy.send(:compilation_digest_path).basename.to_s
host_hash = Digest::SHA1.hexdigest("-custom-path")
expected_path = "last-compilation-digest-#{Shakapacker.env}-#{host_hash}"

expect(actual_path).to eq expected_path
Expand Down

0 comments on commit cb84653

Please sign in to comment.