UNPKG

8.02 kBMarkdownView Raw
1# penthouse
2
3> Critical Path CSS Generator
4
5[![NPM version][npm-image]][npm-url]
6[![Build Status][travis-image]][travis-url]
7[![Downloads][dlcounter-image]][npm-url]
8
9## About
10
11Penthouse 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](http://www.phpied.com/css-and-the-critical-path/).
12
13The process is automatic and the generated CSS is production ready as is. Behind the scenes Penthouse is using [puppeteer](https://github.com/GoogleChrome/puppeteer) to generate the critical css via the chromium:headless.
14
15## Usage
16
17(If you don’t want to write code, you can use [the online hosted version](https://jonassebastianohlsson.com/criticalpathcssgenerator/).)
18
19```
20yarn add --dev penthouse
21```
22(or `npm install`, if not using [yarn](https://yarnpkg.com))
23
24### Basic example
25
26```js
27penthouse({
28 url: 'http://google.com',
29 cssString: 'body { color: red }'
30})
31.then(criticalCss => {
32 // use the critical css
33 fs.writeFileSync('outfile.css', criticalCss);
34})
35```
36Note: `Penthouse` returns a promise (since version `0.11`),
37but if you prefer you can also pass in a traditional node-style `callback`
38function as the second argument.
39
40### More examples
41https://github.com/pocketjoso/penthouse/tree/master/examples
42
43### Performance when running many jobs
44Penthouse is optimised for running many jobs in parallel.
45One shared browser instance is re-used and each job runs in its own browser tab.
46There's only so many jobs you can run in parallel before your machine starts running out of resources. To run many jobs effectively, see the [many urls example](https://github.com/pocketjoso/penthouse/tree/master/examples/many-urls.js).
47
48## Options
49Only `url` and `cssString` are required - all other options are optional.
50
51| Name | Type | Default | Description |
52| ---------------- | ------------------ | ------------- |------------- |
53| url | `string` | | Accessible url. Use `file:///` protocol for local html files. |
54| cssString | `string` | | Original css to extract critical css from |
55| css | `string` | | Path to original css file on disk (if using instead of `cssString`) |
56| width | `integer` | `1300` | Width for critical viewport |
57| height | `integer` | `900` | Height for critical viewport |
58| screenshots | `object` | | Configuration for screenshots (not used by default). See [Screenshot example](https://github.com/pocketjoso/penthouse/tree/master/examples/screenshots.js) |
59| keepLargerMediaQueries | `boolean` | `false` | Keep media queries even for width/height values larger than critical viewport. |
60| forceInclude | `array` | `[]` | Array of css selectors to keep in critical css, even if not appearing in critical viewport. Strings or regex (f.e. `['.keepMeEvenIfNotSeenInDom', /^\.button/]`) |
61| propertiesToRemove | `array` | `['(.*)transition(.*)', 'cursor', 'pointer-events', '(-webkit-)?tap-highlight-color', '(.*)user-select']` ] | Css properties to filter out from critical css |
62| timeout | `integer` | `30000` | Ms; abort critical CSS generation after this time |
63| puppeteer | `object` | | Settings for puppeteer. See [Custom puppeteer browser example](https://github.com/pocketjoso/penthouse/tree/master/examples/custom-browser.js) |
64| pageLoadSkipTimeout | `integer` | `0` | Ms; stop waiting for page load after this time (for sites when page load event is unreliable) |
65| renderWaitTime | `integer` | `100` | ms; wait time after page load before critical css extraction starts |
66| blockJSRequests | `boolean` | `true` | set to false to load JS (not recommended)
67| maxEmbeddedBase64Length | `integer` | `1000` | characters; strip out inline base64 encoded resources larger than this |
68| maxElementsToCheckPerSelector | `integer` | `undefined` | Can be specified to limit nr of elements to inspect per css selector, reducing execution time.
69| userAgent | `string` | `'Penthouse Critical Path CSS Generator'` | specify which user agent string when loading the page |
70| customPageHeaders | `object` | | Set extra http headers to be sent with the request for the url. |
71| strict | `boolean` | `false` | Make Penthouse throw on errors parsing the original CSS. Legacy option, not recommended. |
72
73## Troubleshooting
74
75
76### Enable debug logging
77Logging is done via the [`debug`](https://github.com/visionmedia/debug) module under the `penthouse` namespace. You can view more about the logging on their [documentation](https://github.com/visionmedia/debug#usage).
78
79```sh
80# Basic verbose logging for all components
81env DEBUG="penthouse,penthouse:*" node script.js
82```
83
84### Not working on Linux
85Install missing dependencies to get the headless Chrome to run:
86
87```
88sudo apt-get install libnss3
89```
90You might possibly need an even longer list of deps, depending on your dist,
91see [this answer](https://github.com/GoogleChrome/puppeteer/issues/404#issuecomment-323555784)
92
93### Problems with generated CSS
94
95A good first step can be to test your url + css in the hosted critical path css generator, to determine whether the problem
96is with the input your passing (css + url), or with your local setup:
97https://jonassebastianohlsson.com/criticalpathcssgenerator
98
99#### Unstyled content showing when using the critical css
100
101If 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:
102
103##### Your page contains dynamic or JS injected/activated content.
104Generally you have to ensure that all elements you want styled in the critical css appears (visible) in the HTML of your page (with Javascript disabled). The first render of your page, the one critical css helps make much faster, happens _before_ JS has loaded, which is why Penthouse runs with JavaScript disabled. So if your html is essentially empty, or your real content is hidden before a loading spinner or similar you have to adress this before you can get the performance benefits of using critical css.
105
106If 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.
107
108##### Early DOM content moved out of critical viewport via CSS
109This 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).
110Solution: 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.
111
112#### Special glyphs not showing/showing incorrectly
113
114Problems 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: `\2192` (f.e. `content: '\2192';`)
115
116[npm-image]: https://badge.fury.io/js/penthouse.svg
117[npm-url]: https://npmjs.org/package/penthouse
118
119[travis-url]: https://travis-ci.org/pocketjoso/penthouse
120[travis-image]: https://secure.travis-ci.org/pocketjoso/penthouse.svg?branch=master
121
122[dlcounter-image]: https://img.shields.io/npm/dm/penthouse.svg?style=flat