From 86cea46f3c52dc1848e3403b016adda827524d3d Mon Sep 17 00:00:00 2001 From: Mostafa Ahangarha Date: Wed, 4 Oct 2023 14:57:44 +0330 Subject: [PATCH 01/19] Apply logic for digest strategy --- lib/shakapacker/digest_strategy.rb | 7 ++++++- spec/shakapacker/digest_strategy_spec.rb | 16 +++++++++++++++- 2 files changed, 21 insertions(+), 2 deletions(-) diff --git a/lib/shakapacker/digest_strategy.rb b/lib/shakapacker/digest_strategy.rb index 65eadff15..789adbf2c 100644 --- a/lib/shakapacker/digest_strategy.rb +++ b/lib/shakapacker/digest_strategy.rb @@ -53,7 +53,12 @@ def record_compilation_digest end def compilation_digest_path - config.cache_path.join("last-compilation-digest-#{Shakapacker.env}") + config.cache_path.join("last-compilation-digest-#{Shakapacker.env}-#{generate_host_hash}") + end + + def generate_host_hash(*keys) + keys = [Rails.application.config.asset_host, ENV["SHAKAPACKER_ASSET_HOST"]] if keys.empty? + Digest::SHA1.hexdigest(keys.join("-")) end end end diff --git a/spec/shakapacker/digest_strategy_spec.rb b/spec/shakapacker/digest_strategy_spec.rb index 1c727c17b..410371dea 100644 --- a/spec/shakapacker/digest_strategy_spec.rb +++ b/spec/shakapacker/digest_strategy_spec.rb @@ -28,9 +28,23 @@ def remove_compilation_digest_path expect(@digest_strategy.fresh?).to be true end + it "is stale when host changes" do + ENV["SHAKAPACKER_ASSET_HOST"] = "the-host" + + @digest_strategy.after_compile_hook + + ENV["SHAKAPACKER_ASSET_HOST"] = "new-host" + + expect(@digest_strategy.stale?).to be true + expect(@digest_strategy.fresh?).to be_falsey + + ENV["SHAKAPACKER_ASSET_HOST"] = "" + end + it "generates correct compilation_digest_path" do actual_path = @digest_strategy.send(:compilation_digest_path).basename.to_s - expected_path = "last-compilation-digest-#{Shakapacker.env}" + host_hash = Digest::SHA1.hexdigest("") + expected_path = "last-compilation-digest-#{Shakapacker.env}-#{host_hash}" expect(actual_path).to eq expected_path end From 99100199ecea88d2a94a29119ef70ed0cc6b0b5c Mon Sep 17 00:00:00 2001 From: Mostafa Ahangarha Date: Wed, 4 Oct 2023 15:36:15 +0330 Subject: [PATCH 02/19] Add backward compatibility --- lib/install/config/shakapacker.yml | 3 +++ lib/shakapacker/digest_strategy.rb | 5 ++++- spec/shakapacker/digest_strategy_spec.rb | 19 +++++++++++++++++-- 3 files changed, 24 insertions(+), 3 deletions(-) diff --git a/lib/install/config/shakapacker.yml b/lib/install/config/shakapacker.yml index 738766bd1..b18d18ba3 100644 --- a/lib/install/config/shakapacker.yml +++ b/lib/install/config/shakapacker.yml @@ -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 diff --git a/lib/shakapacker/digest_strategy.rb b/lib/shakapacker/digest_strategy.rb index 789adbf2c..b0770feb7 100644 --- a/lib/shakapacker/digest_strategy.rb +++ b/lib/shakapacker/digest_strategy.rb @@ -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) diff --git a/spec/shakapacker/digest_strategy_spec.rb b/spec/shakapacker/digest_strategy_spec.rb index 410371dea..124aa85dd 100644 --- a/spec/shakapacker/digest_strategy_spec.rb +++ b/spec/shakapacker/digest_strategy_spec.rb @@ -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 @@ -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 From ed68c79a9b0f5287404d31cd568161cb3a90a3aa Mon Sep 17 00:00:00 2001 From: Mostafa Ahangarha Date: Wed, 4 Oct 2023 15:51:49 +0330 Subject: [PATCH 03/19] Memoize the genrerated host hash --- lib/shakapacker/digest_strategy.rb | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/lib/shakapacker/digest_strategy.rb b/lib/shakapacker/digest_strategy.rb index b0770feb7..146627516 100644 --- a/lib/shakapacker/digest_strategy.rb +++ b/lib/shakapacker/digest_strategy.rb @@ -60,8 +60,11 @@ def compilation_digest_path end def generate_host_hash(*keys) + @generated_host_hashes ||= {} + keys = [Rails.application.config.asset_host, ENV["SHAKAPACKER_ASSET_HOST"]] if keys.empty? - Digest::SHA1.hexdigest(keys.join("-")) + + @generated_host_hashes[keys] = Digest::SHA1.hexdigest(keys.join("-")) end end end From a990c1a17273e688fb232da6cd4499653fd8993a Mon Sep 17 00:00:00 2001 From: Mostafa Ahangarha Date: Wed, 4 Oct 2023 18:09:41 +0330 Subject: [PATCH 04/19] Add documentation --- README.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/README.md b/README.md index 485198fa2..7fba493b4 100644 --- a/README.md +++ b/README.md @@ -447,6 +447,8 @@ For more details see If you want to use live code reloading, or you have enough JavaScript that on-demand compilation is too slow, you'll need to run `./bin/shakapacker-dev-server`. This process will watch for changes in the relevant files, defined by `shakapacker.yml` configuration settings for `source_path`, `source_entry_path`, and `additional_paths`, and it will then automatically reload the browser to match. This feature is also known as [Hot Module Replacement](https://webpack.js.org/concepts/hot-module-replacement/). +**Note:** If you want to enforce recompilation on asset host change (for example, through `SHAKAPACKER_ASSET_HOST` environment variable), in addition to using digest strategy, you should set `compiler_strategy_asset_host_sensitive: true` in `config/shakapacker.yml`. + #### Common Development Commands ```bash From 3c7c79a553ed631dac00b430a2a0b02c3816073f Mon Sep 17 00:00:00 2001 From: Mostafa Ahangarha Date: Wed, 4 Oct 2023 18:09:56 +0330 Subject: [PATCH 05/19] Update changelog --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 19f796052..8679e1bc3 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -14,6 +14,7 @@ _Please add entries here for your pull requests that are not yet released._ ### Fixed - 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) +- Recompile assets on asset host change [PR 364](https://github.com/shakacode/shakapacker/pull/364) by [ahangarha](https://github.com/ahangarha). ## [v7.1.0] - September 30, 2023 From 013ea12c27e70441745f820b19fa74e07f0cd8a9 Mon Sep 17 00:00:00 2001 From: Mostafa Ahangarha Date: Thu, 5 Oct 2023 11:16:54 +0330 Subject: [PATCH 06/19] Fix memoization --- lib/shakapacker/digest_strategy.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/shakapacker/digest_strategy.rb b/lib/shakapacker/digest_strategy.rb index 146627516..282ee26b7 100644 --- a/lib/shakapacker/digest_strategy.rb +++ b/lib/shakapacker/digest_strategy.rb @@ -64,7 +64,7 @@ def generate_host_hash(*keys) keys = [Rails.application.config.asset_host, ENV["SHAKAPACKER_ASSET_HOST"]] if keys.empty? - @generated_host_hashes[keys] = Digest::SHA1.hexdigest(keys.join("-")) + @generated_host_hashes[keys] ||= Digest::SHA1.hexdigest(keys.join("-")) end end end From d730b3a37cc7756d009355e760aa8932564f2817 Mon Sep 17 00:00:00 2001 From: Mostafa Ahangarha Date: Thu, 5 Oct 2023 11:19:12 +0330 Subject: [PATCH 07/19] Remove unused arguments --- lib/shakapacker/digest_strategy.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/shakapacker/digest_strategy.rb b/lib/shakapacker/digest_strategy.rb index 282ee26b7..e7b904d6b 100644 --- a/lib/shakapacker/digest_strategy.rb +++ b/lib/shakapacker/digest_strategy.rb @@ -59,10 +59,10 @@ def compilation_digest_path config.cache_path.join(path) end - def generate_host_hash(*keys) + def generate_host_hash @generated_host_hashes ||= {} - keys = [Rails.application.config.asset_host, ENV["SHAKAPACKER_ASSET_HOST"]] if keys.empty? + keys = [Rails.application.config.asset_host, ENV["SHAKAPACKER_ASSET_HOST"]] @generated_host_hashes[keys] ||= Digest::SHA1.hexdigest(keys.join("-")) end From b0c81846d9d745f5afbe7dedaf7b87815a88922a Mon Sep 17 00:00:00 2001 From: Mostafa Ahangarha Date: Mon, 16 Oct 2023 14:01:50 +0330 Subject: [PATCH 08/19] Remove config for asset host sensitivity --- lib/install/config/shakapacker.yml | 3 --- lib/shakapacker/digest_strategy.rb | 7 +++++-- spec/backward_compatibility_specs/digest_strategy_spec.rb | 1 + spec/shakapacker/digest_strategy_spec.rb | 8 ++++---- 4 files changed, 10 insertions(+), 9 deletions(-) diff --git a/lib/install/config/shakapacker.yml b/lib/install/config/shakapacker.yml index b18d18ba3..738766bd1 100644 --- a/lib/install/config/shakapacker.yml +++ b/lib/install/config/shakapacker.yml @@ -45,9 +45,6 @@ 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 diff --git a/lib/shakapacker/digest_strategy.rb b/lib/shakapacker/digest_strategy.rb index e7b904d6b..e84fe705a 100644 --- a/lib/shakapacker/digest_strategy.rb +++ b/lib/shakapacker/digest_strategy.rb @@ -54,13 +54,16 @@ def record_compilation_digest def compilation_digest_path path = "last-compilation-digest-#{Shakapacker.env}" - path += "-#{generate_host_hash}" if Shakapacker.config.fetch(:compiler_strategy_asset_host_sensitive) + path += "-#{generate_host_hash}" if generate_host_hash.present? config.cache_path.join(path) end def generate_host_hash - @generated_host_hashes ||= {} + # Using hash for memoizing the host hash is to make testing easier. + # The default value, prevents generating hash in the situation where no value for asset_host + # and SHAKAPACKER_ASSET_HOST are provided, leading to not add hash to the asset path. + @generated_host_hashes ||= { [nil, nil] => "" } keys = [Rails.application.config.asset_host, ENV["SHAKAPACKER_ASSET_HOST"]] diff --git a/spec/backward_compatibility_specs/digest_strategy_spec.rb b/spec/backward_compatibility_specs/digest_strategy_spec.rb index 6a25296d9..0cd91f955 100644 --- a/spec/backward_compatibility_specs/digest_strategy_spec.rb +++ b/spec/backward_compatibility_specs/digest_strategy_spec.rb @@ -9,6 +9,7 @@ def remove_compilation_digest_path before :all do @digest_strategy = Webpacker::DigestStrategy.new + ENV["SHAKAPACKER_ASSET_HOST"] = nil remove_compilation_digest_path end diff --git a/spec/shakapacker/digest_strategy_spec.rb b/spec/shakapacker/digest_strategy_spec.rb index 124aa85dd..6763900e3 100644 --- a/spec/shakapacker/digest_strategy_spec.rb +++ b/spec/shakapacker/digest_strategy_spec.rb @@ -29,8 +29,8 @@ 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) + # 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" @@ -52,8 +52,8 @@ def remove_compilation_digest_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) + # 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" From d8f8e6aade0435c60908484c86b986cc265b89a7 Mon Sep 17 00:00:00 2001 From: Mostafa Ahangarha Date: Mon, 16 Oct 2023 14:18:16 +0330 Subject: [PATCH 09/19] Update documentation --- README.md | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 7fba493b4..3e6888f93 100644 --- a/README.md +++ b/README.md @@ -447,7 +447,12 @@ For more details see If you want to use live code reloading, or you have enough JavaScript that on-demand compilation is too slow, you'll need to run `./bin/shakapacker-dev-server`. This process will watch for changes in the relevant files, defined by `shakapacker.yml` configuration settings for `source_path`, `source_entry_path`, and `additional_paths`, and it will then automatically reload the browser to match. This feature is also known as [Hot Module Replacement](https://webpack.js.org/concepts/hot-module-replacement/). -**Note:** If you want to enforce recompilation on asset host change (for example, through `SHAKAPACKER_ASSET_HOST` environment variable), in addition to using digest strategy, you should set `compiler_strategy_asset_host_sensitive: true` in `config/shakapacker.yml`. +**Note:** +In digest strategy, +if you set a custom asset host either through +`Rails.application.config.asset_host` or `SHAKAPACKER_ASSET_HOST` environment variable, +a hash gets added to the asset path. +This feature causes recompilation of assets by changing the asset host. #### Common Development Commands From 54fce4d13f177beecdde6330a9a7fa7947b2657e Mon Sep 17 00:00:00 2001 From: Judah Meek Date: Tue, 24 Oct 2023 17:22:52 -0500 Subject: [PATCH 10/19] simplify host_hash --- lib/shakapacker/digest_strategy.rb | 16 ++++----------- spec/shakapacker/digest_strategy_spec.rb | 25 +++++++++--------------- 2 files changed, 13 insertions(+), 28 deletions(-) diff --git a/lib/shakapacker/digest_strategy.rb b/lib/shakapacker/digest_strategy.rb index e84fe705a..ed00ff307 100644 --- a/lib/shakapacker/digest_strategy.rb +++ b/lib/shakapacker/digest_strategy.rb @@ -54,20 +54,12 @@ def record_compilation_digest def compilation_digest_path path = "last-compilation-digest-#{Shakapacker.env}" - path += "-#{generate_host_hash}" if generate_host_hash.present? + if Shakapacker::Compiler.env["SHAKAPACKER_ASSET_HOST"].present? + host_hash = Digest::SHA1.hexdigest(Shakapacker::Compiler.env["SHAKAPACKER_ASSET_HOST"]) + path += "-#{host_hash}" + end config.cache_path.join(path) end - - def generate_host_hash - # Using hash for memoizing the host hash is to make testing easier. - # The default value, prevents generating hash in the situation where no value for asset_host - # and SHAKAPACKER_ASSET_HOST are provided, leading to not add hash to the asset path. - @generated_host_hashes ||= { [nil, nil] => "" } - - keys = [Rails.application.config.asset_host, ENV["SHAKAPACKER_ASSET_HOST"]] - - @generated_host_hashes[keys] ||= Digest::SHA1.hexdigest(keys.join("-")) - end end end diff --git a/spec/shakapacker/digest_strategy_spec.rb b/spec/shakapacker/digest_strategy_spec.rb index 6763900e3..1b343dd9b 100644 --- a/spec/shakapacker/digest_strategy_spec.rb +++ b/spec/shakapacker/digest_strategy_spec.rb @@ -29,38 +29,31 @@ 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) + old_host = Shakapacker::Compiler.env["SHAKAPACKER_ASSET_HOST"] - ENV["SHAKAPACKER_ASSET_HOST"] = "the-host" + Shakapacker::Compiler.env["SHAKAPACKER_ASSET_HOST"] = "the-host" @digest_strategy.after_compile_hook - ENV["SHAKAPACKER_ASSET_HOST"] = "new-host" + Shakapacker::Compiler.env["SHAKAPACKER_ASSET_HOST"] = "new-host" expect(@digest_strategy.stale?).to be true expect(@digest_strategy.fresh?).to be_falsey - ENV["SHAKAPACKER_ASSET_HOST"] = nil + Shakapacker::Compiler.env["SHAKAPACKER_ASSET_HOST"] = old_host end it "generates correct compilation_digest_path" do - actual_path = @digest_strategy.send(:compilation_digest_path).basename.to_s - 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) + old_host = Shakapacker::Compiler.env["SHAKAPACKER_ASSET_HOST"] - ENV["SHAKAPACKER_ASSET_HOST"] = "custom-path" + Shakapacker::Compiler.env["SHAKAPACKER_ASSET_HOST"] = "custom-path" actual_path = @digest_strategy.send(:compilation_digest_path).basename.to_s - host_hash = Digest::SHA1.hexdigest("-custom-path") + host_hash = Digest::SHA1.hexdigest("custom-path") expected_path = "last-compilation-digest-#{Shakapacker.env}-#{host_hash}" expect(actual_path).to eq expected_path + + Shakapacker::Compiler.env["SHAKAPACKER_ASSET_HOST"] = old_host end end From 9f64f4a7c3cd1a27afd4c81f6a4bc45c52e7d221 Mon Sep 17 00:00:00 2001 From: Mostafa Ahangarha Date: Tue, 31 Oct 2023 17:28:54 +0330 Subject: [PATCH 11/19] Use config.asset_host --- lib/shakapacker/digest_strategy.rb | 4 +-- spec/shakapacker/digest_strategy_spec.rb | 42 +++++++++++++----------- 2 files changed, 24 insertions(+), 22 deletions(-) diff --git a/lib/shakapacker/digest_strategy.rb b/lib/shakapacker/digest_strategy.rb index ed00ff307..30eda5d0a 100644 --- a/lib/shakapacker/digest_strategy.rb +++ b/lib/shakapacker/digest_strategy.rb @@ -54,8 +54,8 @@ def record_compilation_digest def compilation_digest_path path = "last-compilation-digest-#{Shakapacker.env}" - if Shakapacker::Compiler.env["SHAKAPACKER_ASSET_HOST"].present? - host_hash = Digest::SHA1.hexdigest(Shakapacker::Compiler.env["SHAKAPACKER_ASSET_HOST"]) + if Shakapacker.config.asset_host.present? + host_hash = Digest::SHA1.hexdigest(Shakapacker.config.asset_host) path += "-#{host_hash}" end diff --git a/spec/shakapacker/digest_strategy_spec.rb b/spec/shakapacker/digest_strategy_spec.rb index 1b343dd9b..c39f3ad76 100644 --- a/spec/shakapacker/digest_strategy_spec.rb +++ b/spec/shakapacker/digest_strategy_spec.rb @@ -29,31 +29,33 @@ def remove_compilation_digest_path end it "is stale when host changes" do - old_host = Shakapacker::Compiler.env["SHAKAPACKER_ASSET_HOST"] - - Shakapacker::Compiler.env["SHAKAPACKER_ASSET_HOST"] = "the-host" - - @digest_strategy.after_compile_hook - - Shakapacker::Compiler.env["SHAKAPACKER_ASSET_HOST"] = "new-host" - - expect(@digest_strategy.stale?).to be true - expect(@digest_strategy.fresh?).to be_falsey - - Shakapacker::Compiler.env["SHAKAPACKER_ASSET_HOST"] = old_host + with_env_variable("SHAKAPACKER_ASSET_HOST" => "old-host") do + # Record the digests for old-host + @digest_strategy.after_compile_hook + + with_env_variable("SHAKAPACKER_ASSET_HOST" => "new-host") do + expect(@digest_strategy.stale?).to be true + expect(@digest_strategy.fresh?).to be_falsey + end + end end it "generates correct compilation_digest_path" do - old_host = Shakapacker::Compiler.env["SHAKAPACKER_ASSET_HOST"] - - Shakapacker::Compiler.env["SHAKAPACKER_ASSET_HOST"] = "custom-path" + with_env_variable("SHAKAPACKER_ASSET_HOST" => "custom-path") do + 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}" - 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 + end + end - expect(actual_path).to eq expected_path + it "generates correct compilation_digest_path without the digest of the asset host if asset host is not set" do + with_env_variable("SHAKAPACKER_ASSET_HOST" => nil) do + actual_path = @digest_strategy.send(:compilation_digest_path).basename.to_s + expected_path = "last-compilation-digest-#{Shakapacker.env}" - Shakapacker::Compiler.env["SHAKAPACKER_ASSET_HOST"] = old_host + expect(actual_path).to eq expected_path + end end end From ed6890f73cd01e0d56a5cf216a919c400b172a1c Mon Sep 17 00:00:00 2001 From: Mostafa Ahangarha Date: Tue, 31 Oct 2023 18:08:51 +0330 Subject: [PATCH 12/19] Improve readability --- lib/shakapacker/digest_strategy.rb | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/lib/shakapacker/digest_strategy.rb b/lib/shakapacker/digest_strategy.rb index 30eda5d0a..dec53a27a 100644 --- a/lib/shakapacker/digest_strategy.rb +++ b/lib/shakapacker/digest_strategy.rb @@ -53,11 +53,10 @@ def record_compilation_digest end def compilation_digest_path + asset_host = Shakapacker.config.asset_host + path = "last-compilation-digest-#{Shakapacker.env}" - if Shakapacker.config.asset_host.present? - host_hash = Digest::SHA1.hexdigest(Shakapacker.config.asset_host) - path += "-#{host_hash}" - end + path += "-#{Digest::SHA1.hexdigest(asset_host)}" if asset_host.present? config.cache_path.join(path) end From f1e4e93dfee7b2a81734f07384f2944257ae2772 Mon Sep 17 00:00:00 2001 From: Mostafa Ahangarha Date: Tue, 31 Oct 2023 18:09:11 +0330 Subject: [PATCH 13/19] Improve spec helper docs --- spec/shakapacker/spec_helper_initializer.rb | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/spec/shakapacker/spec_helper_initializer.rb b/spec/shakapacker/spec_helper_initializer.rb index 400b7b525..b3131a849 100644 --- a/spec/shakapacker/spec_helper_initializer.rb +++ b/spec/shakapacker/spec_helper_initializer.rb @@ -24,8 +24,10 @@ def with_rails_env(env) end # Temportarily set env variables to a custom value -# arg: a hash with key:value for each custom env -# Keys could be string or symbol +# +# Params +# +custom_env_hash+:: A hash with key:value for each custom env. +# Keys could be string or symbol def with_env_variable(custom_env_hash) original_env = {} custom_env_hash.each do |key, new_value| From e8c2c89eabd2c7ce0463d9249b67935983f9f043 Mon Sep 17 00:00:00 2001 From: Mostafa Ahangarha Date: Mon, 6 Nov 2023 17:21:10 +0330 Subject: [PATCH 14/19] Stub ENV --- spec/shakapacker/compiler_spec.rb | 14 +++----- spec/shakapacker/configuration_spec.rb | 22 ++++++------ spec/shakapacker/digest_strategy_spec.rb | 38 ++++++++++----------- spec/shakapacker/spec_helper_initializer.rb | 20 ----------- 4 files changed, 33 insertions(+), 61 deletions(-) diff --git a/spec/shakapacker/compiler_spec.rb b/spec/shakapacker/compiler_spec.rb index 96ba78f73..672a85e71 100644 --- a/spec/shakapacker/compiler_spec.rb +++ b/spec/shakapacker/compiler_spec.rb @@ -50,14 +50,10 @@ expect(Shakapacker.compiler.send(:webpack_env)["SHAKAPACKER_ASSET_HOST"]).to be nil expect(Shakapacker.compiler.send(:webpack_env)["SHAKAPACKER_RELATIVE_URL_ROOT"]).to be nil - custom_env_variables = { - "SHAKAPACKER_ASSET_HOST" => "foo.bar", - "SHAKAPACKER_RELATIVE_URL_ROOT" => "/baz" - } - - with_env_variable(custom_env_variables) do - expect(Shakapacker.compiler.send(:webpack_env)["SHAKAPACKER_ASSET_HOST"]).to eq "foo.bar" - expect(Shakapacker.compiler.send(:webpack_env)["SHAKAPACKER_RELATIVE_URL_ROOT"]).to eq "/baz" - end + allow(ENV).to receive(:fetch).with("SHAKAPACKER_ASSET_HOST", nil).and_return("foo.bar") + allow(ENV).to receive(:fetch).with("SHAKAPACKER_RELATIVE_URL_ROOT", nil).and_return("/baz") + + expect(Shakapacker.compiler.send(:webpack_env)["SHAKAPACKER_ASSET_HOST"]).to eq "foo.bar" + expect(Shakapacker.compiler.send(:webpack_env)["SHAKAPACKER_RELATIVE_URL_ROOT"]).to eq "/baz" end end diff --git a/spec/shakapacker/configuration_spec.rb b/spec/shakapacker/configuration_spec.rb index 9f6f6c6b8..f41e61bde 100644 --- a/spec/shakapacker/configuration_spec.rb +++ b/spec/shakapacker/configuration_spec.rb @@ -341,17 +341,16 @@ end it "returns the value of SHAKAPACKER_ASSET_HOST if set" do - with_env_variable("SHAKAPACKER_ASSET_HOST" => "custom_host.abc") do - expect(config.asset_host).to eq "custom_host.abc" - end + expect(ENV).to receive(:fetch).with("SHAKAPACKER_ASSET_HOST", nil).and_return("custom_host.abc") + + expect(config.asset_host).to eq "custom_host.abc" end it "returns ActionController::Base.helpers.compute_asset_host if SHAKAPACKER_ASSET_HOST is not set" do allow(ActionController::Base.helpers).to receive(:compute_asset_host).and_return("domain.abc") + allow(ENV).to receive(:fetch).with("SHAKAPACKER_ASSET_HOST", "domain.abc").and_return("domain.abc") - with_env_variable("SHAKAPACKER_ASSET_HOST" => nil) do - expect(config.asset_host).to eq "domain.abc" - end + expect(config.asset_host).to eq "domain.abc" end end @@ -365,17 +364,16 @@ end it "returns the value of SHAKAPACKER_RELATIVE_URL_ROOT if set" do - with_env_variable("SHAKAPACKER_RELATIVE_URL_ROOT" => "custom_value") do - expect(config.relative_url_root).to eq "custom_value" - end + allow(ENV).to receive(:fetch).with("SHAKAPACKER_RELATIVE_URL_ROOT", nil).and_return("custom_value") + + expect(config.relative_url_root).to eq "custom_value" end it "returns ActionController::Base.helpers.compute_asset_host if SHAKAPACKER_RELATIVE_URL_ROOT is not set" do allow(ActionController::Base).to receive(:relative_url_root).and_return("abcd") + allow(ENV).to receive(:fetch).with("SHAKAPACKER_RELATIVE_URL_ROOT", "abcd").and_return("abcd") - with_env_variable("SHAKAPACKER_RELATIVE_URL_ROOT" => nil) do - expect(config.relative_url_root).to eq "abcd" - end + expect(config.relative_url_root).to eq "abcd" end end end diff --git a/spec/shakapacker/digest_strategy_spec.rb b/spec/shakapacker/digest_strategy_spec.rb index c39f3ad76..07210389f 100644 --- a/spec/shakapacker/digest_strategy_spec.rb +++ b/spec/shakapacker/digest_strategy_spec.rb @@ -29,33 +29,31 @@ def remove_compilation_digest_path end it "is stale when host changes" do - with_env_variable("SHAKAPACKER_ASSET_HOST" => "old-host") do - # Record the digests for old-host - @digest_strategy.after_compile_hook - - with_env_variable("SHAKAPACKER_ASSET_HOST" => "new-host") do - expect(@digest_strategy.stale?).to be true - expect(@digest_strategy.fresh?).to be_falsey - end - end + allow(ENV).to receive(:fetch).with("SHAKAPACKER_ASSET_HOST", nil).and_return("old-host") + # Record the digests for old-host + @digest_strategy.after_compile_hook + + allow(ENV).to receive(:fetch).with("SHAKAPACKER_ASSET_HOST", nil).and_return("new-host") + expect(@digest_strategy.stale?).to be true + expect(@digest_strategy.fresh?).to be_falsey end it "generates correct compilation_digest_path" do - with_env_variable("SHAKAPACKER_ASSET_HOST" => "custom-path") do - 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}" + allow(ENV).to receive(:fetch).with("SHAKAPACKER_ASSET_HOST", nil).and_return("custom-path") - expect(actual_path).to eq expected_path - end + 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 end it "generates correct compilation_digest_path without the digest of the asset host if asset host is not set" do - with_env_variable("SHAKAPACKER_ASSET_HOST" => nil) do - actual_path = @digest_strategy.send(:compilation_digest_path).basename.to_s - expected_path = "last-compilation-digest-#{Shakapacker.env}" + allow(ENV).to receive(:fetch).with("SHAKAPACKER_ASSET_HOST", nil).and_return(nil) - expect(actual_path).to eq expected_path - end + actual_path = @digest_strategy.send(:compilation_digest_path).basename.to_s + expected_path = "last-compilation-digest-#{Shakapacker.env}" + + expect(actual_path).to eq expected_path end end diff --git a/spec/shakapacker/spec_helper_initializer.rb b/spec/shakapacker/spec_helper_initializer.rb index b3131a849..65209edc9 100644 --- a/spec/shakapacker/spec_helper_initializer.rb +++ b/spec/shakapacker/spec_helper_initializer.rb @@ -22,23 +22,3 @@ def with_rails_env(env) Rails.env = ActiveSupport::StringInquirer.new(original) reloaded_config end - -# Temportarily set env variables to a custom value -# -# Params -# +custom_env_hash+:: A hash with key:value for each custom env. -# Keys could be string or symbol -def with_env_variable(custom_env_hash) - original_env = {} - custom_env_hash.each do |key, new_value| - upcased_key = key.to_s.upcase - original_env[upcased_key] = new_value - ENV[upcased_key] = new_value - end - - yield -ensure - original_env.each do |key, original_value| - ENV[key] = original_value - end -end From 4d4849128d4b203910581785bd3634ec0dce12b4 Mon Sep 17 00:00:00 2001 From: Mostafa Ahangarha Date: Tue, 7 Nov 2023 12:05:35 +0330 Subject: [PATCH 15/19] Make asset_host affect watched_files_digest rather file name --- lib/shakapacker/digest_strategy.rb | 11 ++++------- spec/shakapacker/digest_strategy_spec.rb | 2 +- 2 files changed, 5 insertions(+), 8 deletions(-) diff --git a/lib/shakapacker/digest_strategy.rb b/lib/shakapacker/digest_strategy.rb index dec53a27a..24b26abe0 100644 --- a/lib/shakapacker/digest_strategy.rb +++ b/lib/shakapacker/digest_strategy.rb @@ -44,7 +44,9 @@ def watched_files_digest end files = Dir[*expanded_paths].reject { |f| File.directory?(f) } file_ids = files.sort.map { |f| "#{File.basename(f)}/#{Digest::SHA1.file(f).hexdigest}" } - Digest::SHA1.hexdigest(file_ids.join("/")) + + asset_host = Shakapacker.config.asset_host.to_s + Digest::SHA1.hexdigest(file_ids.join("/").concat(asset_host)) end def record_compilation_digest @@ -53,12 +55,7 @@ def record_compilation_digest end def compilation_digest_path - asset_host = Shakapacker.config.asset_host - - path = "last-compilation-digest-#{Shakapacker.env}" - path += "-#{Digest::SHA1.hexdigest(asset_host)}" if asset_host.present? - - config.cache_path.join(path) + config.cache_path.join("last-compilation-digest-#{Shakapacker.env}") end end end diff --git a/spec/shakapacker/digest_strategy_spec.rb b/spec/shakapacker/digest_strategy_spec.rb index 07210389f..acf319799 100644 --- a/spec/shakapacker/digest_strategy_spec.rb +++ b/spec/shakapacker/digest_strategy_spec.rb @@ -43,7 +43,7 @@ def remove_compilation_digest_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}" + expected_path = "last-compilation-digest-#{Shakapacker.env}" expect(actual_path).to eq expected_path end From a5fe8aa245dfa7f371febf6d59b7aeecfc4df380 Mon Sep 17 00:00:00 2001 From: Mostafa Ahangarha Date: Tue, 7 Nov 2023 17:34:51 +0330 Subject: [PATCH 16/19] Update docs in readme --- README.md | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 3e6888f93..c49d154d5 100644 --- a/README.md +++ b/README.md @@ -449,10 +449,9 @@ If you want to use live code reloading, or you have enough JavaScript that on-de **Note:** In digest strategy, -if you set a custom asset host either through +setting or changing the asset host either through `Rails.application.config.asset_host` or `SHAKAPACKER_ASSET_HOST` environment variable, -a hash gets added to the asset path. -This feature causes recompilation of assets by changing the asset host. +forces the recompilation of assets. #### Common Development Commands From 4664179ccf4b11a92e363ebe3e41d5283e2ab01b Mon Sep 17 00:00:00 2001 From: Mostafa Ahangarha Date: Sat, 18 Nov 2023 23:08:08 +0330 Subject: [PATCH 17/19] Revert "Update docs in readme" This reverts commit 8a5e8d991710ad06b781ceae19fa6f658684da4b. --- README.md | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index c49d154d5..3e6888f93 100644 --- a/README.md +++ b/README.md @@ -449,9 +449,10 @@ If you want to use live code reloading, or you have enough JavaScript that on-de **Note:** In digest strategy, -setting or changing the asset host either through +if you set a custom asset host either through `Rails.application.config.asset_host` or `SHAKAPACKER_ASSET_HOST` environment variable, -forces the recompilation of assets. +a hash gets added to the asset path. +This feature causes recompilation of assets by changing the asset host. #### Common Development Commands From b96ed069f6fd89b1a24d9177eb44d979ff46b1bc Mon Sep 17 00:00:00 2001 From: Mostafa Ahangarha Date: Fri, 24 Nov 2023 21:58:29 +0330 Subject: [PATCH 18/19] Revert "Update documentation" This reverts commit 272b36a7beff2a30cc32ac2cd4d8072fe2cc9f33. --- README.md | 7 +------ 1 file changed, 1 insertion(+), 6 deletions(-) diff --git a/README.md b/README.md index 3e6888f93..7fba493b4 100644 --- a/README.md +++ b/README.md @@ -447,12 +447,7 @@ For more details see If you want to use live code reloading, or you have enough JavaScript that on-demand compilation is too slow, you'll need to run `./bin/shakapacker-dev-server`. This process will watch for changes in the relevant files, defined by `shakapacker.yml` configuration settings for `source_path`, `source_entry_path`, and `additional_paths`, and it will then automatically reload the browser to match. This feature is also known as [Hot Module Replacement](https://webpack.js.org/concepts/hot-module-replacement/). -**Note:** -In digest strategy, -if you set a custom asset host either through -`Rails.application.config.asset_host` or `SHAKAPACKER_ASSET_HOST` environment variable, -a hash gets added to the asset path. -This feature causes recompilation of assets by changing the asset host. +**Note:** If you want to enforce recompilation on asset host change (for example, through `SHAKAPACKER_ASSET_HOST` environment variable), in addition to using digest strategy, you should set `compiler_strategy_asset_host_sensitive: true` in `config/shakapacker.yml`. #### Common Development Commands From 6fad55e51a1924ea6e8bb023387b251da4965780 Mon Sep 17 00:00:00 2001 From: Mostafa Ahangarha Date: Sat, 25 Nov 2023 01:00:51 +0330 Subject: [PATCH 19/19] Remove the changes in the README file --- README.md | 2 -- 1 file changed, 2 deletions(-) diff --git a/README.md b/README.md index 7fba493b4..485198fa2 100644 --- a/README.md +++ b/README.md @@ -447,8 +447,6 @@ For more details see If you want to use live code reloading, or you have enough JavaScript that on-demand compilation is too slow, you'll need to run `./bin/shakapacker-dev-server`. This process will watch for changes in the relevant files, defined by `shakapacker.yml` configuration settings for `source_path`, `source_entry_path`, and `additional_paths`, and it will then automatically reload the browser to match. This feature is also known as [Hot Module Replacement](https://webpack.js.org/concepts/hot-module-replacement/). -**Note:** If you want to enforce recompilation on asset host change (for example, through `SHAKAPACKER_ASSET_HOST` environment variable), in addition to using digest strategy, you should set `compiler_strategy_asset_host_sensitive: true` in `config/shakapacker.yml`. - #### Common Development Commands ```bash