Class: Middleman::Extensions::CacheBuster

Inherits:
Middleman::Extension show all
Defined in:
middleman-core/lib/middleman-core/extensions/cache_buster.rb

Overview

The Cache Buster extension

Constant Summary

Constants included from Contracts

Contracts::PATH_MATCHER

Instance Attribute Summary

Attributes inherited from Middleman::Extension

#app, #options

Instance Method Summary collapse

Methods inherited from Middleman::Extension

activated_extension, #add_exposed_to_context, #after_build, #after_configuration, after_extension_activated, #after_extension_activated, #before_build, #before_configuration, clear_after_extension_callbacks, config, define_setting, expose_to_application, expose_to_config, expose_to_template, global_config, helpers, #manipulate_resource_list, option, #ready, resources

Methods included from Contracts

#Contract

Constructor Details

#initialize(app, options_hash = ::Middleman::EMPTY_HASH, &block) ⇒ CacheBuster

Returns a new instance of CacheBuster.


10
11
12
13
14
15
16
# File 'middleman-core/lib/middleman-core/extensions/cache_buster.rb', line 10

def initialize(app, options_hash = ::Middleman::EMPTY_HASH, &block)
  super

  require 'set'
  @set_of_exts = Set.new(options.exts || app.config[:asset_extensions])
  @set_of_sources = Set.new options.sources
end

Instance Method Details

#manipulate_resource_list_container!(resource_list) ⇒ Object


19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'middleman-core/lib/middleman-core/extensions/cache_buster.rb', line 19

def manipulate_resource_list_container!(resource_list)
  resource_list.by_extensions(@set_of_sources).each do |r|
    next if Array(options.rewrite_ignore || []).any? do |i|
      ::Middleman::Util.path_match(i, "/#{r.destination_path}")
    end

    r.add_filter ::Middleman::InlineURLRewriter.new(:cache_buster,
                                                    app,
                                                    r,
                                                    url_extensions: @set_of_exts,
                                                    ignore: options.ignore,
                                                    proc: method(:rewrite_url))
  end
end

#rewrite_url(asset_path, _dirpath, _request_path) ⇒ Object


35
36
37
# File 'middleman-core/lib/middleman-core/extensions/cache_buster.rb', line 35

def rewrite_url(asset_path, _dirpath, _request_path)
  "#{asset_path}?#{Time.now.strftime('%s')}"
end