Skip to content

Commit 77282c8

Browse files
Merge branch 'master' into refactors
2 parents b8db8b8 + f505de0 commit 77282c8

File tree

11 files changed

+300
-54
lines changed

11 files changed

+300
-54
lines changed

README.md

+9-2
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.6.0'
30+
gem 'critical-path-css-rails', '~> 2.10.0'
3131
```
3232

3333
Download and install by running:
@@ -54,6 +54,7 @@ First, you'll need to configue a few things in the YAML file: `config/critical_p
5454

5555
* `manifest_name`: If you're using the asset pipeline, add the manifest name.
5656
* `css_path`: If you're not using the asset pipeline, you'll need to define the path to the application's main CSS. The gem assumes your CSS lives in `RAILS_ROOT/public`. If your main CSS file is in `RAILS_ROOT/public/assets/main.css`, you would set the variable to `/assets/main.css`.
57+
* `css_paths`: If you have the need to specify multiple CSS source files, you can do so with `css_paths`. Note that `css_path` and `css_paths` are **mutually exclusive**; if using `css_path`, configuration for `css_paths` should be omitted, and vice versa. When using this option, a separate CSS path must be specified for each route, and they will be matched based on the order specified (the first CSS path will be applied to the first route, the second CSS path to the second route, etc).
5758
* `routes`: List the routes that you would like to generate the critical CSS for. (i.e. /resources, /resources/show/1, etc.)
5859
* `base_url`: Add your application's URL for the necessary environments.
5960

@@ -87,7 +88,7 @@ A simple example using [loadcss-rails](https://github.com/michael-misshore/loadc
8788
<script>
8889
loadCSS("<%= stylesheet_path('application') %>");
8990
</script>
90-
<link rel="preload" href="<%= stylesheet_path('application') %>" as="style" onload="this.rel='stylesheet'">
91+
<link rel="preload" href="<%= stylesheet_path('application') %>" as="style" onload="this.onload=null;this.rel='stylesheet'">
9192
<noscript>
9293
<link rel="stylesheet" href="<%= stylesheet_path('application') %>">
9394
</noscript>
@@ -146,6 +147,12 @@ This gem is to be tested inside of docker/docker-compose. [Combustion](https://g
146147

147148
Once shell'd in, run `bundle exec rspec spec` to run the test. The test rails app lives in `spec/internal`, and it can be viewed locally at `http://localhost:9292/`
148149

150+
If you encounter Chromium errors trying to run the tests, installing [Puppeteer](https://github.com/GoogleChrome/puppeteer) might help.
151+
152+
```Bash
153+
npm install puppeteer
154+
```
155+
149156

150157
## Versions
151158

lib/config/critical_path_css.yml

+3
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@ defaults: &defaults
33
manifest_name: application
44
# Else provide the relative path of your CSS file from the /public directory
55
# css_path: /path/to/css/from/public/main.css
6+
# Or provide a separate path for each route
7+
# css_paths:
8+
# - /path/to/css/from/public/main.css
69
routes:
710
- /
811

lib/critical_path_css/configuration.rb

+4
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,10 @@ def css_path
1313
@config['css_path']
1414
end
1515

16+
def css_paths
17+
@config['css_paths']
18+
end
19+
1620
def manifest_name
1721
@config['manifest_name']
1822
end

lib/critical_path_css/css_fetcher.rb

+6-3
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,10 @@ def initialize(config)
1010
end
1111

1212
def fetch
13-
@config.routes.map { |route| [route, css_for_route(route)] }.to_h
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
1417
end
1518

1619
def fetch_route(route)
@@ -19,10 +22,10 @@ def fetch_route(route)
1922

2023
protected
2124

22-
def css_for_route(route)
25+
def css_for_route(route, css_path)
2326
options = {
2427
'url' => @config.base_url + route,
25-
'css' => @config.css_path,
28+
'css' => css_path,
2629
## optional params
2730
# viewport dimensions
2831
'width' => 1300,

lib/critical_path_css/rails/config_loader.rb

+17-2
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,19 @@ class ConfigLoader
55

66
def load
77
config = YAML.safe_load(ERB.new(File.read(configuration_file_path)).result, [], [], true)[::Rails.env]
8-
config['css_path'] = "#{::Rails.root}/public" + (
8+
validate_css_path config
9+
if config['css_path']
10+
config['css_path'] = "#{::Rails.root}/public" + (
911
config['css_path'] ||
1012
ActionController::Base.helpers.stylesheet_path(
1113
config['manifest_name'], host: ''
1214
)
13-
)
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
1421
config
1522
end
1623

@@ -19,6 +26,14 @@ def load
1926
def configuration_file_path
2027
@configuration_file_path ||= ::Rails.root.join('config', CONFIGURATION_FILENAME)
2128
end
29+
30+
def validate_css_path(config)
31+
if config['css_path'] && config['css_paths']
32+
raise LoadError, 'Cannot specify both css_path and css_paths'
33+
elsif config['css_paths'] && config['css_paths'].length != config['routes'].length
34+
raise LoadError, 'Must specify css_paths for each route'
35+
end
36+
end
2237
end
2338
end
2439
end
+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
module CriticalPathCSS
22
module Rails
3-
VERSION = '2.6.0'.freeze
3+
VERSION = '3.0.0'.freeze
44
end
55
end

package-lock.json

+70-45
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
"lib": "lib"
88
},
99
"dependencies": {
10-
"penthouse": "=1.6.0"
10+
"penthouse": "=1.10.1"
1111
},
1212
"license": "MIT"
1313
}
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
1+
<h1>Critical Path CSS Rails Test App</h1>
12
<p><%= CriticalPathCss.fetch(request.path) %></p>

0 commit comments

Comments
 (0)