Skip to content

Rails 4 View Template Cache Digestor Compatibility #50

@eprothro

Description

@eprothro

Rails 4 pulls in cache digesting for fragment caching, which is fantastic. However, by default, only ERB templates are registered to be considered and parsed for cache key calculation.

As I would assume Haml support for cache digests wont get added to Rails, do you think it's appropriate to add the initializer code required to get that working into your gem?

@nzaillian suggested this to the haml gem in issue 708, for reference.

The following works for me in Rails 4 (a port of the code required to get this working in rails 3.x with the cache_digests gem and haml templates):

# config/initializers/cache_digests.rb

# Tell cache digests to parse haml view templates
# when calculating cache keys for view fragments
# as Rails only supports ERB by default

ActiveSupport.on_load(:action_view) do
  ActiveSupport.on_load(:after_initialize) do
    require 'action_view/dependency_tracker'
    ActionView::DependencyTracker.register_tracker :haml, ActionView::DependencyTracker::ERBTracker

    if Rails.env.development?
      # recalculate cache digest keys for each request in development

      # using blackhole cache until Rails path is released that allows us to
      # simply set `config.cache_template_loading` false in development.rb
      # https://github.com/rails/rails/pull/10791
      class BlackHole < Hash
        def []=(*); end
      end
      module ::ActionView
        class Digestor
          @@cache = BlackHole.new
        end
      end
    end
  end
end

Thoughts?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions