Contributors: danielbachhuber, tinybit Tags: performance Requires at least: 4.5 Tested up to: 6.9 Requires PHP: 7.4 Stable tag: 0.2.2 License: GPLv2 or later License URI: https://www.gnu.org/licenses/gpl-2.0.html
Generates and serves inline critical CSS.
The TinyBit Critical CSS plugin works with tinybit-critical-css-server to generate and serve inline critical CSS.
Here's how the plugin works:
- Critical CSS generation process is triggered by WP-CLI or the refresh webhook.
- Plugin renders a given WordPress page. The HTML and CSS of the page are sent to tinybit-critical-css-server.
- When tinybit-critical-css-server returns a successful response, plugin stores critical CSS to the filesystem.
- 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.
First, you'll need a working installation of 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:
handleis the name of the script handle (i.e. the first argument towp_enqueue_script()).sourceis the file path of the source CSS file.criticalis the file path where the critical CSS will be written.whenis 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 ashtml.
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.
- Autoload critical CSS options to avoid persistent object cache notoptions race condition.
- Add option to store critical CSS in database vs filesystem.
- Remove unfortunate attempts at working around a deployment trigger issue. (0.1.2-0.1.6)
- Add filter to generated HTML for modification before critical CSS is determined.
- Initial release.