diff --git a/activestorage/CHANGELOG.md b/activestorage/CHANGELOG.md index 11e4b98327129..17c0bba1320c5 100644 --- a/activestorage/CHANGELOG.md +++ b/activestorage/CHANGELOG.md @@ -1,3 +1,8 @@ +* Add `image/webp` to `config.active_storage.web_image_content_types` when `load_defaults "7.2"` + is set. + + *Lewis Buckley* + * Fix N+1 query when fetching preview images for non-image assets *Aaron Patterson & Justin Searls* diff --git a/activestorage/test/fixtures/files/valley.webp b/activestorage/test/fixtures/files/valley.webp new file mode 100644 index 0000000000000..122741b605f31 Binary files /dev/null and b/activestorage/test/fixtures/files/valley.webp differ diff --git a/activestorage/test/models/variant_test.rb b/activestorage/test/models/variant_test.rb index b7768f57ae387..a7aa3892a1f68 100644 --- a/activestorage/test/models/variant_test.rb +++ b/activestorage/test/models/variant_test.rb @@ -114,6 +114,17 @@ class ActiveStorage::VariantTest < ActiveSupport::TestCase assert_equal 8, image.height end + test "resized variation of WEBP blob" do + blob = create_file_blob(filename: "valley.webp") + variant = blob.variant(resize_to_limit: [50, 50]).processed + assert_match(/valley\.webp/, variant.url) + + image = read_image(variant) + assert_equal "WEBP", image.type + assert_equal 50, image.width + assert_equal 33, image.height + end + test "optimized variation of GIF blob" do blob = create_file_blob(filename: "image.gif", content_type: "image/gif") diff --git a/guides/source/configuring.md b/guides/source/configuring.md index 145bf2dc577a1..4e792dd3adb45 100644 --- a/guides/source/configuring.md +++ b/guides/source/configuring.md @@ -58,6 +58,10 @@ NOTE: If you need to apply configuration directly to a class, use a [lazy load h Below are the default values associated with each target version. In cases of conflicting values, newer versions take precedence over older versions. +#### Default Values for Target Version 7.2 + +- [`config.active_storage.web_image_content_types`](#config-active-storage-web-image-content-types): `%w[image/png image/jpeg image/gif image/webp]` + #### Default Values for Target Version 7.1 - [`config.action_dispatch.debug_exception_log_level`](#config-action-dispatch-debug-exception-log-level): `:error` @@ -2847,13 +2851,14 @@ config.active_storage.variable_content_types = %w(image/png image/gif image/jpeg Accepts an array of strings regarded as web image content types in which variants can be processed without being converted to the fallback PNG format. -If you want to use `WebP` or `AVIF` variants in your application you can add -`image/webp` or `image/avif` to this array. -By default, this is defined as: +For example, if you want to use `AVIF` variants in your application you can add +`image/avif` to this array. -```ruby -config.active_storage.web_image_content_types = %w(image/png image/jpeg image/gif) -``` +The default value depends on the `config.load_defaults` target version: +| Starting with version | The default value is | +| --------------------- | ----------------------------------------------- | +| (original) | `%w(image/png image/jpeg image/gif)` | +| 7.2 | `%w(image/png image/jpeg image/gif image/webp)` | #### `config.active_storage.content_types_to_serve_as_binary` diff --git a/railties/lib/rails/application/configuration.rb b/railties/lib/rails/application/configuration.rb index edcbabccf3f49..d123a2bab236d 100644 --- a/railties/lib/rails/application/configuration.rb +++ b/railties/lib/rails/application/configuration.rb @@ -323,6 +323,10 @@ def load_defaults(target_version) end when "7.2" load_defaults "7.1" + + if respond_to?(:active_storage) + active_storage.web_image_content_types = %w( image/png image/jpeg image/gif image/webp ) + end else raise "Unknown version #{target_version.to_s.inspect}" end diff --git a/railties/lib/rails/generators/rails/app/templates/config/initializers/new_framework_defaults_7_2.rb.tt b/railties/lib/rails/generators/rails/app/templates/config/initializers/new_framework_defaults_7_2.rb.tt index fcf2024ecf0ce..218eb7709f23c 100644 --- a/railties/lib/rails/generators/rails/app/templates/config/initializers/new_framework_defaults_7_2.rb.tt +++ b/railties/lib/rails/generators/rails/app/templates/config/initializers/new_framework_defaults_7_2.rb.tt @@ -8,3 +8,9 @@ # # Read the Guide for Upgrading Ruby on Rails for more info on each option. # https://guides.rubyonrails.org/upgrading_ruby_on_rails.html + +# Adds webp to the list of content types Active Storage considers as an image +# Prevents automatic conversion to a fallback PNG, and assumes clients support webp, as they support gif, jpeg, and png. +# This is possible due to broad browser support for webp, but older browsers and email clients may still not support +# webp. +# Rails.application.config.active_storage.web_image_content_types = %w[image/png image/jpeg image/gif image/webp]