Sha256: b8cd1f5e9181f5f4124dbf08642566347240b2594e9a4c44bb4ec0e344bbc405

Contents?: true

Size: 1.92 KB

Versions: 24

Compression:

Stored size: 1.92 KB

Contents

# frozen_string_literal: true

# :markup: markdown

module ActionController
  # # Action Controller Etag With Template Digest
  #
  # When our views change, they should bubble up into HTTP cache freshness and
  # bust browser caches. So the template digest for the current action is
  # automatically included in the ETag.
  #
  # Enabled by default for apps that use Action View. Disable by setting
  #
  #     config.action_controller.etag_with_template_digest = false
  #
  # Override the template to digest by passing `:template` to `fresh_when` and
  # `stale?` calls. For example:
  #
  #     # We're going to render widgets/show, not posts/show
  #     fresh_when @post, template: 'widgets/show'
  #
  #     # We're not going to render a template, so omit it from the ETag.
  #     fresh_when @post, template: false
  #
  module EtagWithTemplateDigest
    extend ActiveSupport::Concern

    include ActionController::ConditionalGet

    included do
      class_attribute :etag_with_template_digest, default: true

      etag do |options|
        determine_template_etag(options) if etag_with_template_digest
      end
    end

    private
      def determine_template_etag(options)
        if template = pick_template_for_etag(options)
          lookup_and_digest_template(template)
        end
      end

      # Pick the template digest to include in the ETag. If the `:template` option is
      # present, use the named template. If `:template` is `nil` or absent, use the
      # default controller/action template. If `:template` is false, omit the template
      # digest from the ETag.
      def pick_template_for_etag(options)
        unless options[:template] == false
          options[:template] || lookup_context.find_all(action_name, _prefixes).first&.virtual_path
        end
      end

      def lookup_and_digest_template(template)
        ActionView::Digestor.digest name: template, format: nil, finder: lookup_context
      end
  end
end

Version data entries

24 entries across 24 versions & 2 rubygems

Version Path
actionpack-8.0.2 lib/action_controller/metal/etag_with_template_digest.rb
actionpack-8.0.1 lib/action_controller/metal/etag_with_template_digest.rb
actionpack-8.0.0.1 lib/action_controller/metal/etag_with_template_digest.rb
actionpack-7.2.2.1 lib/action_controller/metal/etag_with_template_digest.rb
actionpack-8.0.0 lib/action_controller/metal/etag_with_template_digest.rb
actionpack-7.2.2 lib/action_controller/metal/etag_with_template_digest.rb
actionpack-8.0.0.rc2 lib/action_controller/metal/etag_with_template_digest.rb
actionpack-7.2.1.2 lib/action_controller/metal/etag_with_template_digest.rb
actionpack-8.0.0.rc1 lib/action_controller/metal/etag_with_template_digest.rb
actionpack-7.2.1.1 lib/action_controller/metal/etag_with_template_digest.rb
actionpack-8.0.0.beta1 lib/action_controller/metal/etag_with_template_digest.rb
omg-actionpack-8.0.0.alpha9 lib/action_controller/metal/etag_with_template_digest.rb
omg-actionpack-8.0.0.alpha8 lib/action_controller/metal/etag_with_template_digest.rb
omg-actionpack-8.0.0.alpha7 lib/action_controller/metal/etag_with_template_digest.rb
omg-actionpack-8.0.0.alpha4 lib/action_controller/metal/etag_with_template_digest.rb
omg-actionpack-8.0.0.alpha3 lib/action_controller/metal/etag_with_template_digest.rb
omg-actionpack-8.0.0.alpha2 lib/action_controller/metal/etag_with_template_digest.rb
omg-actionpack-8.0.0.alpha1 lib/action_controller/metal/etag_with_template_digest.rb
actionpack-7.2.1 lib/action_controller/metal/etag_with_template_digest.rb
actionpack-7.2.0 lib/action_controller/metal/etag_with_template_digest.rb