peterbe / minimalcss

Extract the minimal CSS used in a set of URLs with puppeteer

Home Page:https://minimalcss.app/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Bad error when using anchor hash in URL

peterbe opened this issue · comments

E.g.

const minimalcss = require('./index')
minimalcss.minimize({
  urls: ['https://www.peterbe.com#other-important-security-considerations']
  // urls: ['https://www.peterbe.com']
}).then(result => {
  console.log('OUTPUT', result.finalCss.length, result.finalCss)
}).catch(error => {
  console.error(`Failed the minimize CSS: ${error}`)
})

When I run it:

▶ time node --trace-warnings hack.js
Failed the minimize CSS: Error: Navigation Timeout Exceeded: 30000ms exceeded
node --trace-warnings hack.js  1.23s user 0.25s system 4% cpu 31.588 total

Note in the sample code above, if I switch to use urls: ['https://www.peterbe.com'] instead, it works.

So according to the documentation the call to page.goto can throw errors if, amongst other things, "target URL is invalid" but we aren't prepared for any errors thrown on that line:

response = await page.goto(pageUrl, { waitUntil: 'networkidle0' });
or
response = await page.goto(pageUrl);

This code:

const puppeteer = require('puppeteer');

const Url = "https://www.peterbe.com#other-important-security-considerations";
// const Url="https://www.peterbe.com";

(async () => {
  const browser = await puppeteer.launch();
  console.log('Browser version', await browser.version());

  const page = await browser.newPage();
  await page.setRequestInterception(true);
  await page.setDefaultNavigationTimeout(5 * 1000);
  page.on('request', request => {
    const requestUrl = request.url();
    console.log(requestUrl);
    request.continue();
  });
  page.on('response', response => {
    console.log(response.status(), response.url());
  });
  await page.setJavaScriptEnabled(false);
  await page.goto(Url);
  const htmlVanilla = await page.content();
  await page.setJavaScriptEnabled(true);
  const response = await page.goto(Url, {
    waitUntil: 'networkidle0'
  });
  const htmlJS = await page.content();
  await browser.close();
})();

When you run that it will crash and fail with Error: Navigation Timeout Exceeded: 5000ms exceeded.
But when I upgraded to puppeteer 1.4.0 that code above works.

Ignore this comment above about not catching errors around await page.goto(...). Those lines of code are actually already in a try{...} catch(e) {return safeReject(e)}.