Skip to content

Commit e50eb37

Browse files
committed
Add a penthouse wrapper script
1 parent 27cdd02 commit e50eb37

File tree

4 files changed

+60
-15
lines changed

4 files changed

+60
-15
lines changed

.codeclimate.yml

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,2 @@
11
languages:
22
Ruby: true
3-
exclude_paths:
4-
- lib/penthouse/penthouse.js

critical-path-css-rails.gemspec

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,6 @@ Gem::Specification.new do |s|
1010
s.description = 'Only load the CSS you need for the initial viewport in Rails!'
1111
s.license = 'MIT'
1212

13-
s.add_runtime_dependency 'phantomjs', ['~> 2.1']
14-
1513
s.files = `git ls-files`.split("\n")
1614
s.executables = `git ls-files -- bin/*`.split("\n").map { |f| File.basename(f) }
1715
s.require_path = 'lib'

lib/critical_path_css/css_fetcher.rb

Lines changed: 20 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
1+
require 'open3'
2+
13
module CriticalPathCss
24
class CssFetcher
3-
require 'phantomjs'
45
require 'critical_path_css/configuration'
56

6-
PENTHOUSE_PATH = "#{File.dirname(__FILE__)}/../penthouse/penthouse.js"
7+
JS_FETCHER_PATH = File.expand_path('../fetch-css.js',
8+
File.dirname(__FILE__)).freeze
79

810
def initialize
911
@config = Configuration.new
@@ -20,15 +22,22 @@ def fetch_route(route)
2022
protected
2123

2224
def css_for_route(route)
23-
url = @config.base_url + route
24-
25-
Phantomjs.run(
26-
'--ignore-ssl-errors=true',
27-
'--ssl-protocol=tlsv1',
28-
PENTHOUSE_PATH,
29-
url,
30-
@config.css_path
31-
)
25+
options = {
26+
url: @config.base_url + route,
27+
cssPath: @config.css_path,
28+
phantomJsOptions: {
29+
:'ignore-ssl-errors' => true,
30+
:'ssl-protocol' => 'tlsv1'
31+
}
32+
}
33+
out, err, st = Open3.capture3('node', JS_FETCHER_PATH, JSON.dump(options))
34+
unless st.exitstatus.zero?
35+
STDOUT.puts out
36+
STDERR.puts err
37+
raise "Failed to get CSS for route #{route}\n" \
38+
" with options=#{options.inspect}"
39+
end
40+
out
3241
end
3342
end
3443
end

lib/fetch-css.js

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
const penthouse = require('penthouse');
2+
const fs = require('fs');
3+
4+
const argOptions = JSON.parse(process.argv[2]);
5+
const penthouseOptions = Object.assign({
6+
url: null, // required
7+
css: null, // required
8+
// OPTIONAL params
9+
width: 1300, // viewport width
10+
height: 900, // viewport height
11+
forceInclude: [ // CSS selectors to always include, e.g.:
12+
// '.keepMeEvenIfNotSeenInDom',
13+
// /^\.regexWorksToo/
14+
],
15+
timeout: 30000, // ms; abort critical CSS generation after this timeout
16+
strict: false, // set to true to throw on CSS errors (will run faster if no errors)
17+
maxEmbeddedBase64Length: 1000, // characters; strip out inline base64 encoded resources larger than this
18+
userAgent: 'Penthouse Critical Path CSS Generator', // specify which user agent string when loading the page
19+
renderWaitTime: 100, // ms; render wait timeout before CSS processing starts (default: 100)
20+
blockJSRequests: true, // set to false to load (external) JS (default: true)
21+
phantomJsOptions: { // see `phantomjs --help` for the list of all available options
22+
// 'proxy': 'http://proxy.company.com:8080',
23+
// 'ssl-protocol': 'SSLv3'
24+
},
25+
customPageHeaders: {
26+
'Accept-Encoding': 'identity' // add if getting compression errors like 'Data corrupted'
27+
}
28+
}, argOptions);
29+
30+
const STDOUT_FD = 1;
31+
const STDERR_FD = 2;
32+
33+
penthouse(penthouseOptions).then(function(criticalCss) {
34+
fs.writeSync(STDOUT_FD, criticalCss);
35+
fs.fsyncSync(STDOUT_FD);
36+
}).catch(function(err) {
37+
fs.writeSync(STDERR_FD, err);
38+
fs.fsyncSync(STDERR_FD);
39+
process.exit(1);
40+
});

0 commit comments

Comments
 (0)