Skip to content

Commit ce4c3a2

Browse files
Merge pull request #34 from mudbugmedia/refactors
Refactors
2 parents f505de0 + c04137c commit ce4c3a2

File tree

8 files changed

+135
-123
lines changed

8 files changed

+135
-123
lines changed

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ However, more packages may need to be installed depending on your OS distributio
2727
After reviewing the dependency requirements, add `critical-path-css-rails` to your Gemfile:
2828

2929
```
30-
gem 'critical-path-css-rails', '~> 2.10.0'
30+
gem 'critical-path-css-rails', '~> 3.0.0'
3131
```
3232

3333
Download and install by running:

docker/ruby/Dockerfile

+3-2
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,11 @@ RUN apt-get update && apt-get install -y build-essential libpq-dev nodejs npm
77
# Install Penthouse JS Dependencies
88
RUN apt-get install -y libpangocairo-1.0-0 libx11-xcb1 libxcomposite1 libxcursor1 libxdamage1 libxi6 libxtst6 libnss3 libcups2 libxss1 libxrandr2 libgconf2-4 libasound2 libatk1.0-0 libgtk-3-0
99

10+
# Configure Node/NPM
1011
RUN npm cache clean -f
1112
RUN npm install -g n
12-
RUN n 8.9.3
13-
RUN ln -sf /usr/local/n/versions/node/8.9.3/bin/node /usr/bin/nodejs
13+
RUN n 10.15.1
14+
RUN ln -sf /usr/local/n/versions/node/10.15.1/bin/node /usr/bin/nodejs
1415

1516
ENV BUNDLE_PATH /gems
1617

lib/critical-path-css-rails.rb

+8-9
Original file line numberDiff line numberDiff line change
@@ -6,16 +6,11 @@ module CriticalPathCss
66
CACHE_NAMESPACE = 'critical-path-css'.freeze
77

88
def self.generate(route)
9-
::Rails.cache.write(
10-
route,
11-
CssFetcher.new(config).fetch_route(route),
12-
namespace: CACHE_NAMESPACE,
13-
expires_in: nil
14-
)
9+
::Rails.cache.write(route, fetcher.fetch_route(route), namespace: CACHE_NAMESPACE, expires_in: nil)
1510
end
1611

1712
def self.generate_all
18-
CssFetcher.new(config).fetch.each do |route, css|
13+
fetcher.fetch.each do |route, css|
1914
::Rails.cache.write(route, css, namespace: CACHE_NAMESPACE, expires_in: nil)
2015
end
2116
end
@@ -32,7 +27,11 @@ def self.fetch(route)
3227
::Rails.cache.read(route, namespace: CACHE_NAMESPACE) || ''
3328
end
3429

35-
def self.config
36-
@config ||= Configuration.new(CriticalPathCss::Rails::ConfigLoader.new.load)
30+
def self.fetcher
31+
@fetcher ||= CssFetcher.new(Configuration.new(config_loader.config))
32+
end
33+
34+
def self.config_loader
35+
@config_loader ||= CriticalPathCss::Rails::ConfigLoader.new
3736
end
3837
end

lib/critical_path_css/css_fetcher.rb

+15-15
Original file line numberDiff line numberDiff line change
@@ -10,33 +10,21 @@ def initialize(config)
1010
end
1111

1212
def fetch
13-
@config.routes.map.with_index { |route, index|
14-
css_path = @config.css_paths[index].present? ? @config.css_paths[index] : @config.css_path
15-
[route, css_for_route(route, css_path)]
16-
}.to_h
13+
@config.routes.map { |route| [route, fetch_route(route)] }.to_h
1714
end
1815

1916
def fetch_route(route)
20-
css_for_route route
21-
end
22-
23-
protected
24-
25-
def css_for_route(route, css_path)
2617
options = {
2718
'url' => @config.base_url + route,
28-
'css' => css_path,
29-
## optional params
30-
# viewport dimensions
19+
'css' => fetch_css_path_for_route(route),
3120
'width' => 1300,
3221
'height' => 900,
22+
'timeout' => 30_000,
3323
# CSS selectors to always include, e.g.:
3424
'forceInclude' => [
3525
# '.keepMeEvenIfNotSeenInDom',
3626
# '^\.regexWorksToo'
3727
],
38-
# ms; abort critical CSS generation after this timeout
39-
'timeout' => 30_000,
4028
# set to true to throw on CSS errors (will run faster if no errors)
4129
'strict' => false,
4230
# characters; strip out inline base64 encoded resources larger than this
@@ -63,5 +51,17 @@ def css_for_route(route, css_path)
6351
end
6452
out
6553
end
54+
55+
private
56+
57+
def fetch_css_path_for_route(route)
58+
index_for_route = @config.routes.index(route)
59+
60+
if index_for_route && @config.css_paths[index_for_route]
61+
@config.css_paths[index_for_route]
62+
else
63+
@config.css_path
64+
end
65+
end
6666
end
6767
end

lib/critical_path_css/rails/config_loader.rb

+22-17
Original file line numberDiff line numberDiff line change
@@ -3,22 +3,13 @@ module Rails
33
class ConfigLoader
44
CONFIGURATION_FILENAME = 'critical_path_css.yml'.freeze
55

6-
def load
7-
config = YAML.safe_load(ERB.new(File.read(configuration_file_path)).result, [], [], true)[::Rails.env]
8-
validate_css_path config
9-
if config['css_path']
10-
config['css_path'] = "#{::Rails.root}/public" + (
11-
config['css_path'] ||
12-
ActionController::Base.helpers.stylesheet_path(
13-
config['manifest_name'], host: ''
14-
)
15-
)
16-
config['css_paths'] = []
17-
else
18-
config['css_path'] = ''
19-
config['css_paths'] = config['css_paths'].collect { |path| "#{::Rails.root}/public#{path}" }
20-
end
21-
config
6+
def initialize
7+
validate_css_paths
8+
format_css_paths
9+
end
10+
11+
def config
12+
@config ||= YAML.safe_load(ERB.new(File.read(configuration_file_path)).result, [], [], true)[::Rails.env]
2213
end
2314

2415
private
@@ -27,7 +18,21 @@ def configuration_file_path
2718
@configuration_file_path ||= ::Rails.root.join('config', CONFIGURATION_FILENAME)
2819
end
2920

30-
def validate_css_path(config)
21+
def format_css_paths
22+
if config['css_path']
23+
config['css_path'] = format_path(config['css_path'])
24+
config['css_paths'] = []
25+
else
26+
config['css_path'] = ''
27+
config['css_paths'] = config['css_paths'].collect { |path| format_path(path) }
28+
end
29+
end
30+
31+
def format_path(path)
32+
"#{::Rails.root}/public#{path}"
33+
end
34+
35+
def validate_css_paths
3136
if config['css_path'] && config['css_paths']
3237
raise LoadError, 'Cannot specify both css_path and css_paths'
3338
elsif config['css_paths'] && config['css_paths'].length != config['routes'].length

0 commit comments

Comments
 (0)