Skip to content

Commit fe10a9f

Browse files
authored
Added ignore request error option (#365)
* Added ignore request error option * Added test case for ignore request errors * Added `ignoreRequestErrors` to comment * Fixed test ref for 404 ignore test
1 parent d97c387 commit fe10a9f

File tree

5 files changed

+31
-4
lines changed

5 files changed

+31
-4
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,7 @@ key is `urls`. Other optional options are:
179179
* `timeout` - Maximum navigation time in milliseconds, defaults to 30 seconds, pass 0 to disable timeout.
180180
* `ignoreCSSErrors` - By default, any CSS parsing error throws an error in `minimalcss`. If you know it's safe to ignore (for example, third-party CSS resources), set this to `true`.
181181
* `ignoreJSErrors` - By default, any JavaScript error encountered by puppeteer
182+
* `ignoreRequestErrors` - When CSS files return 404 or another request error `minimalcss` will ignore this instead of throwing an error.
182183
will be thrown by `minimalcss`. If you know it's safe to ignore errors (for example, on
183184
third-party webpages), set this to `true`.
184185
* `styletags` - If set to `true`, on-page `<style>` tags are parsed along with external stylesheets. By default, only external stylesheets are parsed.

src/run.js

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -244,9 +244,13 @@ const processPage = ({
244244
const responseUrl = response.url();
245245
const resourceType = response.request().resourceType();
246246
if (response.status() >= 400) {
247-
return safeReject(
248-
new Error(`${response.status()} on ${responseUrl}`)
249-
);
247+
if (options.ignoreRequestErrors) {
248+
skippedUrls.add(responseUrl);
249+
} else {
250+
return safeReject(
251+
new Error(`${response.status()} on ${responseUrl}`)
252+
);
253+
}
250254
} else if (response.status() >= 300 && response.status() !== 304) {
251255
// If the 'Location' header points to a relative URL,
252256
// convert it to an absolute URL.
@@ -405,7 +409,7 @@ const processPage = ({
405409

406410
/**
407411
*
408-
* @param {{ urls: Array<string>, debug: boolean, loadimages: boolean, skippable: function, browser: any, userAgent: string, withoutjavascript: boolean, viewport: any, puppeteerArgs: Array<string>, cssoOptions: Object, ignoreCSSErrors?: boolean, ignoreJSErrors?: boolean, styletags?: boolean, enableServiceWorkers?: boolean, disableJavaScript?: boolean, whitelist?: Array<string> }} options
412+
* @param {{ urls: Array<string>, debug: boolean, loadimages: boolean, skippable: function, browser: any, userAgent: string, withoutjavascript: boolean, viewport: any, puppeteerArgs: Array<string>, cssoOptions: Object, ignoreCSSErrors?: boolean, ignoreJSErrors?: boolean, styletags?: boolean, enableServiceWorkers?: boolean, disableJavaScript?: boolean, whitelist?: Array<string>, ignoreRequestErrors?: boolean }} options
409413
* @return Promise<{ finalCss: string, stylesheetContents: { [key: string]: string }, doms: Array<object> }>
410414
*/
411415
const minimalcss = async (options) => {

tests/examples/404css-ignore.css

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
p {
2+
color: red;
3+
}

tests/examples/404css-ignore.html

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<!doctype html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="utf-8">
5+
<link href="404css-ignore.css" rel=stylesheet>
6+
<link href="404.css" rel=stylesheet>
7+
</head>
8+
<body>
9+
<p>Only a p tag here.</p>
10+
</body>
11+
</html>

tests/main.test.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,14 @@ test('handles 404 CSS file', async () => {
100100
}
101101
});
102102

103+
test('ignore 404 CSS file when `ignoreRequestErrors` is enabled', async () => {
104+
const { finalCss } = await runMinimalcss('404css-ignore', {
105+
ignoreRequestErrors: true,
106+
});
107+
108+
expect(finalCss).toEqual('p{color:red}');
109+
});
110+
103111
test('media queries print removed', async () => {
104112
const { finalCss } = await runMinimalcss('media-queries-print');
105113
expect(finalCss).toEqual('');

0 commit comments

Comments
 (0)