fix cache_buster (include ancient bug

This commit is contained in:
Thomas Reynolds 2011-11-18 17:03:09 -08:00
parent 186651c5b5
commit 931253469d
6 changed files with 96 additions and 83 deletions

View file

@ -23,4 +23,4 @@ Feature: Generate mtime-based query string for busting browser caches
Given "cache_buster" feature is "enabled"
And the Server is running at "test-app"
When I go to "/cache-buster.html"
Then I should not see "?"
Then I should see "?"

View file

@ -27,7 +27,8 @@ class Middleman::Base
end
def to_rack_app(&block)
app.run inst(&block)
inner_app = inst(&block)
app.map("/") { run inner_app }
app
end
@ -44,7 +45,7 @@ class Middleman::Base
end
def map(map, &block)
# app.map(map, &block)
app.map(map, &block)
end
def helpers(*extensions, &block)
@ -61,6 +62,8 @@ class Middleman::Base
@defaults[key] = value
end
def asset_stamp; false; end
def before_processing(name=:unnamed, idx=-1, &block)
@before_processes ||= []
@before_processes.insert(idx, [name, block])
@ -312,6 +315,10 @@ public
raw_templates_cache[path]
end
def map(map, &block)
self.class.map(map, &block)
end
# def compiled_templates_cache
# @_compiled_templates_cache ||= {}
# end

View file

@ -2,19 +2,12 @@ require 'pathname'
require 'rbconfig'
require "sprockets"
class RackThingy
def call(env)
$stderr.puts "thingy"
[200, {}, "Sup"]
end
end
module Middleman::CoreExtensions::Sprockets
class << self
def registered(app)
app.set :js_compressor, false
app.set :css_compressor, false
return
# Cut off every extension after .js (which sprockets eats up)
app.build_reroute do |destination, request_path|
if !request_path.match(/\.js\./i)
@ -27,49 +20,48 @@ module Middleman::CoreExtensions::Sprockets
end
end
# app.after_configuration do
# js_env = Middleman::CoreExtensions::Sprockets::JavascriptEnvironment.new(self)
#
# vendor_dir = File.join("vendor", "assets", "javascripts")
# gems_with_js = ::Middleman.rubygems_latest_specs.select do |spec|
# ::Middleman.spec_has_file?(spec, vendor_dir)
# end.each do |spec|
# js_env.append_path File.join(spec.full_gem_path, vendor_dir)
# end
#
# app_dir = File.join("app", "assets", "javascripts")
# gems_with_js = ::Middleman.rubygems_latest_specs.select do |spec|
# ::Middleman.spec_has_file?(spec, app_dir)
# end.each do |spec|
# js_env.append_path File.join(spec.full_gem_path, app_dir)
# end
#
# # add paths to js_env (vendor/assets/javascripts)
# app.map "/#{self.js_dir}" do
# run js_env
# end
# end
app.after_configuration do
js_env = Middleman::CoreExtensions::Sprockets::JavascriptEnvironment.new(self)
vendor_dir = File.join("vendor", "assets", "javascripts")
gems_with_js = ::Middleman.rubygems_latest_specs.select do |spec|
::Middleman.spec_has_file?(spec, vendor_dir)
end.each do |spec|
js_env.append_path File.join(spec.full_gem_path, vendor_dir)
end
app_dir = File.join("app", "assets", "javascripts")
gems_with_js = ::Middleman.rubygems_latest_specs.select do |spec|
::Middleman.spec_has_file?(spec, app_dir)
end.each do |spec|
js_env.append_path File.join(spec.full_gem_path, app_dir)
end
# add paths to js_env (vendor/assets/javascripts)
app.map "/#{self.js_dir}" do
run js_env
end
end
app.after_compass_config do
# css_env = Middleman::CoreExtensions::Sprockets::StylesheetEnvironment.new(self)
#
# vendor_dir = File.join("vendor", "assets", "stylesheets")
# gems_with_css = ::Middleman.rubygems_latest_specs.select do |spec|
# ::Middleman.spec_has_file?(spec, vendor_dir)
# end.each do |spec|
# css_env.append_path File.join(spec.full_gem_path, vendor_dir)
# end
#
# app_dir = File.join("app", "assets", "stylesheets")
# gems_with_css = ::Middleman.rubygems_latest_specs.select do |spec|
# ::Middleman.spec_has_file?(spec, app_dir)
# end.each do |spec|
# css_env.append_path File.join(spec.full_gem_path, app_dir)
# end
css_env = Middleman::CoreExtensions::Sprockets::StylesheetEnvironment.new(self)
app.map "/#{self.css_dir}" do
# run css_env
run ::RackThingy.new
vendor_dir = File.join("vendor", "assets", "stylesheets")
gems_with_css = ::Middleman.rubygems_latest_specs.select do |spec|
::Middleman.spec_has_file?(spec, vendor_dir)
end.each do |spec|
css_env.append_path File.join(spec.full_gem_path, vendor_dir)
end
app_dir = File.join("app", "assets", "stylesheets")
gems_with_css = ::Middleman.rubygems_latest_specs.select do |spec|
::Middleman.spec_has_file?(spec, app_dir)
end.each do |spec|
css_env.append_path File.join(spec.full_gem_path, app_dir)
end
map("/#{self.css_dir}") do
run css_env
end
end
end

View file

@ -1,34 +1,12 @@
module Middleman::Features::CacheBuster
class << self
def registered(app)
app.register_asset_handler :cache_buster do |path, prefix|
http_path = app.before_asset_handler(:cache_buster, path, prefix)
if http_path.include?("://") || !%w(.css .png .jpg .js .gif).include?(File.extname(http_path))
http_path
else
begin
prefix = app.images_dir if prefix == app.http_images_path
rescue
end
real_path_static = File.join(app.views, prefix, path)
if File.readable?(real_path_static)
http_path << "?" + File.mtime(real_path_static).strftime("%s")
elsif app.build?
real_path_dynamic = File.join(app.root, app.build_dir, prefix, path)
http_path << "?" + File.mtime(real_path_dynamic).strftime("%s") if File.readable?(real_path_dynamic)
end
http_path
end
end
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(app.root, app.build_dir), app.views)
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
@ -39,4 +17,39 @@ module Middleman::Features::CacheBuster
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

View file

@ -77,16 +77,17 @@ module Guard
end
def server_start
app = ::Middleman.server
# Quiet down Guard
# ENV['GUARD_ENV'] = 'test' if @options[:debug] == "true"
@app = app.new
@app.set :environment, (@options[:environment] || "development").to_sym
@app.set :logging, @options.has_key?(:debug) && (@options[:debug] == "true")
env = (@options[:environment] || "development").to_sym
is_logging = @options.has_key?(:debug) && (@options[:debug] == "true")
@app = ::Middleman.server.inst do
set :environment, env
set :logging, is_logging
end
app_rack = ::Middleman::Base.app
app_rack.run @app
app_rack = @app.class.to_rack_app
@server_job = fork do
opts = @options.dup

View file

@ -33,7 +33,7 @@ Gem::Specification.new do |s|
s.add_dependency("coffee-script", ["~> 2.2.0"])
s.add_dependency("execjs", ["~> 1.2.7"])
s.add_dependency("sprockets", ["~> 2.0"])
s.add_dependency("sprockets-sass", ["~> 0.4.0"])
s.add_dependency("sprockets-sass", ["~> 0.3.0"])
s.add_dependency("hooks", ["~> 0.2.0"])
s.add_dependency("rb-fsevent")
s.add_dependency("guard", ["~> 0.8.8"])