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
|
8 | npm i taki
|
9 | ```
|
10 |
|
11 | Built 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
|
16 | const taki = require('taki')
|
17 |
|
18 | // Prerender this page to static HTML
|
19 | // Wait for 1s since this page renders remote markdown file
|
20 | taki({ 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
|
30 | taki([
|
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 |
|
40 | By 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
|
43 | taki({
|
44 | url: 'http://my-web.com',
|
45 | manually: true
|
46 | })
|
47 | ```
|
48 |
|
49 | And in your website's source code:
|
50 |
|
51 | ```diff
|
52 | fetchSomeData().then(data => {
|
53 | this.setState({ data }, () => {
|
54 | + window.snapshot && window.snapshot()
|
55 | })
|
56 | })
|
57 | ```
|
58 |
|
59 | Alternatively, choose your own method to invoke when your app is ready to return HTML:
|
60 |
|
61 | ```js
|
62 | taki({
|
63 | url: 'http://my-web.com',
|
64 | manually: 'iamready'
|
65 | })
|
66 | ```
|
67 |
|
68 | Then call `window.iamready()` instead of `window.snapshot()` in your app.
|
69 |
|
70 | ### Wait
|
71 |
|
72 | Wait for specific timeout or a CSS selector to appear in dom.
|
73 |
|
74 | ```js
|
75 | taki({
|
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 |
|
84 | This option will be ignored if [manually](#manually-take-snapshot) is set.
|
85 |
|
86 | ### Minify
|
87 |
|
88 | Minify HTML.
|
89 |
|
90 | ```js
|
91 | taki({
|
92 | url,
|
93 | minify: true
|
94 | })
|
95 | ```
|
96 |
|
97 | ### Filter resource
|
98 |
|
99 | We 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
|
102 | taki({
|
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 |
|
118 | You can also use `blockCrossOrigin: true` shortcut to block all cross-origin requests.
|
119 |
|
120 | ## Contributing
|
121 |
|
122 | 1. Fork it!
|
123 | 2. Create your feature branch: `git checkout -b my-new-feature`
|
124 | 3. Commit your changes: `git commit -am 'Add some feature'`
|
125 | 4. Push to the branch: `git push origin my-new-feature`
|
126 | 5. Submit a pull request :D
|
127 |
|
128 |
|
129 | ## Author
|
130 |
|
131 | **taki** © [egoist](https://github.com/egoist), Released under the [MIT](./LICENSE) License.<br>
|
132 | Authored 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)
|