-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathreadme.txt
82 lines (59 loc) · 2.88 KB
/
readme.txt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
=== TinyBit Critical CSS ===
Contributors: danielbachhuber, tinybit
Tags: performance
Requires at least: 4.5
Tested up to: 6.4
Requires PHP: 7.4
Stable tag: 0.1.1
License: GPLv2 or later
License URI: https://www.gnu.org/licenses/gpl-2.0.html
Generates and serves inline critical CSS.
== Description ==
The TinyBit Critical CSS plugin works with [tinybit-critical-css-server](https://github.com/pinchofyum/tinybit-critical-css-server) to generate and serve inline critical CSS.
Here's how the plugin works:
1. Critical CSS generation process is triggered by WP-CLI or the refresh webhook.
2. Plugin renders a given WordPress page. The HTML and CSS of the page are sent to tinybit-critical-css-server.
3. When tinybit-critical-css-server returns a successful response, plugin stores critical CSS to the filesystem.
4. If the critical CSS exists for a given request, the critical CSS is included inline and the stylesheet is deferred.
Et voila! You have inline critical CSS.
== Installation ==
First, you'll need a working installation of [tinybit-critical-css-server](https://github.com/pinchofyum/tinybit-critical-css-server). Get that running if you haven't already.
Once the server is running, add a `TINYBIT_CRITICAL_CSS_SERVER` constant to your preferred location:
```
define( 'TINYBIT_CRITICAL_CSS_SERVER', 'http://localhost:8080' );
```
This constant tells the plugin where to send requests.
Next, filter `tinybit_critical_css_pages` to define the pages you'd like to generate critical CSS for:
```
add_filter(
'tinybit_critical_css_pages',
function() {
return [
home_url( '/' ) => [
'handle' => 'tinybit-style',
'source' => TINYBIT_ASSETS_DIR . '/dist/style.css',
'critical' => TINYBIT_ASSETS_DIR . '/dist/critical/home.css',
'when' => function() {
return is_home();
},
],
];
}
);
```
In this particular example:
* `handle` is the name of the script handle (i.e. the first argument to `wp_enqueue_script()`).
* `source` is the file path of the source CSS file.
* `critical` is the file path where the critical CSS will be written.
* `when` is a callback for the context you'd like the critical CSS file to be used (so you can use it on more than one page).
* `home_url( '/' )` is the page to be rendered and passed to the server as `html`.
Next, to generate critical CSS via WP-CLI, run:
```
wp tinybit-critical-css generate --url=https://tinybit.com/
```
If you'd like to regenerate critical CSS after each deploy, run `wp tinybit-critical-css refresh-webhook` to grab a webhook you can ping. Because the webhook queues a one-time cron job, we'd recommend using some alternate WP Cron system to avoid timeout issues.
== Changelog ==
= 0.1.1 =
* Add filter to generated HTML for modification before critical CSS is determined.
= 0.1.0 =
* Initial release.