critical-path-css-rails 0.2.4 → 0.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 95bdd37882da54487a75f7757f4df5f45a0daa45
4
- data.tar.gz: 468b72efbff091a327d69a7fe1876f4698d0d363
3
+ metadata.gz: ae358ce3a4aeb862c846a0bf3ece801de83d7ab2
4
+ data.tar.gz: 477c25419bc4500075240103fd268245b7366dfb
5
5
  SHA512:
6
- metadata.gz: a6868c40d8d58f03a185cdf5b002ca010e27e7fcd22368a2ad498314362a1069b5166303f1d469f7808df7c4307730d6e47014fea433d4271d2580343b2687a5
7
- data.tar.gz: 36c659a731465c67db6a2b695bea134a321a333a95b9d715525049bf0c197932231508d258315bb5681019688edbbba683a3778a48cb1229ad1244278ea4576a
6
+ metadata.gz: 02041fc89747c725ae5e975793b4202b29bd202ab786c38e089994d02288fd11384a1434871d6896c7007cfbe83a1f9bff7a9677b03a38f5834c6fd17ee5f7ba
7
+ data.tar.gz: c783c2979070b990f35ffb04ac69f5f963b8856abde488fb4584729fd37fd410f1744e4cf10f810884a2d8ae33d4a08b0cc33a08881a40623c688670c4c8aba3
data/README.md CHANGED
@@ -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.
83
125
 
84
126
  ## Versions
85
127
 
@@ -3,12 +3,25 @@ module CriticalPathCss
3
3
 
4
4
  CACHE_NAMESPACE = 'critical-path-css'
5
5
 
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
7
12
  CssFetcher.new.fetch.each do |route, css|
8
13
  Rails.cache.write(route, css, namespace: CACHE_NAMESPACE, expires_in: nil)
9
14
  end
10
15
  end
11
16
 
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
+
12
25
  def self.fetch(route)
13
26
  Rails.cache.read(route, namespace: CACHE_NAMESPACE) || ''
14
27
  end
@@ -13,7 +13,11 @@ module CriticalPathCss
13
13
  @config.routes.map { |route| [route, css_for_route(route)] }.to_h
14
14
  end
15
15
 
16
- private
16
+ def fetch_route(route)
17
+ css_for_route route
18
+ end
19
+
20
+ protected
17
21
 
18
22
  def css_for_route(route)
19
23
  Phantomjs.run(PENTHOUSE_PATH, @config.base_url + route, @config.css_path)
@@ -1,6 +1,6 @@
1
1
  module CriticalPathCSS
2
2
  module Rails
3
- VERSION = '0.2.4'
3
+ VERSION = '0.3.0'
4
4
  PENTHOUSE_VERSION = '0.3.4'
5
5
  end
6
6
  end
@@ -3,7 +3,14 @@ require 'critical-path-css-rails'
3
3
  namespace :critical_path_css do
4
4
  desc 'Generate critical CSS for the routes defined'
5
5
  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(/.*/)
7
14
  end
8
15
  end
9
16
 
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: critical-path-css-rails
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.4
4
+ version: 0.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Michael Misshore
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2016-10-04 00:00:00.000000000 Z
11
+ date: 2017-01-18 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: phantomjs