Sha256: f7c38f2460d9d8eb10542b69d22d908c9ffe3a12e7b404b5015bbe94080de138

Contents?: true

Size: 1.81 KB

Versions: 1

Compression:

Stored size: 1.81 KB

Contents

module Middleman::Features::CacheBuster
  class << self
    def registered(app)
      app.send :include, InstanceMethods
      
      app.compass_config do |config|
        config.asset_cache_buster do |path, real_path|
          real_path = real_path.path if real_path.is_a? File
          real_path = real_path.gsub(File.join(self.root, self.build_dir), self.views)
          if File.readable?(real_path)
            File.mtime(real_path).strftime("%s") 
          else
            $stderr.puts "WARNING: '#{File.basename(path)}' was not found (or cannot be read) in #{File.dirname(real_path)}"
          end
        end
      end
    end
    alias :included :registered
  end
  
  module InstanceMethods
    def asset_url(path, prefix="")
      http_path = super

      if http_path.include?("://") || !%w(.css .png .jpg .js .gif).include?(File.extname(http_path))
        http_path
      else
        begin
          prefix = self.images_dir if prefix == self.http_images_path
        rescue
        end

        real_path_static = File.join(prefix, path)
        result = resolve_template(real_path_static)
        
        if self.build?
          real_path_dynamic = File.join(self.build_dir, prefix, path)
          real_path_dynamic = File.expand_path(real_path_dynamic, self.root)
          http_path << "?" + File.mtime(real_path_dynamic).strftime("%s") if File.readable?(real_path_dynamic)
        elsif result
          if result[1].nil?
            http_path << "?" + File.mtime(result[0]).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 durin
            # developement
            http_path << "?" + Time.now.strftime("%s")
          end
        end
        
        http_path
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
middleman-3.0.0.alpha.2 lib/middleman/features/cache_buster.rb