Sha256: d95b1e7c4856023a064413473479f79a30df4f76c47ffbf97c5e1a719a7b1b35

Contents?: true

Size: 1.83 KB

Versions: 66

Compression:

Stored size: 1.83 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

      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

66 entries across 62 versions & 6 rubygems

Version Path
actionpack-7.0.8.7 lib/action_controller/metal/etag_with_template_digest.rb
actionpack-7.0.8.6 lib/action_controller/metal/etag_with_template_digest.rb
actionpack-6.1.7.10 lib/action_controller/metal/etag_with_template_digest.rb
actionpack-6.1.7.9 lib/action_controller/metal/etag_with_template_digest.rb
actionpack-7.0.8.5 lib/action_controller/metal/etag_with_template_digest.rb
blacklight-spotlight-3.6.0.beta8 vendor/bundle/ruby/3.2.0/gems/actionpack-7.0.8.4/lib/action_controller/metal/etag_with_template_digest.rb
cm-admin-1.5.22 vendor/bundle/ruby/3.3.0/gems/actionpack-7.0.5.1/lib/action_controller/metal/etag_with_template_digest.rb
cm-admin-1.5.21 vendor/bundle/ruby/3.3.0/gems/actionpack-7.0.5.1/lib/action_controller/metal/etag_with_template_digest.rb
cm-admin-1.5.20 vendor/bundle/ruby/3.3.0/gems/actionpack-7.0.5.1/lib/action_controller/metal/etag_with_template_digest.rb
actionpack-7.0.8.4 lib/action_controller/metal/etag_with_template_digest.rb
actionpack-6.1.7.8 lib/action_controller/metal/etag_with_template_digest.rb
actionpack-7.0.8.1 lib/action_controller/metal/etag_with_template_digest.rb
actionpack-6.1.7.7 lib/action_controller/metal/etag_with_template_digest.rb
scrapbook-0.3.2 vendor/ruby/2.7.0/gems/actionpack-7.0.3.1/lib/action_controller/metal/etag_with_template_digest.rb
scrapbook-0.3.2 vendor/ruby/2.7.0/gems/actionpack-6.1.6.1/lib/action_controller/metal/etag_with_template_digest.rb
scrapbook-0.3.2 vendor/ruby/2.7.0/gems/actionpack-7.0.2.3/lib/action_controller/metal/etag_with_template_digest.rb
actionpack-7.0.8 lib/action_controller/metal/etag_with_template_digest.rb
actionpack-7.0.7.2 lib/action_controller/metal/etag_with_template_digest.rb
actionpack-6.1.7.6 lib/action_controller/metal/etag_with_template_digest.rb
actionpack-7.0.7.1 lib/action_controller/metal/etag_with_template_digest.rb