You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.md
+46-4
Original file line number
Diff line number
Diff line change
@@ -8,13 +8,16 @@ This gem assumes that you'll load the rest of the CSS asyncronously. At the mome
8
8
9
9
This gem uses [PhantomJS](https://github.com/colszowka/phantomjs-gem) and [Penthouse](https://github.com/pocketjoso/penthouse) to generate the critical CSS.
10
10
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.
11
14
12
15
## Installation
13
16
14
17
Add `critical-path-css-rails` to your Gemfile:
15
18
16
19
```
17
-
gem 'critical-path-css-rails', '~> 0.2.0'
20
+
gem 'critical-path-css-rails', '~> 0.3.0'
18
21
```
19
22
20
23
Download and install by running:
@@ -61,15 +64,15 @@ To load the generated critical CSS into your layout, in the head tag, insert:
61
64
62
65
```HTML+ERB
63
66
<style>
64
-
<%= CriticalPathCss.fetch(request.path) %>
67
+
<%= CriticalPathCss.fetch(request.path) %>
65
68
</style>
66
69
```
67
70
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:
69
72
70
73
```HTML+ERB
71
74
<style>
72
-
<%= CriticalPathCss.fetch(request.path) %>
75
+
<%= CriticalPathCss.fetch(request.path) %>
73
76
</style>
74
77
<script>
75
78
loadCSS("<%= stylesheet_path('application') %>");
@@ -80,6 +83,45 @@ A simple example using loadcss-rails looks like:
80
83
</noscript>
81
84
```
82
85
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.
0 commit comments