Critical Path CSS Generator
Penthouse is the original critical path css generator, helping you out to speed up page rendering for your websites. Supply your site's full CSS and the page you want to create the critical CSS for, and Penthouse will return the critical CSS needed to perfectly render the above the fold content of the page. Read more about critical path css here.
The process is automatic and the generated CSS is production ready as is. Behind the scenes Penthouse is using puppeteer to generate the critical css via the chromium:headless.
Penthouse can be used:
yarn add --dev penthouse
npm install, if not using yarn)
This will add penthouse to the list of dependencies.
Penthouse returns a promise (since version
but if you prefer you can also pass in a traditional node-style
function as the second argument.
The Penthouse Node module can also be used in Gulp.
# Basic verbose logging for all componentsenv DEBUG="penthouse,penthouse:*" node script.js
Install missing dependencies to get the headless Chrome to run:
sudo apt-get install libnss3
You might possibly need an even longer list of deps, depending on your dist, see this answer
A good first step can be to test your url + css in the hosted critical path css generator, to determine whether the problem is with the input your passing (css + url), or with your local setup: https://jonassebastianohlsson.com/criticalpathcssgenerator
If you see flashes of unstyled content showing after applying your critical css then something is wrong. Below are the most commont causes and some general related advice:
If your html is fine, but varies based on things such as the logged in user, third party advertising etc, then you can use the
forceInclude parameter to force specific extra styles to remain in the critical css, even if Penthouse doesn’t see them on the page during critical css generation.
This problem can happen if you have an element appearing early in the DOM, but with styles applied to move outside of the critical viewport (using absolute position or transforms). Penthouse does not look at the absolute position and transform values and will just see the element as not being part of the critical viewport, and hence Penthouse will not consider it’s styles critical (so the unstyled element will show when the critical css is used).
Solution: Consider whether it really should appear so early in the DOM, or use the
forceInclude property to make sure the styles to "hide"/move it are left in the critical css.
Problems with special characters like → after converting? Make sure you use the correct hexadecimal format in your CSS. You can always get this format from your browser console, by entering '→'
.charCodeAt(0).toString(16) (answer for this arrow glyph is
2192). When using hexadecimal format in CSS it needs to be prepended with a backslash, like so: