Sha256: 9083b8da4a69f4becc693a06ff0f9287d79c6b712d917bf67acd430833b89199

Contents?: true

Size: 1.86 KB

Versions: 91

Compression:

Stored size: 1.86 KB

Contents

# frozen_string_literal: true

module ActionController
  # 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

      ActiveSupport.on_load :action_view, yield: true do
        etag do |options|
          determine_template_etag(options) if etag_with_template_digest
        end
      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] || "#{controller_path}/#{action_name}"
        end
      end

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

Version data entries

91 entries across 91 versions & 5 rubygems

Version Path
actionpack-5.2.8.1 lib/action_controller/metal/etag_with_template_digest.rb
actionpack-5.2.8 lib/action_controller/metal/etag_with_template_digest.rb
actionpack-5.2.7.1 lib/action_controller/metal/etag_with_template_digest.rb
actionpack-5.2.7 lib/action_controller/metal/etag_with_template_digest.rb
actionpack-5.2.6.3 lib/action_controller/metal/etag_with_template_digest.rb
actionpack-5.2.6.2 lib/action_controller/metal/etag_with_template_digest.rb
actionpack-5.2.6.1 lib/action_controller/metal/etag_with_template_digest.rb
actionpack-5.2.6 lib/action_controller/metal/etag_with_template_digest.rb
actionpack-5.2.4.6 lib/action_controller/metal/etag_with_template_digest.rb
actionpack-5.2.5 lib/action_controller/metal/etag_with_template_digest.rb
actionpack-5.2.4.5 lib/action_controller/metal/etag_with_template_digest.rb
actionpack-5.2.4.4 lib/action_controller/metal/etag_with_template_digest.rb
actionpack-5.2.4.3 lib/action_controller/metal/etag_with_template_digest.rb
actionpack-5.2.4.2 lib/action_controller/metal/etag_with_template_digest.rb
actionpack-5.2.4.1 lib/action_controller/metal/etag_with_template_digest.rb
zuora_connect_ui-0.10.0 vendor/ruby/2.6.0/gems/actionpack-5.2.3/lib/action_controller/metal/etag_with_template_digest.rb
actionpack-5.2.4 lib/action_controller/metal/etag_with_template_digest.rb
actionpack-5.2.4.rc1 lib/action_controller/metal/etag_with_template_digest.rb
zuora_connect_ui-0.9.2 vendor/ruby/2.6.0/gems/actionpack-5.2.3/lib/action_controller/metal/etag_with_template_digest.rb
zuora_connect_ui-0.9.1 vendor/ruby/2.6.0/gems/actionpack-5.2.3/lib/action_controller/metal/etag_with_template_digest.rb