Install penthouse
masterYou can install penthouse as a development dependency using yarn or npm.
yarn add --dev penthouse(or npm install if you are not using yarn)
repository·master·Indexed 25 days ago
https://github.com/pocketjoso/penthouseA critical path CSS generator (version 2.3.3) that uses Puppeteer and a headless Chromium instance to extract the minimal CSS required to render the above-the-fold content of a webpage. It allows for viewport customization via width and height, selector-based inclusion or exclusion, and the ability to block JavaScript requests to optimize page rendering speed.
You can install penthouse as a development dependency using yarn or npm.
yarn add --dev penthouse(or npm install if you are not using yarn)
Penthouse accepts an options object to customize the extraction process.
| Name | Type | Default | Description |
|---|---|---|---|
url | string | Accessible url. Use file:/// protocol for local html files. | |
cssString | string | Original css to extract critical css from | |
css | string | Path to original css file on disk (if using instead of cssString) | |
width | integer | 1300 | Width for critical viewport |
height | integer | 900 | Height for critical viewport |
screenshots | object | Configuration for screenshots (not used by default). | |
keepLargerMediaQueries | boolean | false | Keep media queries even for width/height values larger than critical viewport. |
forceInclude | array | [] | Array of css selectors to keep in critical css, even if not appearing in critical viewport. Strings or regex (e.f. ['.keepMeEvenIfNotSeenInDom', /^\.button/]) |
forceExclude | array | [] | Array of css selectors to remove in critical css, even if appearing in critical viewport. Strings or regex (e.f. ['.doNotKeepMeEvenIfNotSeenInDom', /^\.button/]) |
propertiesToRemove | array | ['(.*)transition(.*)', 'cursor', 'pointer-events', '(-webkit-)?tap-highlight-color', '(.*)user-select'] | Css properties to filter out from critical css |
timeout | integer | 30000 | Ms; abort critical CSS generation after this time |
puppeteer | object | Settings for puppeteer. | |
pageLoadSkipTimeout | integer | 0 | Ms; stop waiting for page load after this time |
renderWaitTime | integer | 100 | ms; wait time after page load before critical css extraction starts |
blockJSRequests | boolean | true | set to false to load JS (not recommended) |
maxEmbeddedBase64Length | integer | 1000 | characters; strip out inline base64 encoded resources larger than this |
maxElementsToCheckPerSelector | integer | undefined | Limit nr of elements to inspect per css selector to reduce execution time. |
userAgent | string | 'Penthouse Critical Path CSS Generator' | specify which user agent string when loading the page |
customPageHeaders | object | Set extra http headers to be sent with the request for the url. | |
cookies | array | [] | For formatting of each cookie, see Puppeteer setCookie docs |
strict | boolean | false | Make Penthouse throw on errors parsing the original CSS. (Legacy, not recommended) |
allowedResponseCode | number|regex|function | Let Penthouse stop if the server response code is not matching this value. |
If you see flashes of unstyled content (FOUC) when using critical CSS, check the following:
forceInclude option to keep those styles in the critical CSS.absolute positioning or transform values when determining if an element is in the viewport. If an element is early in the DOM but moved into the viewport via CSS, Penthouse might exclude its styles. Use forceInclude to prevent this.If you are running on Linux and encounter issues with headless Chrome, you may need to install missing dependencies. A common requirement is libnss3.
sudo apt-get install libnss3sudo apt-get install libnss3If special characters (like arrows) are not showing correctly, ensure you are using the correct hexadecimal format in your CSS, prepended with a backslash.
Example for the arrow glyph →:
'→'.charCodeAt(0).toString(16) (returns 2192).content: '\2192';Penthouse uses the debug module. You can enable verbose logging for all components by setting the DEBUG environment variable.
# Basic verbose logging for all components
env DEBUG="penthouse,penthouse:*" node script.js# Basic verbose logging for all components
env DEBUG="penthouse,penthouse:*" node script.jsPenthouse extracts the critical CSS needed to render the above-the-fold content of a page. It uses Puppeteer and a headless Chromium instance.
Only url and cssString (or css) are required. Note that the HTML found via the url is expected to be styled; Penthouse does not inject styles, it only prunes the provided CSS.
To run many jobs effectively, reuse a single browser instance and run each job in its own browser tab to optimize performance.
penthouse({
url: 'http://google.com',
cssString: 'body { color: red }'
})
.then(criticalCss => {
// use the critical css
fs.writeFileSync('outfile.css', criticalCss);
})Penthouse allows fine-grained control over the Puppeteer browser instance used for CSS pruning. Key configuration capabilities include:
userAgent: Set a custom User-Agent string.cookies: Pass an array of cookie objects to be set in the browser.customPageHeaders: Set extra HTTP headers for the page requests.blockJSRequests: If set to true, Penthouse will disable JavaScript and intercept/abort all .js requests to speed up the process and reduce noise.width & height: Define the viewport dimensions used to determine critical CSS.If you encounter the error PAGE_UNLOADED_DURING_EXECUTION: Critical css generation script could not be executed., it typically means the page navigated away (via window.location, a meta tag refresh, or a redirect) after loading but before Penthouse could finish.
To resolve this:
You can capture screenshots to visually verify the critical CSS application. Provide a screenshots object with a basePath to enable this. Penthouse will take two screenshots:
before: A screenshot of the page with original styles.after: A screenshot of the page after the generated critical CSS has been inlined.The file extension is determined by the screenshots.type (e.g., jpeg results in .jpg, otherwise .png).
The main export of penthouse is an asynchronous function used to generate critical-path CSS for a given URL. It accepts an options object and can be used with a callback or as a Promise.
Key features include:
cssString) or a file path (css).width and height.puppeteer.getBrowser.blockJSRequests).forceInclude, forceExclude).propertiesToRemove).When configuring Penthouse, you can use the allowedResponseCode option to ensure the page loads with the expected HTTP status. This option supports three types of validation:
200).