Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

Using fewer for js now too :) #1

Open
wants to merge 5 commits into from

2 participants

@mloughran

But I'd have appreciated it not breaking on prod because java was not installed...

@benpickles

that would be Digest::MD5.hexdigest(read)

Yes - good point ;)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Oct 12, 2010
  1. @mloughran
  2. @mloughran

    Rescued minification errors by returning unminified source

    mloughran authored
    * Typically minification is only enabled in production
    * Should avoid breaking production at all costs
  3. @mloughran
  4. @mloughran
Commits on Oct 13, 2010
  1. @mloughran

    Switched eTag generation to more dependable MD5

    mloughran authored
    * File timestamps can be unreliable across multiple servers
This page is out of date. Refresh to see the latest.
View
4 README.md
@@ -11,9 +11,11 @@ Using Fewer in your Rails app is easy, just initialize your Fewer apps and add t
gem 'closure-compiler', :group => :production
# config/initializers/fewer.rb
+ require 'fewer'
+
Fewer::App.new(:javascripts,
:engine => Fewer::Engines::Js,
- :engine_options => { :min => Rails.env.production? }
+ :engine_options => { :min => Rails.env.production? },
:root => Rails.root.join('app', 'javascripts')
)
Fewer::App.new(:stylesheets,
View
17 lib/fewer.rb
@@ -1,3 +1,20 @@
+module Fewer
+ class << self
+ attr_writer :logger
+
+ def logger
+ @logger ||= begin
+ defined?(Rails) ? Rails.logger : begin
+ require 'logger'
+ log = Logger.new(STDOUT)
+ log.level = Logger::INFO
+ log
+ end
+ end
+ end
+ end
+end
+
require 'fewer/app'
require 'fewer/engines'
require 'fewer/errors'
View
19 lib/fewer/app.rb
@@ -25,13 +25,20 @@ def initialize(name, options = {})
def call(env)
eng = engine(names_from_path(env['PATH_INFO']))
- headers = {
- 'Content-Type' => eng.content_type,
- 'Cache-Control' => "public, max-age=#{cache}",
- 'Last-Modified' => eng.mtime.rfc2822
- }
- [200, headers, [eng.read]]
+ if env["HTTP_IF_NONE_MATCH"] && env["HTTP_IF_NONE_MATCH"] == eng.etag
+ Fewer.logger.debug "Fewer: returning 304 not modified"
+ [304, {}, []]
+ else
+ headers = {
+ 'Content-Type' => eng.content_type,
+ 'Cache-Control' => "public, max-age=#{cache}",
+ 'Last-Modified' => eng.mtime.rfc2822,
+ 'ETag' => eng.etag
+ }
+
+ [200, headers, [eng.read]]
+ end
rescue Fewer::MissingSourceFileError => e
[404, { 'Content-Type' => 'text/plain' }, [e.message]]
rescue => e
View
7 lib/fewer/engines/abstract.rb
@@ -1,3 +1,5 @@
+require 'digest/md5'
+
module Fewer
module Engines
class Abstract
@@ -30,6 +32,11 @@ def mtime
}.max
end
+ def etag
+ # MD5 for concatenation of all files
+ Digest::MD5.hexdigest(paths.map { |path| File.read(path) }.join)
+ end
+
def paths
@paths ||= names.map { |name|
File.join(root, "#{name}#{extension}")
View
11 lib/fewer/engines/js.rb
@@ -13,7 +13,16 @@ def extension
def read
if options[:min]
- ::Closure::Compiler.new.compress(super)
+ begin
+ ::Closure::Compiler.new.compress(super)
+ rescue LoadError
+ Fewer.logger.error "Javascript minification not available since the closure-compiler gem is not installed."
+ super
+ rescue StandardError => e
+ # Unfortunately errors do not all descend from Closure::Error
+ Fewer.logger.error "Exception raised in closure-compiler gem: #{e.class} (#{e.message}). Javascript returned without minification."
+ super
+ end
else
super
end
Something went wrong with that request. Please try again.