Skip to content

Invalidate asset URLs when prefix or digest settings change - #558

Closed
OskarEichler wants to merge 1 commit into
rails:masterfrom
OskarEichler:codex/invalidate-assets-for-prefix-and-digest-changes
Closed

OskarEichler wants to merge 1 commit into
rails:masterfrom
OskarEichler:codex/invalidate-assets-for-prefix-and-digest-changes

Conversation

@OskarEichler

Copy link
Copy Markdown

Summary

Include the asset prefix and digest setting in the existing asset-URL cache dependency. Reusing a Sprockets cache after either setting changes must not retain old URLs in compiled CSS.

Reproduction

Use require "sprockets/railtie"; env in the focused examples is a Sprockets environment with Rails::Context included, digest assets enabled and prefix /assets.

require "sprockets/railtie"
require "tmpdir"
Dir.mktmpdir do |dir|
  File.write(File.join(dir, "main.css"), "p { background: url(icon.png); }")
  File.write(File.join(dir, "icon.png"), "image bytes")
  cache = {}
  [["/old", true], ["/new", true], ["/new", false]].each do |prefix, digest|
    env = Sprockets::Environment.new(dir)
    env.cache = cache
    env.append_path(dir)
    env.context_class.include(Sprockets::Rails::Context)
    env.context_class.assets_prefix = prefix
    env.context_class.digest_assets = digest
    env.context_class.config = ActiveSupport::OrderedOptions.new
    env.register_postprocessor "text/css", Sprockets::Rails::AssetUrlProcessor
    puts env["main.css"].to_s
  end
end
# Before: all three outputs retain /old/icon-<digest>.png.
# After: /old/icon-<digest>.png, /new/icon-<digest>.png, /new/icon.png.

Verification

  • Unmodified existing rake test: 151 tests / 796 assertions, zero failures/errors/skips on Ruby 4.0.6 + Rails 8.1.3.1 + Sprockets 4.4.1; Ruby 3.2.11 with the same dependencies; and Ruby 3.2.11 + Rails 7.2.3.1 + Sprockets 3.7.5. Baselines pass too.
  • 48 focused external checks pass per configuration; original behavior reproduced before applying the patch.
  • MT_CPU=1 serializes Rails isolation tests that share fixture directories. External verification Gemfiles pin these combinations because the repository root Gemfile combines current Rails with Rack 2 and Sprockets 3.
  • Package remains 3.5.2 with unchanged requirements/file list. Ruby syntax and diff whitespace checked; the consumer RuboCop configuration retains the same 16 pre-existing offenses.

Compatibility and limitations

No public API signature or dependency changes. Breaking changes: none intended; the incorrect behavior described above changes. Older supported Ruby versions and Linux CI have not been run locally. No test files were added or edited under the consumer repository's no-test-edit constraint; regression evidence is supplied here and through external focused checks. Prepared with AI assistance and verified with the commands above.

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.

2 participants