|
|
2 years ago | |
|---|---|---|
| .. | ||
| dist | 2 years ago | |
| node_modules | 2 years ago | |
| src | 2 years ago | |
| README.md | 2 years ago | |
| package.json | 2 years ago | |
Critters is a plugin that inlines your app's critical CSS and lazy-loads the rest.
It's a little different from other options, because it doesn't use a headless browser to render content. This tradeoff allows Critters to be very fast and lightweight. It also means Critters inlines all CSS rules used by your document, rather than only those needed for above-the-fold content. For alternatives, see Similar Libraries.
Critters' design makes it a good fit when inlining critical CSS for prerendered/SSR'd Single Page Applications. It was developed to be an excellent compliment to prerender-loader, combining to dramatically improve first paint time for most Single Page Applications.
First, install Critters as a development dependency:
npm i -D critters
or
yarn add -D critters
import Critters from 'critters';
const critters = new Critters({
// optional configuration (see below)
});
const html = `
<style>
.red { color: red }
.blue { color: blue }
</style>
<div class="blue">I'm Blue</div>
`;
const inlined = await critters.process(html);
console.log(inlined);
// "<style>.blue{color:blue}</style><div class=\"blue\">I'm Blue</div>"
Critters is also available as a Webpack plugin called critters-webpack-plugin.
The Webpack plugin supports the same configuration options as the main critters package:
// webpack.config.js
+const Critters = require('critters-webpack-plugin');
module.exports = {
plugins: [
+ new Critters({
+ // optional configuration (see below)
+ })
]
}
That's it! The resultant html will have its critical CSS inlined and the stylesheets lazy-loaded.
All optional. Pass them to new Critters({ ... }).
optionspath String Base path location of the CSS files (default: '')publicPath String Public path of the CSS resources. This prefix is removed from the href (default: '')external Boolean Inline styles from external stylesheets (default: true)inlineThreshold Number Inline external stylesheets smaller than a given size (default: 0)minimumExternalSize Number If the non-critical external stylesheet would be below this size, just inline it (default: 0)pruneSource Boolean Remove inlined rules from the external stylesheet (default: false)mergeStylesheets Boolean Merged inlined stylesheets into a single <style> tag (default: true)additionalStylesheets Array<String> Glob for matching other stylesheets to be used while looking for critical CSS.preload String Which preload strategy to usenoscriptFallback Boolean Add <noscript> fallback to JS-based strategiesinlineFonts Boolean Inline critical font-face rules (default: false)preloadFonts Boolean Preloads critical fonts (default: true)fonts Boolean Shorthand for setting inlineFonts + preloadFonts* Values:
true to inline critical font-face rules and preload the fontsfalse to don't inline any font-face rules and don't preload fontskeyframes String Controls which keyframes rules are inlined.* Values:
"critical": (default) inline keyframes rules used by the critical CSS"all" inline all keyframes rules"none" remove all keyframes rulescompress Boolean Compress resulting critical CSS (default: true)logLevel String Controls log level of the plugin (default: "info")logger object Provide a custom logger interface loggerCustom logger interface:
Type: object
trace function (String) Prints a trace messagedebug function (String) Prints a debug messageinfo function (String) Prints an information messagewarn function (String) Prints a warning messageerror function (String) Prints an error messageControls log level of the plugin. Specifies the level the logger should use. A logger will not produce output for any log level beneath the specified level. Available levels and order are:
Type: ("info" | "warn" | "error" | "trace" | "debug" | "silent")
The mechanism to use for lazy-loading stylesheets.
Note: JS indicates a strategy requiring JavaScript (falls back to <noscript> unless disabled).
media="not x" and removing once loaded. JSrel="stylesheet" once loaded (details). JS<link rel="alternate stylesheet preload"> and swap to rel="stylesheet" once loaded (details). JS"js", but the stylesheet is disabled until fully loaded.Type: (default | "body" | "media" | "swap" | "swap-high" | "js" | "js-lazy")
There are a number of other libraries that can inline Critical CSS, each with a slightly different approach. Here are a few great options:
This is not an official Google product.