1 | # grunt-check-pages
|
2 |
|
3 | > Grunt task that checks various aspects of a web page for correctness.
|
4 |
|
5 | [![npm version][npm-image]][npm-url]
|
6 | [![GitHub tag][github-tag-image]][github-tag-url]
|
7 | [![Build status][travis-image]][travis-url]
|
8 | [![Coverage][coveralls-image]][coveralls-url]
|
9 | [![License][license-image]][license-url]
|
10 |
|
11 |
|
12 | ## Getting Started
|
13 |
|
14 | This plugin requires Grunt `~0.4.4`.
|
15 |
|
16 | If you haven't used [Grunt](http://gruntjs.com/) before, be sure to check out the [Getting Started](http://gruntjs.com/getting-started) guide, as it explains how to create a [Gruntfile](http://gruntjs.com/sample-gruntfile) as well as install and use Grunt plugins. Once you're familiar with that process, you may install this plugin with this command:
|
17 |
|
18 | ```shell
|
19 | npm install grunt-check-pages --save-dev
|
20 | ```
|
21 |
|
22 | Once the plugin has been installed, it may be enabled inside your Gruntfile with this line of JavaScript:
|
23 |
|
24 | ```js
|
25 | grunt.loadNpmTasks('grunt-check-pages');
|
26 | ```
|
27 |
|
28 | (*For similar functionality without a Grunt dependency, please see the [`check-pages`](https://www.npmjs.com/package/check-pages) package.*)
|
29 |
|
30 |
|
31 | ## The "checkPages" task
|
32 |
|
33 |
|
34 | ### Overview
|
35 |
|
36 | An important aspect of creating a web site is validating the structure, content, and configuration of the site's pages. The `checkPages` task provides an easy way to integrate this testing into your normal Grunt workflow.
|
37 |
|
38 | By providing a list of pages to scan, the task can:
|
39 |
|
40 | * Validate each page is accessible
|
41 | * Validate all links point to accessible content (similar to the [W3C Link Checker](http://validator.w3.org/checklink))
|
42 | * Validate links with query string [file hashes](http://en.wikipedia.org/wiki/List_of_hash_functions) have the expected content
|
43 | * Validate page structure for XHTML compliance (akin to the [W3C Markup Validation Service](http://validator.w3.org/))
|
44 | * Validate a page's response time is below some threshold
|
45 | * Validate a page takes advantage of [caching for better performance](https://developers.google.com/speed/docs/insights/LeverageBrowserCaching)
|
46 | * Validate a page takes advantage of [compression for better performance](https://developers.google.com/speed/docs/insights/EnableCompression)
|
47 |
|
48 |
|
49 | ### Usage
|
50 |
|
51 | In your project's Gruntfile, add a section named `checkPages` to the data object passed into `grunt.initConfig()`.
|
52 | The following example includes all supported options:
|
53 |
|
54 | ```js
|
55 | grunt.initConfig({
|
56 | checkPages: {
|
57 | development: {
|
58 | options: {
|
59 | pageUrls: [
|
60 | 'http://localhost:8080/',
|
61 | 'http://localhost:8080/blog',
|
62 | 'http://localhost:8080/about.html'
|
63 | ],
|
64 | checkLinks: true,
|
65 | onlySameDomain: true,
|
66 | queryHashes: true,
|
67 | noRedirects: true,
|
68 | noLocalLinks: true,
|
69 | noEmptyFragments: true,
|
70 | linksToIgnore: [
|
71 | 'http://localhost:8080/broken.html'
|
72 | ],
|
73 | checkXhtml: true,
|
74 | checkCaching: true,
|
75 | checkCompression: true,
|
76 | maxResponseTime: 200,
|
77 | userAgent: 'custom-user-agent/1.2.3',
|
78 | summary: true
|
79 | }
|
80 | },
|
81 | production: {
|
82 | options: {
|
83 | pageUrls: [
|
84 | 'http://example.com/',
|
85 | 'http://example.com/blog',
|
86 | 'http://example.com/about.html'
|
87 | ],
|
88 | checkLinks: true,
|
89 | maxResponseTime: 500
|
90 | }
|
91 | }
|
92 | }
|
93 | });
|
94 | ```
|
95 |
|
96 |
|
97 | ### Options
|
98 |
|
99 | #### pageUrls
|
100 |
|
101 | Type: `Array` of `String`
|
102 | Default value: `undefined`
|
103 | *Required*
|
104 |
|
105 | `pageUrls` specifies a list of URLs for web pages the task will check.
|
106 |
|
107 | URLs must be absolute and can point to local or remote content. The `pageUrls` array can be empty, but must be present.
|
108 |
|
109 | To store the list outside `Gruntfile.js`, read the array from a JSON file instead: `pageUrls: grunt.file.readJSON('pageUrls.json')`.
|
110 |
|
111 | #### checkLinks
|
112 |
|
113 | Type: `Boolean`
|
114 | Default value: `false`
|
115 |
|
116 | Enabling `checkLinks` causes each link in a page to be checked for validity (i.e., an [HTTP HEAD or GET request](http://en.wikipedia.org/wiki/Hypertext_Transfer_Protocol#Request_methods) returns success).
|
117 |
|
118 | For efficiency, a `HEAD` request is made first and a successful result validates the link. Because some web servers misbehave, a failed `HEAD` request is followed by a `GET` request to definitively validate the link.
|
119 |
|
120 | The following element/attribute pairs are used to identify links:
|
121 |
|
122 | * `a`/`href`
|
123 | * `area`/`href`
|
124 | * `audio`/`src`
|
125 | * `embed`/`src`
|
126 | * `iframe`/`src`
|
127 | * `img`/`src`
|
128 | * `input`/`src`
|
129 | * `link`/`href`
|
130 | * `object`/`data`
|
131 | * `script`/`src`
|
132 | * `source`/`src`
|
133 | * `track`/`src`
|
134 | * `video`/`src`
|
135 |
|
136 | #### onlySameDomain
|
137 |
|
138 | Type: `Boolean`
|
139 | Default value: `false`
|
140 | Used by: `checkLinks`
|
141 |
|
142 | Set this option to `true` to block the checking of links on different domains than the referring page.
|
143 |
|
144 | This can be useful during development when external sites aren't changing and don't need to be checked.
|
145 |
|
146 | #### queryHashes
|
147 |
|
148 | Type: `Boolean`
|
149 | Default value: `false`
|
150 | Used by: `checkLinks`
|
151 |
|
152 | Set this option to `true` to verify links with [file hashes](http://en.wikipedia.org/wiki/List_of_hash_functions) in the query string point to content that hashes to the expected value.
|
153 |
|
154 | Query hashes can be used to [invalidate cached responses](https://developers.google.com/web/fundamentals/performance/optimizing-content-efficiency/http-caching#invalidating-and-updating-cached-responses) when [leveraging browser caching](https://developers.google.com/speed/docs/insights/LeverageBrowserCaching) via long cache lifetimes.
|
155 |
|
156 | Supported hash functions are:
|
157 |
|
158 | * image.png?[crc32](http://en.wikipedia.org/wiki/Cyclic_redundancy_check)=e4f013b5
|
159 | * styles.css?[md5](http://en.wikipedia.org/wiki/MD5)=4f47458e34bc855a46349c1335f58cc3
|
160 | * archive.zip?[sha1](http://en.wikipedia.org/wiki/SHA-1)=9511fa1a787d021bdf3aa9538029a44209fb5c4c
|
161 |
|
162 | #### noRedirects
|
163 |
|
164 | Type: `Boolean`
|
165 | Default value: `false`
|
166 | Used by: `checkLinks`
|
167 |
|
168 | Set this option to `true` to fail the task if any [HTTP redirects](http://en.wikipedia.org/wiki/URL_redirection) are encountered.
|
169 |
|
170 | This can be useful to ensure outgoing links are to the content's canonical location.
|
171 |
|
172 | #### noLocalLinks
|
173 |
|
174 | Type: `Boolean`
|
175 | Default value: `false`
|
176 | Used by: `checkLinks`
|
177 |
|
178 | Set this option to `true` to fail the task if any links to [`localhost`](http://en.wikipedia.org/wiki/Localhost) are encountered.
|
179 |
|
180 | This is useful to detect temporary links that may work during development but would fail when deployed.
|
181 |
|
182 | The list of host names recognized as `localhost` are:
|
183 |
|
184 | * localhost
|
185 | * 127.0.0.1 (and the rest of the `127.0.0.0/8` address block)
|
186 | * ::1 (and its expanded forms)
|
187 |
|
188 | #### noEmptyFragments
|
189 |
|
190 | Type: `Boolean`
|
191 | Default value: `false`
|
192 | Used by: `checkLinks`
|
193 |
|
194 | Set this option to `true` to fail the task if any links contain an empty [fragment identifier (hash)](http://en.wikipedia.org/wiki/Fragment_identifier) such as `<a href="#">`.
|
195 |
|
196 | This is useful to identify placeholder links that haven't been updated.
|
197 |
|
198 | #### linksToIgnore
|
199 |
|
200 | Type: `Array` of `String`
|
201 | Default value: `undefined`
|
202 | Used by: `checkLinks`
|
203 |
|
204 | `linksToIgnore` specifies a list of URLs that should be ignored by the link checker.
|
205 |
|
206 | This is useful for links that are not accessible during development or known to be unreliable.
|
207 |
|
208 | #### checkXhtml
|
209 |
|
210 | Type: `Boolean`
|
211 | Default value: `false`
|
212 |
|
213 | Enabling `checkXhtml` attempts to parse each URL's content as [XHTML](http://en.wikipedia.org/wiki/XHTML) and fails if there are any structural errors.
|
214 |
|
215 | This can be useful to ensure a page's structure is well-formed and unambiguous for browsers.
|
216 |
|
217 | #### checkCaching
|
218 |
|
219 | Type: `Boolean`
|
220 | Default value: `false`
|
221 |
|
222 | Enabling `checkCaching` verifies the HTTP [`Cache-Control`](https://tools.ietf.org/html/rfc2616#section-14.9) and [`ETag`](https://tools.ietf.org/html/rfc2616#section-14.19) response headers are present and valid.
|
223 |
|
224 | This is useful to ensure a page makes use of browser caching for better performance.
|
225 |
|
226 | #### checkCompression
|
227 |
|
228 | Type: `Boolean`
|
229 | Default value: `false`
|
230 |
|
231 | Enabling `checkCompression` verifies the HTTP [`Content-Encoding`](https://tools.ietf.org/html/rfc2616#section-14.11) response header is present and valid.
|
232 |
|
233 | This is useful to ensure a page makes use of compression for better performance.
|
234 |
|
235 | #### maxResponseTime
|
236 |
|
237 | Type: `Number`
|
238 | Default value: `undefined`
|
239 |
|
240 | `maxResponseTime` specifies the maximum amount of time (in milliseconds) a page request can take to finish downloading.
|
241 |
|
242 | Requests that take more time will trigger a failure (but are still checked for other issues).
|
243 |
|
244 | #### userAgent
|
245 |
|
246 | Type: `String`
|
247 | Default value: `grunt-check-pages/x.y.z`
|
248 |
|
249 | `userAgent` specifies the value of the HTTP [`User-Agent`](https://tools.ietf.org/html/rfc2616#section-14.43) header sent with all page/link requests.
|
250 |
|
251 | This is useful for pages that alter their behavior based on the user agent. Setting the value `null` omits the `User-Agent` header entirely.
|
252 |
|
253 | #### summary
|
254 |
|
255 | Type: `Boolean`
|
256 | Default value: `false`
|
257 |
|
258 | Enabling the `summary` option logs a summary of each issue found after all checks have completed.
|
259 |
|
260 | This makes it easy to pick out failures when running tests against many pages.
|
261 |
|
262 |
|
263 | ## Release History
|
264 |
|
265 | * 0.1.0 - Initial release, support for `checkLinks` and `checkXhtml`.
|
266 | * 0.1.1 - Tweak README for better formatting.
|
267 | * 0.1.2 - Support page-only mode (no link or XHTML checks), show response time for requests.
|
268 | * 0.1.3 - Support `maxResponseTime` option, buffer all page responses, add "no-cache" header to requests.
|
269 | * 0.1.4 - Support `checkCaching` and `checkCompression` options, improve error handling, use [`gruntMock`](https://www.npmjs.com/package/gruntmock).
|
270 | * 0.1.5 - Support `userAgent` option, weak entity tags, update `nock` dependency.
|
271 | * 0.2.0 - Support `noLocalLinks` option, rename `disallowRedirect` option to `noRedirects`, switch to [`ESLint`](http://eslint.org/), update `superagent` and `nock` dependencies.
|
272 | * 0.3.0 - Support `queryHashes` option for CRC-32/MD5/SHA-1, update `superagent` dependency.
|
273 | * 0.4.0 - Rename `onlySameDomainLinks` option to `onlySameDomain`, fix handling of redirected page links, use page order for links, update all dependencies.
|
274 | * 0.5.0 - Show location of redirected links with `noRedirects` option, switch to `crc-hash` dependency.
|
275 | * 0.6.0 - Support `summary` option, update `crc-hash`, `grunt-eslint`, `nock` dependencies.
|
276 | * 0.6.1 - Add badges for automated build and coverage info to README (along with npm, GitHub, and license).
|
277 | * 0.6.2 - Switch from `superagent` to `request`, update `grunt-eslint` and `nock` dependencies.
|
278 | * 0.7.0 - Move task implementation into reusable `check-pages` package.
|
279 | * 0.7.1 - Fix misreporting of "Bad link" for redirected links when noRedirects enabled.
|
280 | * 0.8.0 - Suppress redundant link checks, support `noEmptyFragments` option, update dependencies.
|
281 |
|
282 |
|
283 | [npm-image]: https://img.shields.io/npm/v/grunt-check-pages.svg
|
284 | [npm-url]: https://www.npmjs.com/package/grunt-check-pages
|
285 | [github-tag-image]: https://img.shields.io/github/tag/DavidAnson/grunt-check-pages.svg
|
286 | [github-tag-url]: https://github.com/DavidAnson/grunt-check-pages
|
287 | [travis-image]: https://img.shields.io/travis/DavidAnson/grunt-check-pages.svg
|
288 | [travis-url]: https://travis-ci.org/DavidAnson/grunt-check-pages
|
289 | [coveralls-image]: https://img.shields.io/coveralls/DavidAnson/grunt-check-pages.svg
|
290 | [coveralls-url]: https://coveralls.io/r/DavidAnson/grunt-check-pages
|
291 | [license-image]: https://img.shields.io/npm/l/grunt-check-pages.svg
|
292 | [license-url]: http://opensource.org/licenses/MIT
|