html-snapshots 0.19.0 → 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.github/workflows/verify.yml +2 -2
- package/HISTORY.md +42 -0
- package/README.md +92 -244
- package/examples/README.md +149 -0
- package/examples/custom/package-lock.json +1090 -9
- package/examples/custom/package.json +1 -1
- package/examples/debug-phantomjs/package.json +1 -1
- package/examples/debug-phantomjs/snapshot.js +1 -0
- package/examples/debug-puppeteer/README.md +18 -0
- package/examples/debug-puppeteer/package-lock.json +2804 -0
- package/examples/debug-puppeteer/package.json +12 -0
- package/examples/debug-puppeteer/snapshot.js +30 -0
- package/examples/html5rocks/package-lock.json +2 -2
- package/examples/html5rocks/package.json +1 -1
- package/examples/html5rocks/snapshot.js +0 -6
- package/examples/process-limit/package-lock.json +2 -2
- package/examples/process-limit/package.json +1 -1
- package/examples/simple-promise/package-lock.json +2 -2
- package/examples/simple-promise/package.json +1 -1
- package/examples/sitemap-index/package-lock.json +2 -2
- package/examples/sitemap-index/package.json +1 -1
- package/examples/verbose/package-lock.json +3 -4
- package/examples/verbose/package.json +1 -1
- package/examples/verbose/snapshot.js +1 -0
- package/lib/html-snapshots.js +87 -4
- package/lib/input-generators/_base.js +7 -1
- package/lib/puppeteer/index.js +98 -0
- package/lib/puppeteer/removeScripts.js +8 -0
- package/package.json +6 -2
- package/test/mocha/browsers/puppeteer.js +106 -0
- package/test/mocha/browsers/server/index.html +9 -0
- package/test/mocha/browsers/test.js +11 -0
- package/test/mocha/html-snapshots/basics.js +60 -47
- package/test/mocha/html-snapshots/phantomjs-options.js +54 -60
- package/test/mocha/html-snapshots/process-limit.js +100 -92
- package/test/mocha/html-snapshots/puppeteer.js +47 -0
- package/test/mocha/html-snapshots/robots.js +137 -120
- package/test/mocha/html-snapshots/server/public/page-sitemap.xml +16 -0
- package/test/mocha/html-snapshots/sitemap-index.js +89 -80
- package/test/mocha/html-snapshots/sitemap.js +75 -31
- package/test/mocha/html-snapshots/snapshot-scripts.js +124 -120
- package/test/mocha/html-snapshots/test.js +11 -2
- package/test/mocha/html-snapshots/use-jquery.js +65 -64
- package/test/mocha/html-snapshots/utils.js +29 -29
|
@@ -0,0 +1,149 @@
|
|
|
1
|
+
# Examples
|
|
2
|
+
|
|
3
|
+
+ [Per-page Selectors](#example---per-page-selectors-and-timeouts)
|
|
4
|
+
+ [Per-page Output Paths](#example---per-page-special-output-paths)
|
|
5
|
+
+ [Per-page jQuery](#example---per-page-selectors-and-jquery)
|
|
6
|
+
+ [Array Input](#example---array)
|
|
7
|
+
+ [More Array Input](./simple-promise/)
|
|
8
|
+
+ [Array Input DRY](./html5rocks/)
|
|
9
|
+
+ [Sitemap Index](./sitemap-index/)
|
|
10
|
+
+ [Process Limit](./process-limit/)
|
|
11
|
+
+ [Script Removal](#example---remote-robotstxt-remove-script-tags-from-html-snapshots)
|
|
12
|
+
+ [Custom Filters](./custom/)
|
|
13
|
+
+ [Debug PhantomJS w/Verbose Output](./verbose/)
|
|
14
|
+
+ [Debug PhantomJS w/Attach](./debug-phantomjs/)
|
|
15
|
+
+ [Debug Puppeteer in devtools](./debug-puppeteer/)
|
|
16
|
+
|
|
17
|
+
### Example - Per page selectors and timeouts
|
|
18
|
+
```javascript
|
|
19
|
+
const htmlSnapshots = require('html-snapshots');
|
|
20
|
+
htmlSnapshots.run({
|
|
21
|
+
input: 'sitemap',
|
|
22
|
+
source: 'https://host.domain/sitemap.xml',
|
|
23
|
+
outputDir: './snapshots',
|
|
24
|
+
outputDirClean: true,
|
|
25
|
+
selector: {
|
|
26
|
+
'https://host.domain/': '#home-content',
|
|
27
|
+
'__default': '#dynamic-content'
|
|
28
|
+
},
|
|
29
|
+
timeout: {
|
|
30
|
+
'https://host.domain/superslowpage/': 20000,
|
|
31
|
+
'__default': 10000
|
|
32
|
+
}
|
|
33
|
+
})
|
|
34
|
+
.then(completed => {
|
|
35
|
+
// completed is an array of full file paths to the completed snapshots.
|
|
36
|
+
})
|
|
37
|
+
.catch(error => {
|
|
38
|
+
// error is an Error instance.
|
|
39
|
+
// error.completed is an array of snapshot file paths that were completed.
|
|
40
|
+
// error.notCompleted is an array of file paths that did NOT complete.
|
|
41
|
+
});
|
|
42
|
+
```
|
|
43
|
+
This reads the urls from your sitemap.xml and produces snapshots in the ./snapshots directory. In this example, a selector named "#dynamic-content" appears in all pages across the site except the home page, where "#home-content" appears \(the appearance of a selector in the output triggers the snapshot\). Finally, a default timeout of 10000 ms is set on all pages except http://https://host.domain/superslowpage/, where it waits 20000 ms.
|
|
44
|
+
|
|
45
|
+
### Example - Per page special output paths
|
|
46
|
+
```javascript
|
|
47
|
+
const htmlSnapshots = require('html-snapshots');
|
|
48
|
+
htmlSnapshots.run({
|
|
49
|
+
input: 'sitemap',
|
|
50
|
+
source: 'https://host.domain/sitemap.xml',
|
|
51
|
+
outputDir: './snapshots',
|
|
52
|
+
outputDirClean: true,
|
|
53
|
+
outputPath: {
|
|
54
|
+
'https://host.domain/services/?page=1': 'services/page/1',
|
|
55
|
+
'https://host.domain/services/?page=2': 'services/page/2'
|
|
56
|
+
},
|
|
57
|
+
selector: '#dynamic-content'
|
|
58
|
+
})
|
|
59
|
+
.then(completed => {
|
|
60
|
+
// completed is an array of full file paths to the completed snapshots.
|
|
61
|
+
})
|
|
62
|
+
.catch(error => {
|
|
63
|
+
// error is an Error instance.
|
|
64
|
+
// error.completed is an array of snapshot file paths that were completed.
|
|
65
|
+
// error.notCompleted is an array of file paths that did NOT complete.
|
|
66
|
+
});
|
|
67
|
+
```
|
|
68
|
+
This example implies there are a couple of pages with query strings in sitemap.xml, and we don't want html-snapshots to create directories with query string characters in the names. We would also have a rewrite rule that reflects this same mapping when `_escaped_fragment_` shows up in the querystring of a request so we serve the snapshot from the appropriate directory.
|
|
69
|
+
|
|
70
|
+
### Example - Per page selectors and jQuery
|
|
71
|
+
```javascript
|
|
72
|
+
const htmlSnapshots = require('html-snapshots');
|
|
73
|
+
htmlSnapshots.run({
|
|
74
|
+
source: '/path/to/robots.txt',
|
|
75
|
+
hostname: 'mysite.com',
|
|
76
|
+
outputDir: './snapshots',
|
|
77
|
+
outputDirClean: true,
|
|
78
|
+
browser: 'phantomjs',
|
|
79
|
+
selector: {
|
|
80
|
+
'__default': '#dynamic-content',
|
|
81
|
+
'/jqpage': 'A-Selector-Not-Supported-By-querySelector'
|
|
82
|
+
},
|
|
83
|
+
useJQuery: {
|
|
84
|
+
'/jqpage': true,
|
|
85
|
+
'__default': false
|
|
86
|
+
}
|
|
87
|
+
})
|
|
88
|
+
.then(completed => {
|
|
89
|
+
// completed is an array of full file paths to the completed snapshots.
|
|
90
|
+
})
|
|
91
|
+
.catch(error => {
|
|
92
|
+
// error is an Error instance.
|
|
93
|
+
// error.completed is an array of snapshot file paths that were completed.
|
|
94
|
+
// error.notCompleted is an array of file paths that did NOT complete.
|
|
95
|
+
});
|
|
96
|
+
```
|
|
97
|
+
This reads the urls from your robots.txt and produces snapshots in the ./snapshots directory. In this example, a selector named "#dynamic-content" appears in all pages across the site except in "/jqpage", where a selector not supported by [querySelector](https://developer.mozilla.org/en-US/docs/Web/API/document.querySelector) is used. Further, "/jqpage" loads jQuery itself \(required\). All the other pages don't need to use special selectors, so the default is set to `false`. Notice that since a robots.txt input is used, full URLs are **not** used to match selectors. Instead, paths \(and QueryStrings and any Hashes\) are used, just as specified in the robots.txt file itself.
|
|
98
|
+
|
|
99
|
+
### Example - Array
|
|
100
|
+
```javascript
|
|
101
|
+
const htmlSnapshots = require('html-snapshots');
|
|
102
|
+
htmlSnapshots.run({
|
|
103
|
+
input: 'array',
|
|
104
|
+
source: ['http://mysite.com', 'http://mysite.com/contact', 'http://mysite.com:82/special'],
|
|
105
|
+
outputDir: './snapshots',
|
|
106
|
+
outputDirClean: true,
|
|
107
|
+
selector: '#dynamic-content'
|
|
108
|
+
})
|
|
109
|
+
.then(completed => {
|
|
110
|
+
// completed is an array of full file paths to the completed snapshots.
|
|
111
|
+
})
|
|
112
|
+
.catch(error => {
|
|
113
|
+
// error is an Error instance.
|
|
114
|
+
// error.completed is an array of snapshot file paths that were completed.
|
|
115
|
+
// error.notCompleted is an array of file paths that did NOT complete.
|
|
116
|
+
});
|
|
117
|
+
```
|
|
118
|
+
Generates snapshots for "/", "/contact", and "/special" from mysite.com. "/special" uses port 82. All use http protocol. Array input can be powerful, check out a [simple example](/examples/simple-promise), or a more [complex example](/examples/html5rocks).
|
|
119
|
+
|
|
120
|
+
### Example - Remote robots.txt, remove script tags from html snapshots
|
|
121
|
+
```javascript
|
|
122
|
+
const assert = require('assert');
|
|
123
|
+
const fs = require('fs');
|
|
124
|
+
const htmlSnapshots = require('html-snapshots');
|
|
125
|
+
|
|
126
|
+
htmlSnapshots.run({
|
|
127
|
+
source: 'http://localhost/robots.txt',
|
|
128
|
+
outputDir: './snapshots',
|
|
129
|
+
outputDirClean: true,
|
|
130
|
+
selector: '#dynamic-content',
|
|
131
|
+
snapshotScript: {
|
|
132
|
+
script: 'removeScripts'
|
|
133
|
+
}
|
|
134
|
+
})
|
|
135
|
+
.then(completed => {
|
|
136
|
+
completed.forEach(snapshotFile => {
|
|
137
|
+
const content = fs.readFileSync(snapshotFile, { encoding: 'utf8'});
|
|
138
|
+
assert.equal(false, /<script\b[^<]*(?:(?!<\/script>)<[^<]*)*<\/script>/gi.test(content));
|
|
139
|
+
});
|
|
140
|
+
// It didn't throw b/c there are no script tags in the html snapshots
|
|
141
|
+
console.log('stripped all script tags as expected');
|
|
142
|
+
})
|
|
143
|
+
.catch(error => {
|
|
144
|
+
// error is an Error instance.
|
|
145
|
+
// error.completed is an array of snapshot file paths that were completed.
|
|
146
|
+
// error.notCompleted is an array of file paths that did NOT complete.
|
|
147
|
+
});
|
|
148
|
+
```
|
|
149
|
+
Removes all script tags from the output of the html snapshot. Custom filters are also supported, see the customFilter Example in the explanation of the `snapshotScript` option. Also, check out the concrete [example](/examples/custom).
|