UNPKG

3.65 kBMarkdownView Raw
1# taki
2
3[![NPM version](https://img.shields.io/npm/v/taki.svg?style=flat)](https://npmjs.com/package/taki) [![NPM downloads](https://img.shields.io/npm/dm/taki.svg?style=flat)](https://npmjs.com/package/taki) [![CircleCI](https://circleci.com/gh/egoist/taki/tree/master.svg?style=shield)](https://circleci.com/gh/egoist/taki/tree/master) [![donate](https://img.shields.io/badge/$-donate-ff69b4.svg?maxAge=2592000&style=flat)](https://github.com/egoist/donate)
4
5## Install
6
7```bash
8npm i taki
9```
10
11Built on the top of Google's [Puppeteer](https://github.com/GoogleChrome/puppeteer), for a jsdom/chromy version please visit [here](https://github.com/egoist/taki/tree/jsdom-chromy).
12
13## Usage
14
15```js
16const taki = require('taki')
17
18// Prerender this page to static HTML
19// Wait for 1s since this page renders remote markdown file
20taki({ url: 'https://sao.js.org', wait: 1000 })
21.then(html => {
22 // serialized html string of target url
23 console.log(html)
24})
25```
26
27### Multiplate URLs
28
29```js
30taki([
31 { url: 'https://sao.js.org' },
32 { url: 'https://sao.js.org/#/create' }
33]).then(result => {
34 // Then the result will an array of html string
35})
36```
37
38### Manually take snapshot
39
40By default **taki** will take a snapshot of the URL when all resources are loaded, if you have control of the website's source code, you can disable that and manually call `window.snapshot`:
41
42```js
43taki({
44 url: 'http://my-web.com',
45 manually: true
46})
47```
48
49And in your website's source code:
50
51```diff
52fetchSomeData().then(data => {
53 this.setState({ data }, () => {
54+ window.snapshot && window.snapshot()
55 })
56})
57```
58
59Alternatively, choose your own method to invoke when your app is ready to return HTML:
60
61```js
62taki({
63 url: 'http://my-web.com',
64 manually: 'iamready'
65})
66```
67
68Then call `window.iamready()` instead of `window.snapshot()` in your app.
69
70### Wait
71
72Wait for specific timeout or a CSS selector to appear in dom.
73
74```js
75taki({
76 url,
77 // Wait for 3000 ms
78 wait: 3000,
79 // Or wait for <div class="comments"></div> to appear
80 wait: '.comments'
81})
82```
83
84This option will be ignored if [manually](#manually-take-snapshot) is set.
85
86### Minify
87
88Minify HTML.
89
90```js
91taki({
92 url,
93 minify: true
94})
95```
96
97### Filter resource
98
99We always abort network requests to following types of resource: `stylesheet` `image` `media` `font` since they're not required to render the page. In addtion, you can use `resourceFilter` option to abort specfic type of resource:
100
101```js
102taki({
103 url,
104 /**
105 * @param {Object} context
106 * @param {string} context.type - Resource type
107 * @see {@link https://github.com/GoogleChrome/puppeteer/blob/master/docs/api.md#requestresourcetype}
108 * @param {string} context.url - Resource URL
109 * @see {@link https://github.com/GoogleChrome/puppeteer/blob/master/docs/api.md#requesturl}
110 * @returns {boolean} Whether to load this resource
111 */
112 resourceFilter({ type, url }) {
113 // Return true to load the resource, false otherwise.
114 }
115})
116```
117
118You can also use `blockCrossOrigin: true` shortcut to block all cross-origin requests.
119
120## Contributing
121
1221. Fork it!
1232. Create your feature branch: `git checkout -b my-new-feature`
1243. Commit your changes: `git commit -am 'Add some feature'`
1254. Push to the branch: `git push origin my-new-feature`
1265. Submit a pull request :D
127
128
129## Author
130
131**taki** © [egoist](https://github.com/egoist), Released under the [MIT](./LICENSE) License.<br>
132Authored and maintained by egoist with help from contributors ([list](https://github.com/egoist/taki/contributors)).
133
134> [Website](https://egoist.sh) · GitHub [@egoist](https://github.com/egoist) · Twitter [@_egoistlily](https://twitter.com/_egoistlily)