Security News
Combatting Alert Fatigue by Prioritizing Malicious Intent
In 2023, data breaches surged 78% from zero-day and supply chain attacks, but developers are still buried under alerts that are unable to prevent these threats.
Extract & Inline Critical-path CSS from HTML
A module by @addyosmani and @sindresorhus
$ npm install --save critical
Include:
var critical = require('critical');
critical.generateInline({
base: 'dist/',
src: 'index.html',
styleTarget: 'styles/main.css',
htmlTarget: 'index-critical.html',
width: 320,
height: 480,
minify: true
});
Basic usage:
critical.generate({
base: 'test/',
src: 'index.html',
dest: 'styles/main.css',
width: 320,
height: 480
});
Generate and minify critical-path CSS:
critical.generate({
base: 'test/',
src: 'index.html',
dest: 'styles/styles.min.css',
minify: true,
width: 320,
height: 480
});
Generate and return output via a callback:
critical.generate({
base: 'test/',
src: 'index.html',
width: 320,
height: 480
}, function (err, output) {
// You now have critical-path CSS
// Works with and without dest specified
});
<style>
/ critical CSS from generationBasic usage:
critical.inline({
base: 'test/',
src: 'index-critical.html',
dest: 'inlined.html'
});
Minify and inline stylesheets:
critical.inline({
base: 'test/',
src: 'index-critical.html',
dest: 'inlined-minified.html',
minify: true
});
Inline and return output via a callback:
critical.inline({
base: 'test/',
src: 'index-critical.html'
}, function (err, output){
// You now have HTML with inlined critical-path CSS
// Works with and without dest specified
});
Name | Type | Description |
---|---|---|
base | string | Base directory in which the source and destination are to be written |
src | string | Location of the HTML source to be operated against |
dest | string | Location of where to save the output of an operation |
width | integer | (Generation only) Width of the target viewport |
height | integer | (Generation only) Height of the target viewport |
minify | boolean | Enable minification of CSS output |
styleTarget | string | (generateInline only) Destination for critical-path styles |
htmlTarget | string | (generateInline only) Destination for (critical-path CSS) style-inlined HTML |
CSS is required to construct the render tree for your pages and JavaScript will often block on CSS during initial construction of the page. You should ensure that any non-essential CSS is marked as non-critical (e.g. print and other media queries), and that the amount of critical CSS and the time to deliver it is as small as possible.
For best performance, you may want to consider inlining the critical CSS directly into the HTML document. This eliminates additional roundtrips in the critical path and if done correctly can be used to deliver a “one roundtrip” critical path length where only the HTML is a blocking resource.
Why, yes!. Take a look at this Gulp project which demonstrates using Critical to generate and inline critical-path CSS.
I recommend using Penthouse directly if your app has a large number of styles or stylesheets being dynamically injected into the DOM. Critical is best used when your page uses a fixed set of stylesheets as we can automatically scrape this for you, avoiding the overhead of passing known styles yourself manually to Penthouse.
Apache 2.0
Copyright 2014 Google Inc
FAQs
Extract & Inline Critical-path CSS from HTML
We found that critical demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 0 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
In 2023, data breaches surged 78% from zero-day and supply chain attacks, but developers are still buried under alerts that are unable to prevent these threats.
Security News
Solo open source maintainers face burnout and security challenges, with 60% unpaid and 60% considering quitting.
Security News
License exceptions modify the terms of open source licenses, impacting how software can be used, modified, and distributed. Developers should be aware of the legal implications of these exceptions.