Sha256: 03ac381754066a9af7417ce9b465f2316a963dc348371a603a075c7224cbbcad

Contents?: true

Size: 1.96 KB

Versions: 5

Compression:

Stored size: 1.96 KB

Contents

module Middleman::Extensions
  module 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(root, build_dir), source)
            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 .jpeg .svg .svgz .js .gif).include?(File.extname(http_path))
          http_path
        else
          begin
            prefix = images_dir if prefix == http_images_path
          rescue
          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 sitemap.exists?(real_path_static)
            page = sitemap.page(real_path_static)
            if !page.template?
              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
  
  register :cache_buster, CacheBuster
end

Version data entries

5 entries across 5 versions & 2 rubygems

Version Path
middleman-more-3.0.0.alpha.7 lib/middleman-more/extensions/cache_buster.rb
middleman-3.0.0.alpha.6 lib/middleman/extensions/cache_buster.rb
middleman-3.0.0.alpha.5 lib/middleman/extensions/cache_buster.rb
middleman-3.0.0.alpha.4 lib/middleman/extensions/cache_buster.rb
middleman-3.0.0.alpha.3 lib/middleman/extensions/cache_buster.rb