sprockets-rails icon indicating copy to clipboard operation
sprockets-rails copied to clipboard

Image file extensions are now required (from v.3.0

Open virgoproz opened this issue 9 years ago • 1 comments

Until 2.3.3, it was ok to do image_tag('image')

From 3.0, to avoid getting the AssetsNotPrecompiled error, you need to mandatorily specify the file extension .... image_tag('image.png')

Again, not a big deal but a breaking change worth noting or fixing it is unwanted behavior accidentally added.

virgoproz avatar Jan 05 '16 09:01 virgoproz

We've been hit by this a few times and have added the following to our codebase to catch it. No guarantees but we've had the warning for few months and have not seen other side effects so maybe it can help someone else.

Version reference: rails 6.1.7.4 sprockets 4.2.0 sprockets-rails 3.4.2

In a file config/initializers/sprockets_missing_extension_error.rb we have

# This solves an issues where locally assets can be used without extensions without a warning while in production that fails
# See https://github.com/rails/rails/issues/42784 
# It should cause errors if you use `asset_path('foo') / image_tag('foo') / image_path('foo')`
# when it should be `foo.png`
# Doesn't run in prod just in case it has other edge cases.

unless Rails.env.production?
  module FailOnMissingExtensionForAssetPaths
    def resolve_asset_path(path, allow_non_precompiled = false)
      raise ArgumentError, "asset #{path} should be declared with a filename extension" if File.extname(path).blank?
      super
    end
  end

  Sprockets::Rails::Helper.prepend FailOnMissingExtensionForAssetPaths
end

danwakefield avatar Jan 10 '24 15:01 danwakefield