By Dennis Burger, Waddinxveen aug. 2014
This Grunt workflow task is using grunt-penthouse and grunt-contrib-cssmin plugins to extract Critical Path (above-the-fold content) Css from various URL's. This extracted Css can then be (re)inserted as embedded Css (using a HTML <style> -tag) back into the HTML-head of the corresponding webpage.
Using a lazy-loading technique, the other Css files could be loaded into the HTML-DOM later by Javascript. This greatly speeds up the initial page rendering and perception of page speed.
Google PageSpeed Insights recommends inlining this Critical Path Css into the HTML page to stop Css rendering blocking issues in the webbrowser. Website performance (and initial page render speed) are more and more becoming a important search-engine ranking factor.
- NodeJS installed
- The Gruntfile.jsandpackage.jsonfiles from this Dutchwebworks Github Gist
- NodeJS Grunt Penthouse plugin
- Github pocketjoso / penthouse
- Filament group article: How we make RWD sites load fast as heck
- Online Critical Path Css Generator by Jonas Sebastian Ohlsson
When NodeJS and Grunt command-line tools (CLI) are installed open a Terminal (or MS-DOS command-line) window and cd into the directory where the Gruntfile.js file resides.
First make sure the required NPM Node packages (Grunt plugins) are installed in the familiar node_modules/ directory (ignore this directory in version-control) by running:
sudo npm install
On Windows, omit the sudo part.
Open the Gruntfile.js and configure the various path's in the config section. Like the (local) domain name of the website and the required (PhantomJS) screen width and height. The height is important, every Css rule used within this space (by the webbrowser) will be extracted.
In the penthouse section, change, copy/paste the config blocks for each page (URL). Each page will have it's own critical (minified) Css file. Name the Css files accordingly.
First open a Terminal (or MS-DOS command-line) window to cd into the same directory as the Gruntfile.js. Then run the (default) Grunt task:
grunt
This will open each configured page URL (in the penthouse section) using a headless / webkit (command-line) webbrowser called PhantomJS. The Grunt penthouse task will extract the visible Css (using the configured width and height) and save it to the outfile output file.
A separate Grunt task named cssmin (will automatically  run after the penthouse task) will minify the extracted Css into a separate *.min.css version of each Css file.
The last step is to (manually) embed this extract minified Css into the corresponding page's HTML-head.
Or one could use for example PHP to embed this extracted critical Css directly into the HTML-head from the minfied Css file as a <style> tag.