Module: Middleman::Extensions::CacheBuster::InstanceMethods

Defined in:
lib/middleman-more/extensions/cache_buster.rb

Overview

Cache buster instance methods

Instance Method Summary collapse

Instance Method Details

#asset_url(path, prefix = "") ⇒ Object

asset_url override if we’re using cache busting

Parameters:

  • path (String)
  • prefix (String) (defaults to: "")


38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/middleman-more/extensions/cache_buster.rb', line 38

def asset_url(path, prefix="")
  http_path = super

  if http_path.include?("://") || !%w(.css .png .jpg .jpeg .svg .svgz .js .gif).include?(File.extname(http_path))
    http_path
  else
    if respond_to?(:http_images_path) && prefix == http_images_path
      prefix = images_dir
    end

    real_path_static = File.join(prefix, path)

    if build?
      real_path_dynamic = File.join(build_dir, prefix, path)
      real_path_dynamic = File.expand_path(real_path_dynamic, root)
      http_path << "?" + File.mtime(real_path_dynamic).strftime("%s") if File.readable?(real_path_dynamic)
    elsif resource = sitemap.find_resource_by_path(real_path_static)
      if !resource.template?
        http_path << "?" + File.mtime(resource.source_file).strftime("%s")
      else
        # It's a template, possible with partials. We can't really
        # know when it's updated, so generate fresh cache buster every
        # time during developement
        http_path << "?" + Time.now.strftime("%s")
      end
    end

    http_path
  end
end