Skip to content

Commit bcc2fbc

Browse files
authored
feat: make compilation stale on asset host change (shakacode#364)
1 parent 409e46a commit bcc2fbc

File tree

7 files changed

+42
-40
lines changed

7 files changed

+42
-40
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ _Please add entries here for your pull requests that are not yet released._
1414

1515
### Fixed
1616
- Recommend `server` option instead of deprecated `https` option when `--https` is provided [PR 380](https://github.com/shakacode/shakapacker/pull/380) by [G-Rath](https://github.com/g-rath)
17+
- Recompile assets on asset host change [PR 364](https://github.com/shakacode/shakapacker/pull/364) by [ahangarha](https://github.com/ahangarha).
1718

1819
## [v7.1.0] - September 30, 2023
1920

lib/shakapacker/digest_strategy.rb

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,9 @@ def watched_files_digest
4444
end
4545
files = Dir[*expanded_paths].reject { |f| File.directory?(f) }
4646
file_ids = files.sort.map { |f| "#{File.basename(f)}/#{Digest::SHA1.file(f).hexdigest}" }
47-
Digest::SHA1.hexdigest(file_ids.join("/"))
47+
48+
asset_host = Shakapacker.config.asset_host.to_s
49+
Digest::SHA1.hexdigest(file_ids.join("/").concat(asset_host))
4850
end
4951

5052
def record_compilation_digest

spec/backward_compatibility_specs/digest_strategy_spec.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ def remove_compilation_digest_path
99

1010
before :all do
1111
@digest_strategy = Webpacker::DigestStrategy.new
12+
ENV["SHAKAPACKER_ASSET_HOST"] = nil
1213
remove_compilation_digest_path
1314
end
1415

spec/shakapacker/compiler_spec.rb

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -50,14 +50,10 @@
5050
expect(Shakapacker.compiler.send(:webpack_env)["SHAKAPACKER_ASSET_HOST"]).to be nil
5151
expect(Shakapacker.compiler.send(:webpack_env)["SHAKAPACKER_RELATIVE_URL_ROOT"]).to be nil
5252

53-
custom_env_variables = {
54-
"SHAKAPACKER_ASSET_HOST" => "foo.bar",
55-
"SHAKAPACKER_RELATIVE_URL_ROOT" => "/baz"
56-
}
57-
58-
with_env_variable(custom_env_variables) do
59-
expect(Shakapacker.compiler.send(:webpack_env)["SHAKAPACKER_ASSET_HOST"]).to eq "foo.bar"
60-
expect(Shakapacker.compiler.send(:webpack_env)["SHAKAPACKER_RELATIVE_URL_ROOT"]).to eq "/baz"
61-
end
53+
allow(ENV).to receive(:fetch).with("SHAKAPACKER_ASSET_HOST", nil).and_return("foo.bar")
54+
allow(ENV).to receive(:fetch).with("SHAKAPACKER_RELATIVE_URL_ROOT", nil).and_return("/baz")
55+
56+
expect(Shakapacker.compiler.send(:webpack_env)["SHAKAPACKER_ASSET_HOST"]).to eq "foo.bar"
57+
expect(Shakapacker.compiler.send(:webpack_env)["SHAKAPACKER_RELATIVE_URL_ROOT"]).to eq "/baz"
6258
end
6359
end

spec/shakapacker/configuration_spec.rb

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -341,17 +341,16 @@
341341
end
342342

343343
it "returns the value of SHAKAPACKER_ASSET_HOST if set" do
344-
with_env_variable("SHAKAPACKER_ASSET_HOST" => "custom_host.abc") do
345-
expect(config.asset_host).to eq "custom_host.abc"
346-
end
344+
expect(ENV).to receive(:fetch).with("SHAKAPACKER_ASSET_HOST", nil).and_return("custom_host.abc")
345+
346+
expect(config.asset_host).to eq "custom_host.abc"
347347
end
348348

349349
it "returns ActionController::Base.helpers.compute_asset_host if SHAKAPACKER_ASSET_HOST is not set" do
350350
allow(ActionController::Base.helpers).to receive(:compute_asset_host).and_return("domain.abc")
351+
allow(ENV).to receive(:fetch).with("SHAKAPACKER_ASSET_HOST", "domain.abc").and_return("domain.abc")
351352

352-
with_env_variable("SHAKAPACKER_ASSET_HOST" => nil) do
353-
expect(config.asset_host).to eq "domain.abc"
354-
end
353+
expect(config.asset_host).to eq "domain.abc"
355354
end
356355
end
357356

@@ -365,17 +364,16 @@
365364
end
366365

367366
it "returns the value of SHAKAPACKER_RELATIVE_URL_ROOT if set" do
368-
with_env_variable("SHAKAPACKER_RELATIVE_URL_ROOT" => "custom_value") do
369-
expect(config.relative_url_root).to eq "custom_value"
370-
end
367+
allow(ENV).to receive(:fetch).with("SHAKAPACKER_RELATIVE_URL_ROOT", nil).and_return("custom_value")
368+
369+
expect(config.relative_url_root).to eq "custom_value"
371370
end
372371

373372
it "returns ActionController::Base.helpers.compute_asset_host if SHAKAPACKER_RELATIVE_URL_ROOT is not set" do
374373
allow(ActionController::Base).to receive(:relative_url_root).and_return("abcd")
374+
allow(ENV).to receive(:fetch).with("SHAKAPACKER_RELATIVE_URL_ROOT", "abcd").and_return("abcd")
375375

376-
with_env_variable("SHAKAPACKER_RELATIVE_URL_ROOT" => nil) do
377-
expect(config.relative_url_root).to eq "abcd"
378-
end
376+
expect(config.relative_url_root).to eq "abcd"
379377
end
380378
end
381379
end

spec/shakapacker/digest_strategy_spec.rb

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,29 @@ def remove_compilation_digest_path
2828
expect(@digest_strategy.fresh?).to be true
2929
end
3030

31+
it "is stale when host changes" do
32+
allow(ENV).to receive(:fetch).with("SHAKAPACKER_ASSET_HOST", nil).and_return("old-host")
33+
# Record the digests for old-host
34+
@digest_strategy.after_compile_hook
35+
36+
allow(ENV).to receive(:fetch).with("SHAKAPACKER_ASSET_HOST", nil).and_return("new-host")
37+
expect(@digest_strategy.stale?).to be true
38+
expect(@digest_strategy.fresh?).to be_falsey
39+
end
40+
3141
it "generates correct compilation_digest_path" do
42+
allow(ENV).to receive(:fetch).with("SHAKAPACKER_ASSET_HOST", nil).and_return("custom-path")
43+
44+
actual_path = @digest_strategy.send(:compilation_digest_path).basename.to_s
45+
host_hash = Digest::SHA1.hexdigest("custom-path")
46+
expected_path = "last-compilation-digest-#{Shakapacker.env}"
47+
48+
expect(actual_path).to eq expected_path
49+
end
50+
51+
it "generates correct compilation_digest_path without the digest of the asset host if asset host is not set" do
52+
allow(ENV).to receive(:fetch).with("SHAKAPACKER_ASSET_HOST", nil).and_return(nil)
53+
3254
actual_path = @digest_strategy.send(:compilation_digest_path).basename.to_s
3355
expected_path = "last-compilation-digest-#{Shakapacker.env}"
3456

spec/shakapacker/spec_helper_initializer.rb

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -22,21 +22,3 @@ def with_rails_env(env)
2222
Rails.env = ActiveSupport::StringInquirer.new(original)
2323
reloaded_config
2424
end
25-
26-
# Temportarily set env variables to a custom value
27-
# arg: a hash with key:value for each custom env
28-
# Keys could be string or symbol
29-
def with_env_variable(custom_env_hash)
30-
original_env = {}
31-
custom_env_hash.each do |key, new_value|
32-
upcased_key = key.to_s.upcase
33-
original_env[upcased_key] = new_value
34-
ENV[upcased_key] = new_value
35-
end
36-
37-
yield
38-
ensure
39-
original_env.each do |key, original_value|
40-
ENV[key] = original_value
41-
end
42-
end

0 commit comments

Comments
 (0)