Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixes #2

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
118 changes: 58 additions & 60 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)
Expand All @@ -134,4 +132,4 @@ DEPENDENCIES
rspec

BUNDLED WITH
1.13.5
1.15.3
8 changes: 7 additions & 1 deletion lib/rails_critical_css_server/error_handler.rb
Original file line number Diff line number Diff line change
@@ -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

16 changes: 11 additions & 5 deletions lib/rails_critical_css_server/rewrite.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The way this is written you should be able to override the name of the manifest manually when you call this method. I call it like this:

= RailsCriticalCssServer::Rewrite.call(self,
  skip: current_user.present?,
  manifest: asset_url('css_manifest.css')) do

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

true. but then you have the manifest name twice in your template and it doesn't handle the generated hash in the filename.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The generated hash will be added to the filename, since the asset_url helper is smart enough to generate the correct digest and everything in a production environment.

This approach will also break because you may have more than one stylesheet in your HTML; for instance in one of my apps I have to split my css files up to avoid IE9 rule limit restrictions. So I have something like this:

= RailsCriticalCssServer::Rewrite.call(self,
  manifest: asset_url('css_manifest.css')) do
  = stylesheet_link_tag 'bootstrap'
  = stylesheet_link_tag 'common'
  = stylesheet_link_tag 'page_rules'

css_manifest.css in this case is a single stylesheet with all the rules, in an easy format that criticalcss can read.

def initialize(template, version: Config.version, skip: false, cache: true, &block)
@template = template
@manifest = manifest
@version = version
@skip = skip
@block = block
Expand All @@ -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)
Expand All @@ -48,13 +51,16 @@ 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
Expand All @@ -69,7 +75,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
Expand Down
2 changes: 1 addition & 1 deletion rails_critical_css_server.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -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