diff --git a/README.md b/README.md index 69ab92a2..b86e0688 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..ccf4556b 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. @@ -405,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) => { 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..03b4d1ef 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-ignore', { + ignoreRequestErrors: true, + }); + + expect(finalCss).toEqual('p{color:red}'); +}); + test('media queries print removed', async () => { const { finalCss } = await runMinimalcss('media-queries-print'); expect(finalCss).toEqual('');