From 9ccfbfc9e82da93b99fa3df163d286bbf5e63794 Mon Sep 17 00:00:00 2001 From: Freek Gruntjes Date: Thu, 2 Apr 2020 17:14:16 +0200 Subject: [PATCH 1/4] Added ignore request error option --- README.md | 1 + src/run.js | 10 +++++++--- 2 files changed, 8 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index dedd5ec2..32d2588c 100644 --- a/README.md +++ b/README.md @@ -179,6 +179,7 @@ key is `urls`. Other optional options are: * `timeout` - Maximum navigation time in milliseconds, defaults to 30 seconds, pass 0 to disable timeout. * `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`. * `ignoreJSErrors` - By default, any JavaScript error encountered by puppeteer +* `ignoreRequestErrors` - When CSS files return 404 or another request error `minimalcss` will ignore this instead of throwing an error. will be thrown by `minimalcss`. If you know it's safe to ignore errors (for example, on third-party webpages), set this to `true`. * `styletags` - If set to `true`, on-page `` tags are parsed along with external stylesheets. By default, only external stylesheets are parsed. diff --git a/src/run.js b/src/run.js index 196a098a..fc557771 100644 --- a/src/run.js +++ b/src/run.js @@ -244,9 +244,13 @@ const processPage = ({ const responseUrl = response.url(); const resourceType = response.request().resourceType(); if (response.status() >= 400) { - return safeReject( - new Error(`${response.status()} on ${responseUrl}`) - ); + if (options.ignoreRequestErrors) { + skippedUrls.add(responseUrl); + } else { + return safeReject( + new Error(`${response.status()} on ${responseUrl}`) + ); + } } else if (response.status() >= 300 && response.status() !== 304) { // If the 'Location' header points to a relative URL, // convert it to an absolute URL. From 9d679b78ea9afc160579be7f1449d514832b2b89 Mon Sep 17 00:00:00 2001 From: Freek Gruntjes Date: Tue, 28 Jul 2020 12:39:01 +0200 Subject: [PATCH 2/4] Added test case for ignore request errors --- tests/examples/404css-ignore.css | 3 +++ tests/examples/404css-ignore.html | 11 +++++++++++ tests/main.test.js | 8 ++++++++ 3 files changed, 22 insertions(+) create mode 100644 tests/examples/404css-ignore.css create mode 100644 tests/examples/404css-ignore.html diff --git a/tests/examples/404css-ignore.css b/tests/examples/404css-ignore.css new file mode 100644 index 00000000..3d9a2b2a --- /dev/null +++ b/tests/examples/404css-ignore.css @@ -0,0 +1,3 @@ +p { + color: red; +} diff --git a/tests/examples/404css-ignore.html b/tests/examples/404css-ignore.html new file mode 100644 index 00000000..29adcad9 --- /dev/null +++ b/tests/examples/404css-ignore.html @@ -0,0 +1,11 @@ + + + + + + + + +

Only a p tag here.

+ + diff --git a/tests/main.test.js b/tests/main.test.js index ac5e51c9..f17e6ee0 100644 --- a/tests/main.test.js +++ b/tests/main.test.js @@ -100,6 +100,14 @@ test('handles 404 CSS file', async () => { } }); +test('ignore 404 CSS file when `ignoreRequestErrors` is enabled', async () => { + const { finalCss } = await runMinimalcss('404css', { + ignoreRequestErrors: true, + }); + + expect(finalCss).toEqual('p{color:red}'); +}); + test('media queries print removed', async () => { const { finalCss } = await runMinimalcss('media-queries-print'); expect(finalCss).toEqual(''); From c245443393c7dc7a42d4621780788b824540c14c Mon Sep 17 00:00:00 2001 From: Freek Gruntjes Date: Tue, 28 Jul 2020 12:41:18 +0200 Subject: [PATCH 3/4] Added `ignoreRequestErrors` to comment --- src/run.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/run.js b/src/run.js index fc557771..ccf4556b 100644 --- a/src/run.js +++ b/src/run.js @@ -409,7 +409,7 @@ const processPage = ({ /** * - * @param {{ urls: Array, debug: boolean, loadimages: boolean, skippable: function, browser: any, userAgent: string, withoutjavascript: boolean, viewport: any, puppeteerArgs: Array, cssoOptions: Object, ignoreCSSErrors?: boolean, ignoreJSErrors?: boolean, styletags?: boolean, enableServiceWorkers?: boolean, disableJavaScript?: boolean, whitelist?: Array }} options + * @param {{ urls: Array, debug: boolean, loadimages: boolean, skippable: function, browser: any, userAgent: string, withoutjavascript: boolean, viewport: any, puppeteerArgs: Array, cssoOptions: Object, ignoreCSSErrors?: boolean, ignoreJSErrors?: boolean, styletags?: boolean, enableServiceWorkers?: boolean, disableJavaScript?: boolean, whitelist?: Array, ignoreRequestErrors?: boolean }} options * @return Promise<{ finalCss: string, stylesheetContents: { [key: string]: string }, doms: Array }> */ const minimalcss = async (options) => { From 7f6421d8b7b57dafa18b1387a8acf9229db3fdc7 Mon Sep 17 00:00:00 2001 From: Freek Gruntjes Date: Tue, 28 Jul 2020 12:49:45 +0200 Subject: [PATCH 4/4] Fixed test ref for 404 ignore test --- tests/main.test.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/main.test.js b/tests/main.test.js index f17e6ee0..03b4d1ef 100644 --- a/tests/main.test.js +++ b/tests/main.test.js @@ -101,7 +101,7 @@ test('handles 404 CSS file', async () => { }); test('ignore 404 CSS file when `ignoreRequestErrors` is enabled', async () => { - const { finalCss } = await runMinimalcss('404css', { + const { finalCss } = await runMinimalcss('404css-ignore', { ignoreRequestErrors: true, });