Skip to content

Commit 1879c02

Browse files
Merge pull request #11 from Aranda-Cyber-Solutions-LLC/master
Add methods to allow generation of critical CSS for a specific route …
2 parents 73a18a3 + 7a301e5 commit 1879c02

File tree

5 files changed

+74
-8
lines changed

5 files changed

+74
-8
lines changed

README.md

+46-4
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,16 @@ This gem assumes that you'll load the rest of the CSS asyncronously. At the mome
88

99
This gem uses [PhantomJS](https://github.com/colszowka/phantomjs-gem) and [Penthouse](https://github.com/pocketjoso/penthouse) to generate the critical CSS.
1010

11+
## Update
12+
13+
Version 0.3.0 is not compatible with previous versions. Please read the Upgrading from Previous Versions section below for more information.
1114

1215
## Installation
1316

1417
Add `critical-path-css-rails` to your Gemfile:
1518

1619
```
17-
gem 'critical-path-css-rails', '~> 0.2.0'
20+
gem 'critical-path-css-rails', '~> 0.3.0'
1821
```
1922

2023
Download and install by running:
@@ -61,15 +64,15 @@ To load the generated critical CSS into your layout, in the head tag, insert:
6164

6265
```HTML+ERB
6366
<style>
64-
<%= CriticalPathCss.fetch(request.path) %>
67+
<%= CriticalPathCss.fetch(request.path) %>
6568
</style>
6669
```
6770

68-
A simple example using loadcss-rails looks like:
71+
A simple example using [loadcss-rails](https://github.com/michael-misshore/loadcss-rails) looks like:
6972

7073
```HTML+ERB
7174
<style>
72-
<%= CriticalPathCss.fetch(request.path) %>
75+
<%= CriticalPathCss.fetch(request.path) %>
7376
</style>
7477
<script>
7578
loadCSS("<%= stylesheet_path('application') %>");
@@ -80,6 +83,45 @@ A simple example using loadcss-rails looks like:
8083
</noscript>
8184
```
8285

86+
### Route-level Control of CSS Generation and Removal
87+
88+
CriticalPathCss exposes some methods to give the user more control over the generation of Critical CSS and managment of the CSS cache:
89+
90+
``` ruby
91+
CriticalPathCss.generate route # Generates the critical path CSS for the given route (relative path)
92+
93+
CriticalPathCss.generate_all # Generates critical CSS for all routes in critical_path_css.yml
94+
95+
CriticalPathCss.clear route # Removes the CSS for the given route from the cache
96+
97+
CriticalPathCss.clear_matched routes # Removes the CSS for the matched routes from the cache
98+
```
99+
100+
NOTE: The `clear_matched` method will not work with Memcached due to the latter's incompatibility with Rails' `delete_matched` method. We recommend using an alternative cache such as [Redis](https://github.com/redis-store/redis-rails).
101+
102+
In addition to the `critical_path_css:generate` rake task described above, you also have access to task which clears the CSS cache:
103+
104+
```
105+
rake critical_path_css:clear_all
106+
```
107+
NOTE: The `critical_path_css:clear_all` rake task may need to be customized to suit your particular cache implementation.
108+
109+
Careful use of these methods allows the developer to generate critical path CSS dynamically within the app. The user should strongly consider using a [background job](http://edgeguides.rubyonrails.org/active_job_basics.html) when generating CSS in order to avoid tying up a rails thread. The `generate` method will send a GET request to your server which could cause infinite recursion if the developer is not careful.
110+
111+
A user can use these methods to [dynamically generate critical path CSS](https://gist.github.com/taranda/1597e97ccf24c978b59aef9249666c77) without using the `rake critical_path_css:generate` rake task and without hardcoding the application's routes into `config/critical_path_css.yml`. See [this Gist](https://gist.github.com/taranda/1597e97ccf24c978b59aef9249666c77) for an example of such an implementation.
112+
113+
## Upgrading from Previous Versions
114+
115+
The latest version of Critcal Path CSS Rails changes the functionality of the `generate` method. In past versions,
116+
`generate` would produce CSS for all of the routes listed in `config/critical_path_css.yml`. This functionality has been replaced by the `generate_all` method, and `generate` will only produce CSS for one route.
117+
118+
Developers upgrading from versions prior to 0.3.0 will need to replace `CriticalPathCss:generate` with `CriticalPathCss:generate_all` throughout their codebase. One file that will need updating is `lib/tasks/critical_path_css.rake`. Users can upgrade this file automatically by running:
119+
120+
``` prompt
121+
rails generate critical_path_css:install
122+
```
123+
124+
Answer 'Y' when prompted to overwrite `critical_path_css.rake`. However, overwriting `critical_path_css.yml` is not necessary and not recommended.
83125

84126
## Versions
85127

lib/critical-path-css-rails.rb

+14-1
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,25 @@ module CriticalPathCss
33

44
CACHE_NAMESPACE = 'critical-path-css'
55

6-
def self.generate
6+
def self.generate(route)
7+
Rails.cache.write(route, CssFetcher.new.fetch_route(route),
8+
namespace: CACHE_NAMESPACE, expires_in: nil)
9+
end
10+
11+
def self.generate_all
712
CssFetcher.new.fetch.each do |route, css|
813
Rails.cache.write(route, css, namespace: CACHE_NAMESPACE, expires_in: nil)
914
end
1015
end
1116

17+
def self.clear(route)
18+
Rails.cache.delete(route, namespace: CACHE_NAMESPACE)
19+
end
20+
21+
def self.clear_matched(routes)
22+
Rails.cache.delete_matched(routes,namespace: CACHE_NAMESPACE)
23+
end
24+
1225
def self.fetch(route)
1326
Rails.cache.read(route, namespace: CACHE_NAMESPACE) || ''
1427
end

lib/critical_path_css/css_fetcher.rb

+5-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,11 @@ def fetch
1313
@config.routes.map { |route| [route, css_for_route(route)] }.to_h
1414
end
1515

16-
private
16+
def fetch_route(route)
17+
css_for_route route
18+
end
19+
20+
protected
1721

1822
def css_for_route(route)
1923
Phantomjs.run(PENTHOUSE_PATH, @config.base_url + route, @config.css_path)
+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
module CriticalPathCSS
22
module Rails
3-
VERSION = '0.2.4'
3+
VERSION = '0.3.0'
44
PENTHOUSE_VERSION = '0.3.4'
55
end
66
end

lib/tasks/critical_path_css.rake

+8-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,14 @@ require 'critical-path-css-rails'
33
namespace :critical_path_css do
44
desc 'Generate critical CSS for the routes defined'
55
task generate: :environment do
6-
CriticalPathCss.generate
6+
CriticalPathCss.generate_all
7+
end
8+
desc 'Clear all critical CSS from the cache'
9+
task clear_all: :environment do
10+
# Use the following for Redis cache implmentations
11+
CriticalPathCss.clear_matched('*')
12+
# Some other cache implementations may require the following syntax instead
13+
# CriticalPathCss.clear_matched(/.*/)
714
end
815
end
916

0 commit comments

Comments
 (0)