From 9887209624d50365555342fbcba3a0e614cc6dcf Mon Sep 17 00:00:00 2001 From: stereobooster Date: Wed, 8 Aug 2018 21:12:26 +0200 Subject: [PATCH 1/2] Add option timeoutAsWarning --- README.md | 1 + src/run.js | 10 ++++++++-- 2 files changed, 9 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 8b2e17a8..7b05451a 100644 --- a/README.md +++ b/README.md @@ -176,6 +176,7 @@ key is `urls`. Other optional options are: of strings for headless Chrome](https://peter.sh/experiments/chromium-command-line-switches/). * `cssoOptions` - CSSO compress function [options](https://github.com/css/csso#compressast-options) * `timeout` - Maximum navigation time in milliseconds, defaults to 30 seconds, pass 0 to disable timeout. +* `timeoutAsWarning` - This is workaround for the [bug in puppeteer](https://github.com/GoogleChrome/puppeteer/issues/1908#issuecomment-390214976), when there are no open connections but puppeteer reports it as timeout anyway. If you set it to true minimalcss will report timeouts as warnings instead of rejection. Take a note real timeouts still be reported as errors. ## Warnings diff --git a/src/run.js b/src/run.js index 90cce914..3a8b8ed7 100644 --- a/src/run.js +++ b/src/run.js @@ -128,7 +128,6 @@ const processPage = ({ const tracker = createTracker(page); const safeReject = error => { if (fulfilledPromise) return; - fulfilledPromise = true; if (error.message.startsWith('Navigation Timeout Exceeded')) { const urls = tracker.urls(); if (urls.length > 1) { @@ -137,8 +136,15 @@ const processPage = ({ )}`; } else if (urls.length > 0) { error.message += `\nFor ${urls[0]}`; + } else if (options.timeoutAsWarning) { + // This is workaround for the bug in puppeter, when there are + // no open connections but puppeteer reports it as timeout anyway. + // https://github.com/GoogleChrome/puppeteer/issues/1908#issuecomment-390214976 + console.warn(error.message); + return; } } + fulfilledPromise = true; tracker.dispose(); reject(error); }; @@ -339,7 +345,7 @@ const processPage = ({ /** * - * @param {{ urls: Array, debug: boolean, loadimages: boolean, skippable: function, browser: any, userAgent: string, withoutjavascript: boolean, viewport: any, puppeteerArgs: Array, cssoOptions: Object }} options + * @param {{ urls: Array, debug: boolean, loadimages: boolean, skippable: function, browser: any, userAgent: string, withoutjavascript: boolean, viewport: any, puppeteerArgs: Array, cssoOptions: Object, timeoutAsWarning?: boolean }} options * @return Promise<{ finalCss: string, stylesheetContents: { [key: string]: string } }> */ const minimalcss = async options => { From 849c5ccfa0e595ecaa415cad6d0e250479211e8c Mon Sep 17 00:00:00 2001 From: stereobooster Date: Wed, 8 Aug 2018 22:11:54 +0200 Subject: [PATCH 2/2] I realised timeoutAsWarning will not work --- src/run.js | 22 +++++++++++++++------- 1 file changed, 15 insertions(+), 7 deletions(-) diff --git a/src/run.js b/src/run.js index 3a8b8ed7..6191a794 100644 --- a/src/run.js +++ b/src/run.js @@ -136,12 +136,6 @@ const processPage = ({ )}`; } else if (urls.length > 0) { error.message += `\nFor ${urls[0]}`; - } else if (options.timeoutAsWarning) { - // This is workaround for the bug in puppeter, when there are - // no open connections but puppeteer reports it as timeout anyway. - // https://github.com/GoogleChrome/puppeteer/issues/1908#issuecomment-390214976 - console.warn(error.message); - return; } } fulfilledPromise = true; @@ -291,7 +285,21 @@ const processPage = ({ // Second, goto the page and evaluate it with JavaScript. // The 'waitUntil' option determines how long we wait for all // possible assets to load. - response = await page.goto(pageUrl, { waitUntil: 'networkidle0' }); + try { + response = await page.goto(pageUrl, { waitUntil: 'networkidle0' }); + } catch (e) { + if ( + options.timeoutAsWarning && + e.message && + e.message.startsWith('Navigation Timeout Exceeded') && + tracker.urls().length === 0 + ) { + // This is workaround for the bug in puppeter, when there are + // no open connections but puppeteer reports it as timeout anyway. + // https://github.com/GoogleChrome/puppeteer/issues/1908#issuecomment-390214976 + console.warn(e.message); + } + } if (!isOk(response)) { return safeReject( new Error(`${response.status()} on ${pageUrl} (second time)`)