Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Merge branches 'public-root-fix' and 'serve-precached' into ag-work

  • Loading branch information...
commit e0d8b3034751ebb52507a53c03776cdbd7cbc291 2 parents 3314b73 + 6b6d3bf
@argent-smith authored
Showing with 23 additions and 4 deletions.
  1. +16 −3 lib/jammit/middleware.rb
  2. +7 −1 lib/jammit/sinatra.rb
View
19 lib/jammit/middleware.rb
@@ -34,6 +34,7 @@ def _call(env)
# The "package" action receives all requests for asset packages that haven't
# yet been cached. The package will be built, cached, and gzipped.
def package(package, extension)
+ @asset_file = File.join PUBLIC_ROOT, Jammit.package_path, "#{package}.#{extension}"
parse_request(package, extension)
template_ext = Jammit.template_extension.to_sym
result = []
@@ -45,7 +46,7 @@ def package(package, extension)
case @extension
when :js
- @contents = Jammit.packager.pack_javascripts(@package)
+ @contents = (should_compile? ? Jammit.packager.pack_javascripts(@package) : read_asset_file)
@contents = @contents.to_js if @contents.respond_to?(:to_js)
result = [
200,
@@ -55,7 +56,7 @@ def package(package, extension)
[@contents]
]
when template_ext
- @contents = Jammit.packager.pack_templates(@package)
+ @contents = (should_compile? ? Jammit.packager.pack_templates(@package) : read_asset_file)
@contents = @contents.to_js if @contents.respond_to?(:to_js)
[
200,
@@ -70,7 +71,7 @@ def package(package, extension)
headers.merge({
'Content-Type' => Rack::Mime.mime_type(".css")
}),
- [generate_stylesheets]
+ [(should_compile? ? generate_stylesheets : read_asset_file)]
]
end
rescue Jammit::PackageNotFound
@@ -111,5 +112,17 @@ def parse_request(pack, extension)
@package = pack.to_sym
end
+ # Borrowed from Jammit compressor internals.
+ def read_asset_file
+ puts "Reading precached #{@asset_file}"
+ File.open(@asset_file, 'rb:UTF-8') {|f| f.read }
+ end
+
+ # Presume that the ready-to-serve assets should not be recompiled in a
+ # non-development environment.
+ def should_compile?
+ (not File.exist?(@asset_file)) or Jammit.development?
+ end
+
end
end
View
8 lib/jammit/sinatra.rb
@@ -21,7 +21,13 @@ def stylesheet_link_tag(*sources)
# provides hook so you can run
# register Jammit in your
module Jammit
+
+ def self.development?
+ @@development
+ end
+
def self.registered(app)
+ @@development = app.development?
app.helpers Jammit::Helper
app.helpers Jammit::HelperOverrides
@@ -32,4 +38,4 @@ def self.registered(app)
app.before { Jammit.reload! }
end
end
-end
+end
Please sign in to comment.
Something went wrong with that request. Please try again.