UNPKG

4.29 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## Sponsor
6
7<a href="https://oxylabs.io?utm_source=egoist&utm_medium=cpc&utm_campaign=egoist_github_sponsor&adgroupid=20220222">
8<img width="400" alt="Oxylabs: Innovative Proxy Service to Gather Data at Scale" src="https://user-images.githubusercontent.com/8784712/155142247-17264699-1bc8-4b52-8236-8b9ef7b365e2.png" />
9</a>
10
11## Install
12
13```bash
14npm i taki
15```
16
17Built 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).
18
19## Usage
20
21```js
22const { request } = require('taki')
23
24// Prerender this page to static HTML
25// Wait for 1s since this page renders remote markdown file
26request({ url: 'https://sao.js.org', wait: 1000 }).then((html) => {
27 // serialized html string of target url
28 console.log(html)
29})
30```
31
32**NOTE**: You need to call `cleanup` when you no longer use `request`:
33
34```js
35import { cleanup } from 'taki'
36
37// After fetching..
38cleanup()
39```
40
41### Custom html selector
42
43By default it returns the html for the entire document, but you can specify a selector to get the html for a specific element.
44
45```js
46const { request } = require('taki')
47
48request({ url: 'https://example.com', htmlSelector: '.some-element' }).then(
49 (html) => {
50 console.log(html)
51 }
52)
53```
54
55### Manually take snapshot
56
57By 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`:
58
59```js
60request({
61 url: 'http://my-web.com',
62 manually: true,
63})
64```
65
66And in your website's source code:
67
68```diff
69fetchSomeData().then(data => {
70 this.setState({ data }, () => {
71+ window.snapshot && window.snapshot()
72 })
73})
74```
75
76Alternatively, choose your own method to invoke when your app is ready to return HTML:
77
78```js
79request({
80 url: 'http://my-web.com',
81 manually: 'iamready',
82})
83```
84
85Then call `window.iamready()` instead of `window.snapshot()` in your app.
86
87### Wait
88
89Wait for specific timeout or a CSS selector to appear in dom.
90
91```js
92request({
93 url,
94 // Wait for 3000 ms
95 wait: 3000,
96 // Or wait for <div class="comments"></div> to appear
97 wait: '.comments',
98})
99```
100
101This option will be ignored if [manually](#manually-take-snapshot) is set.
102
103### Minify
104
105Minify HTML.
106
107```js
108request({
109 url,
110 minify: true,
111})
112```
113
114### Filter resource
115
116We 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:
117
118```js
119request({
120 url,
121 /**
122 * @param {Object} context
123 * @param {string} context.type - Resource type
124 * @see {@link https://github.com/GoogleChrome/puppeteer/blob/master/docs/api.md#requestresourcetype}
125 * @param {string} context.url - Resource URL
126 * @see {@link https://github.com/GoogleChrome/puppeteer/blob/master/docs/api.md#requesturl}
127 * @returns {boolean} Whether to load this resource
128 */
129 resourceFilter({ type, url }) {
130 // Return true to load the resource, false otherwise.
131 },
132})
133```
134
135You can also use `blockCrossOrigin: true` shortcut to block all cross-origin requests.
136
137## Contributing
138
1391. Fork it!
1402. Create your feature branch: `git checkout -b my-new-feature`
1413. Commit your changes: `git commit -am 'Add some feature'`
1424. Push to the branch: `git push origin my-new-feature`
1435. Submit a pull request :D
144
145## Author
146
147**taki** © [egoist](https://github.com/egoist), Released under the [MIT](./LICENSE) License.<br>
148Authored and maintained by egoist with help from contributors ([list](https://github.com/egoist/taki/contributors)).
149
150> [Website](https://egoist.sh) · GitHub [@egoist](https://github.com/egoist) · Twitter [@\_egoistlily](https://twitter.com/_egoistlily)