From 72ba9e52dcf0293c3ebd0b6b5d45e1275a3bbe3d Mon Sep 17 00:00:00 2001 From: Josh Sharpe Date: Fri, 11 Aug 2017 10:15:06 -0400 Subject: [PATCH 1/4] ease dependency on httparty --- Gemfile.lock | 118 +++++++++++++++--------------- rails_critical_css_server.gemspec | 2 +- 2 files changed, 59 insertions(+), 61 deletions(-) diff --git a/Gemfile.lock b/Gemfile.lock index c1dc090..c020b76 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -2,98 +2,96 @@ PATH remote: . specs: rails_critical_css_server (0.1.3) - httparty (~> 0.13.3) + httparty (>= 0.13.3) rails (>= 3.0) GEM remote: https://rubygems.org/ specs: - actioncable (5.0.1) - actionpack (= 5.0.1) - nio4r (~> 1.2) + actioncable (5.1.3) + actionpack (= 5.1.3) + nio4r (~> 2.0) websocket-driver (~> 0.6.1) - actionmailer (5.0.1) - actionpack (= 5.0.1) - actionview (= 5.0.1) - activejob (= 5.0.1) + actionmailer (5.1.3) + actionpack (= 5.1.3) + actionview (= 5.1.3) + activejob (= 5.1.3) mail (~> 2.5, >= 2.5.4) rails-dom-testing (~> 2.0) - actionpack (5.0.1) - actionview (= 5.0.1) - activesupport (= 5.0.1) + actionpack (5.1.3) + actionview (= 5.1.3) + activesupport (= 5.1.3) rack (~> 2.0) rack-test (~> 0.6.3) rails-dom-testing (~> 2.0) rails-html-sanitizer (~> 1.0, >= 1.0.2) - actionview (5.0.1) - activesupport (= 5.0.1) + actionview (5.1.3) + activesupport (= 5.1.3) builder (~> 3.1) - erubis (~> 2.7.0) + erubi (~> 1.4) rails-dom-testing (~> 2.0) - rails-html-sanitizer (~> 1.0, >= 1.0.2) - activejob (5.0.1) - activesupport (= 5.0.1) + rails-html-sanitizer (~> 1.0, >= 1.0.3) + activejob (5.1.3) + activesupport (= 5.1.3) globalid (>= 0.3.6) - activemodel (5.0.1) - activesupport (= 5.0.1) - activerecord (5.0.1) - activemodel (= 5.0.1) - activesupport (= 5.0.1) - arel (~> 7.0) - activesupport (5.0.1) + activemodel (5.1.3) + activesupport (= 5.1.3) + activerecord (5.1.3) + activemodel (= 5.1.3) + activesupport (= 5.1.3) + arel (~> 8.0) + activesupport (5.1.3) concurrent-ruby (~> 1.0, >= 1.0.2) i18n (~> 0.7) minitest (~> 5.1) tzinfo (~> 1.1) - arel (7.1.4) + arel (8.0.0) builder (3.2.3) - concurrent-ruby (1.0.4) + concurrent-ruby (1.0.5) diff-lcs (1.2.5) - erubis (2.7.0) - globalid (0.3.7) - activesupport (>= 4.1.0) - httparty (0.13.7) - json (~> 1.8) + erubi (1.6.1) + globalid (0.4.0) + activesupport (>= 4.2.0) + httparty (0.15.6) multi_xml (>= 0.5.2) - i18n (0.8.0) - json (1.8.6) + i18n (0.8.6) loofah (2.0.3) nokogiri (>= 1.5.9) - mail (2.6.4) + mail (2.6.6) mime-types (>= 1.16, < 4) method_source (0.8.2) mime-types (3.1) mime-types-data (~> 3.2015) mime-types-data (3.2016.0521) - mini_portile2 (2.1.0) - minitest (5.10.1) + mini_portile2 (2.2.0) + minitest (5.10.3) multi_xml (0.6.0) - nio4r (1.2.1) - nokogiri (1.7.0.1) - mini_portile2 (~> 2.1.0) - rack (2.0.1) + nio4r (2.1.0) + nokogiri (1.8.0) + mini_portile2 (~> 2.2.0) + rack (2.0.3) rack-test (0.6.3) rack (>= 1.0) - rails (5.0.1) - actioncable (= 5.0.1) - actionmailer (= 5.0.1) - actionpack (= 5.0.1) - actionview (= 5.0.1) - activejob (= 5.0.1) - activemodel (= 5.0.1) - activerecord (= 5.0.1) - activesupport (= 5.0.1) - bundler (>= 1.3.0, < 2.0) - railties (= 5.0.1) + rails (5.1.3) + actioncable (= 5.1.3) + actionmailer (= 5.1.3) + actionpack (= 5.1.3) + actionview (= 5.1.3) + activejob (= 5.1.3) + activemodel (= 5.1.3) + activerecord (= 5.1.3) + activesupport (= 5.1.3) + bundler (>= 1.3.0) + railties (= 5.1.3) sprockets-rails (>= 2.0.0) - rails-dom-testing (2.0.2) - activesupport (>= 4.2.0, < 6.0) - nokogiri (~> 1.6) + rails-dom-testing (2.0.3) + activesupport (>= 4.2.0) + nokogiri (>= 1.6) rails-html-sanitizer (1.0.3) loofah (~> 2.0) - railties (5.0.1) - actionpack (= 5.0.1) - activesupport (= 5.0.1) + railties (5.1.3) + actionpack (= 5.1.3) + activesupport (= 5.1.3) method_source rake (>= 0.8.7) thor (>= 0.18.1, < 2.0) @@ -119,8 +117,8 @@ GEM activesupport (>= 4.0) sprockets (>= 3.0.0) thor (0.19.4) - thread_safe (0.3.5) - tzinfo (1.2.2) + thread_safe (0.3.6) + tzinfo (1.2.3) thread_safe (~> 0.1) websocket-driver (0.6.5) websocket-extensions (>= 0.1.0) @@ -134,4 +132,4 @@ DEPENDENCIES rspec BUNDLED WITH - 1.13.5 + 1.15.3 diff --git a/rails_critical_css_server.gemspec b/rails_critical_css_server.gemspec index 11fe504..9a0e942 100644 --- a/rails_critical_css_server.gemspec +++ b/rails_critical_css_server.gemspec @@ -17,7 +17,7 @@ Gem::Specification.new do |s| s.files = Dir["{app,config,db,lib}/**/*", "MIT-LICENSE", "Rakefile", "README.md"] s.add_dependency "rails", ">= 3.0" - s.add_dependency "httparty", "~> 0.13.3" + s.add_dependency "httparty", ">= 0.13.3" s.add_development_dependency('rspec') end From 9b1b942d131a9ea8099b4b13d60bc9ffcf6a1bf4 Mon Sep 17 00:00:00 2001 From: Josh Sharpe Date: Tue, 15 Aug 2017 10:39:45 -0400 Subject: [PATCH 2/4] send extracted css paths to node server --- lib/rails_critical_css_server/rewrite.rb | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/lib/rails_critical_css_server/rewrite.rb b/lib/rails_critical_css_server/rewrite.rb index 7ddc709..ab4fcb0 100644 --- a/lib/rails_critical_css_server/rewrite.rb +++ b/lib/rails_critical_css_server/rewrite.rb @@ -48,13 +48,15 @@ def original_html @original_html = template.capture { block.call } end + def extracted_css_files + @extracted_css_files ||= ExtractCssFromHtml.call(original_html) + end def rewritten_html return @rewritten_html if defined? @rewritten_html - css_files = ExtractCssFromHtml.call(original_html) @rewritten_html = template.capture do template.render 'rails_critical_css_server/css_load', - css_files: css_files, + css_files: extracted_css_files, critical_css: content, original_html: original_html end @@ -69,7 +71,7 @@ def phantomjs_useragent? end def client - @client ||= Client.new(key, request.original_url, manifest) + @client ||= Client.new(key, request.original_url, extracted_css_files.first) end def key From 99a4cd4fdda760382b5feac767e4cc22262ea4d4 Mon Sep 17 00:00:00 2001 From: Josh Sharpe Date: Tue, 15 Aug 2017 11:36:20 -0400 Subject: [PATCH 3/4] fix ErrorHandler to properly use Callable --- lib/rails_critical_css_server/error_handler.rb | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/lib/rails_critical_css_server/error_handler.rb b/lib/rails_critical_css_server/error_handler.rb index 08ac461..50d8a62 100644 --- a/lib/rails_critical_css_server/error_handler.rb +++ b/lib/rails_critical_css_server/error_handler.rb @@ -1,9 +1,15 @@ module RailsCriticalCssServer class ErrorHandler extend Callable + attr_reader :error - def call(error) + def initialize(error) + @error = error + end + + def call Rails.logger.error error end end end + From f894a7006a878f6412514164b5cd5a94ed3c2710 Mon Sep 17 00:00:00 2001 From: Josh Sharpe Date: Tue, 15 Aug 2017 12:14:25 -0400 Subject: [PATCH 4/4] remove manifest arg --- lib/rails_critical_css_server/rewrite.rb | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/lib/rails_critical_css_server/rewrite.rb b/lib/rails_critical_css_server/rewrite.rb index ab4fcb0..51c79e8 100644 --- a/lib/rails_critical_css_server/rewrite.rb +++ b/lib/rails_critical_css_server/rewrite.rb @@ -6,9 +6,8 @@ class Rewrite delegate :request, :params, to: :template - def initialize(template, manifest: template.asset_url('application.css'), version: Config.version, skip: false, cache: true, &block) + def initialize(template, version: Config.version, skip: false, cache: true, &block) @template = template - @manifest = manifest @version = version @skip = skip @block = block @@ -30,6 +29,10 @@ def cache_key private + def manifest + extracted_css_files.first + end + def cached_value return @cached_value if defined? @cached_value @cached_value = Rails.cache.read(self) @@ -51,6 +54,7 @@ def original_html def extracted_css_files @extracted_css_files ||= ExtractCssFromHtml.call(original_html) end + def rewritten_html return @rewritten_html if defined? @rewritten_html