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

feat: make compilation stale on asset host change #364

Merged
merged 19 commits into from
Dec 1, 2023

Conversation

ahangarha
Copy link
Contributor

@ahangarha ahangarha commented Oct 4, 2023

Summary

This PR adds a feature for making digest strategy sensitive towards asset_host change. This way, by changing SHAKAPACKER_ASSET_HOST, we get new asset compilation.

This is an opt-in feature.

Pull Request checklist

  • Add/update test to cover these changes
  • Update documentation
  • Update CHANGELOG file

Other Information

Closes #328

@ahangarha ahangarha requested a review from Judahmeek October 4, 2023 14:30
@ahangarha ahangarha changed the title WIP - feat: make compilation stale on asset host change feat: make compilation stale on asset host change Oct 4, 2023
Copy link
Contributor

@Judahmeek Judahmeek left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM. Just have one question.

lib/shakapacker/digest_strategy.rb Outdated Show resolved Hide resolved
@ahangarha
Copy link
Contributor Author

@Judahmeek
There was an issue with memoization. I fixed it. Now it only runs the digest algorithm once.

@ahangarha ahangarha requested a review from Judahmeek October 5, 2023 07:51
@ahangarha ahangarha requested a review from justin808 October 16, 2023 11:16
lib/shakapacker/digest_strategy.rb Outdated Show resolved Hide resolved
lib/shakapacker/digest_strategy.rb Outdated Show resolved Hide resolved
Copy link
Contributor Author

@ahangarha ahangarha left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Based on my comments, I wonder if we are testing the actual behavior of Shakapacker. In actual apps, I couldn't get any value for Shakapacker::Compiler.env except an empty hash.

lib/shakapacker/digest_strategy.rb Outdated Show resolved Hide resolved
spec/shakapacker/digest_strategy_spec.rb Outdated Show resolved Hide resolved
@G-Rath
Copy link
Contributor

G-Rath commented Oct 27, 2023

I think the problem here is that these are mincing config with env variables: SHAKAPACKER_ASSET_HOST gets set within a private instance method and the resulting env object is passed straight to Open3.capture3 so there's no way you can access it from the digest strategies (aka Shakapacker::Compiler.env["SHAKAPACKER_ASSET_HOST"] will never work).

What they do have is an instance of Shakapacker::Configuration, which feels like a suitable way to expose this anyway i.e. support asset_host within shakapacker.yml whose default value is ActionController::Base.helpers.compute_asset_host unless SHAKAPACKER_ASSET_HOST is set.

You should be able do that by adding this to Shakapacker::Configuration:

  def asset_host
    ENV.fetch("SHAKAPACKER_ASSET_HOST", fetch(:asset_host) || ActionController::Base.helpers.compute_asset_host)
  end

Then in the digest strategy and Shakapacker::Compiler#webpack_env you can do config.asset_host.

Also, I've not tested this but I'm pretty sure you can make this part of the digest returned by watched_files_digest rather than the filepath digest - I think that's nicer because it means those semantics are preserved and you also avoid growing the file name significantly which after a while will tend to annoy Windows.

@justin808
Copy link
Member

this needs rebasing. I merged #374

@ahangarha ahangarha force-pushed the make-compilation-stale-on-host-change branch from dc333f0 to 48a03bc Compare October 31, 2023 13:35
@@ -53,7 +53,12 @@ def record_compilation_digest
end

def compilation_digest_path
config.cache_path.join("last-compilation-digest-#{Shakapacker.env}")
asset_host = Shakapacker.config.asset_host
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can we try including this as part of the digest of the files themselves rather than the file path? that way the path will remain consistent and smaller, especially if in future there are other elements we decide we should be cachebusting on too

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a good point, and I will try to implement it.
Thanks for the suggestion.

README.md Outdated
@@ -447,6 +447,13 @@ 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:**
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fwiw imo this is a bug not a feature since its reasonable to assume given how digest works that changing the asset host would result in the digest changing, and as such doesn't require explicit documentation (similarly, I don't think it does for mtime either because that clearly says its based on timestamps only)

@justin808
Copy link
Member

@ahangarha please make changes suggested by @G-Rath!

@ahangarha
Copy link
Contributor Author

ahangarha commented Nov 7, 2023

Regarding reviews:

@ahangarha ahangarha requested review from G-Rath and justin808 November 7, 2023 09:35
Copy link
Contributor

@G-Rath G-Rath left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is looking good to me, just needs the change to the readme removed and then should be good to go

lib/shakapacker/digest_strategy.rb Show resolved Hide resolved
@justin808
Copy link
Member

LGTM

Copy link
Contributor

@G-Rath G-Rath left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the readme change is still there, but happy to do a follow-up PR removing it instead of blocking this :)

@ahangarha ahangarha force-pushed the make-compilation-stale-on-host-change branch from 51f4d6d to 15c7a6d Compare November 24, 2023 18:33
@ahangarha
Copy link
Contributor Author

the readme change is still there

Frankly, I don't know how that happened.
In any case, in resolving the merge conflict, I fixed this issue too.

README.md Outdated
@@ -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`.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@ahangarha this looks like a previous doc update that was since removed - I think it would probably be best to just do a brand new commit manually undoing the changes to the readme rather than trying to do git revert since everything will be squashed anyway

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Well...
I removed it manually.
Finally... 🙂

@ahangarha ahangarha requested a review from G-Rath November 24, 2023 21:31
@ahangarha ahangarha force-pushed the make-compilation-stale-on-host-change branch from 0bed557 to 6fad55e Compare November 25, 2023 07:45
@justin808 justin808 merged commit bcc2fbc into master Dec 1, 2023
82 checks passed
@justin808 justin808 deleted the make-compilation-stale-on-host-change branch December 1, 2023 06:03
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Assets do not recompile when SHAKAPACKER_ASSET_HOST changes
4 participants