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

Assets do not recompile when SHAKAPACKER_ASSET_HOST changes #328

Closed
eddiesiegel opened this issue Jul 6, 2023 · 2 comments · Fixed by #364
Closed

Assets do not recompile when SHAKAPACKER_ASSET_HOST changes #328

eddiesiegel opened this issue Jul 6, 2023 · 2 comments · Fixed by #364
Assignees
Labels

Comments

@eddiesiegel
Copy link

Expected behavior:

  1. Run rails assets:precompile w/SHAKAPACKER_ASSET_HOST=host-a
  2. Run rails assets:precompile w/SHAKAPACKER_ASSET_HOST=host-b

I would expect that any assets that depended on the asset host would be recompiled the second time and I would now have two different copies of assets w/different fingerprints.

For example, if my application.js includes paths to assets that change when the asset host changes, I would expect to end up with application-hash1.js and application-hash2.js in my output directory.

We do this internally so that we can include a precompiled version of both our production and our staging assets inside of our docker containers, and we serve those assets from different subdomains.

Actual behavior:

On the first call to rails assets:precompile assets are compiled. On the second call, I get this message:

DEBUG -- : Everything's up-to-date. Nothing to do

This appears to be because shakapacker's DigestStrategy only takes the digest of the asset and the name of the environment into account when deciding if an asset is stale.

More context:

We have been able to monkeypatch our way around this internally by doing something like this:

module Shakapacker
  class ImprovedDigestStrategy < DigestStrategy
    private

    def compilation_digest_path
      # The default DigestStrategy stores the digests in a single file per environment. Instead, we
      # want to store a separate digest for each combo of environment and asset host
      #
      # To do this, we construct a string containing the values of the env vars we care about and
      # append their hash to the filename.
      key = [Rails.application.config.asset_host, ENV['SHAKAPACKER_ASSET_HOST']].join('-')
      hash = Digest::SHA1.hexdigest(key)

      config.cache_path.join("last-compilation-digest-#{Shakapacker.env}-#{hash}")
    end
  end

  class CompilerStrategy
    class << self
      alias_method :original_from_config, :from_config
      def from_config
        if Shakapacker.config.compiler_strategy == 'digest_improved'
          Shakapacker::ImprovedDigestStrategy.new
        else
          original_from_config
        end
      end
    end
  end
end

And then in shakapacker.yml:

production:
  <<: *default
  compiler_strategy: digest_improved

Setup environment:

  • Ruby version: 3.0.1
  • Rails version: 6.1
  • Shakapacker version: 7
@eddiesiegel eddiesiegel added the bug label Jul 6, 2023
@ahangarha
Copy link
Contributor

I see this as a new useful feature proposal rather than a bug report.
@justin808 I think this is a good new feature to include. What do you say?

@ahangarha
Copy link
Contributor

@eddiesiegel Would you please check if make-compilation-stale-on-host-change branch adds the feature you need?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants