lighthouse icon indicating copy to clipboard operation
lighthouse copied to clipboard

Expose page functions to custom gatherers

Open upugo-dev opened this issue 7 years ago • 9 comments

Provide the steps to reproduce

  1. Run LH on https://bristololdvic.org.uk with my massive wrapper function and custom gatherer/audit library...

What is the current behavior?

Got a custom audit to test for http status code- snippet below.

const mainResource = await artifacts.requestMainResource({URL: artifacts.URL, devtoolsLog});
 const statusCode = mainResource.statusCode;

 if (statusCode >= 500 && statusCode < 600) {
   return {
     rawValue: false,
     displayValue: `Page returned HTTP status code of ${statusCode}`,
   };
 }

 return {
   rawValue: true,
   displayValue: `Page returned HTTP status code of ${statusCode}`,
 };

What happens is artifacts.requestMainResource({URL: artifacts.URL, devtoolsLog}) rejects with TypeError: devtoolsLog.forEach is not a function

This is nothing to do with the arguments I pass in to the method; I have a feeling the issue is with https://github.com/GoogleChrome/lighthouse/blob/1098f952d5c75b22668aa572532415f81d2817af/lighthouse-core/lib/network-recorder.js#L355

The rejection of this promise then exits this function prematurely, and rawValue will be null which isn't handy for further use!

What is the expected behavior?

Expected behaviour is artifacts.requestMainResource({URL: artifacts.URL, devtoolsLog}) to resolve, returning the main resource so it's parameters can be used for tests in audits.

Environment Information

  • Affected Channels:
  • Lighthouse version: [email protected]
  • Node.js version: v10.14.1
  • Operating System: Windows 10 Home

upugo-dev avatar Feb 25 '19 17:02 upugo-dev

You're using lighthouse 2.9.4?

There have been several breaking changes since then including the method of calling requestMainResource.

PS: There's already an audit that does this exact thing in the SEO category in newer versions of Lighthouse ;)

patrickhulce avatar Feb 25 '19 18:02 patrickhulce

Hey @patrickhulce thanks for taking the time to reply.

You're using lighthouse 2.9.4?

I knew I would get called out on this, but first reply, c'mon! Before posting the issue I tried npm install lighthouse@next and my app wouldn't even get off the ground. I think there would be a lot of work for me to migrate. I saw the migration guidelines from 2 to 3 but none for 3 to 4, is there any docs? What's the key things I should know?

There have been several breaking changes since then including the method of calling requestMainResource.

Well at least that clears that up then!

PS: There's already an audit that does this exact thing in the SEO category in newer versions of Lighthouse ;)

That's good to know, cheers. Guess I should have a look at the new Lighthouse features and which of them can replace audits I've already written myself

upugo-dev avatar Feb 25 '19 18:02 upugo-dev

I knew I would get called out on this, but first reply, c'mon!

Haha, sorry didn't mean to call you out just sayin' we're not really doing any backports, so if there are bugs in 2.9.4 I wouldn't hold my breath for a fix.

Before posting the issue I tried npm install lighthouse@next and my app wouldn't even get off the ground.

FWIW, @next isn't actively used at the moment. The regular lighthouse@latest is currently more stable and ahead of @next.

I saw the migration guidelines from 2 to 3 but none for 3 to 4, is there any docs? What's the key things I should know?

The changes from 3 to 4 were a lot more minor than 2 to 3 and most users shouldn't have a problem just using 4. The breaking changes that did happen were called out in https://github.com/GoogleChrome/lighthouse/releases/tag/v4.0.0

patrickhulce avatar Feb 25 '19 18:02 patrickhulce

Haha, sorry didn't mean to call you out just sayin' we're not really doing any backports, so if there are bugs in 2.9.4 I wouldn't hold my breath for a fix.

I know, I was half-joking anyway, good point.

FWIW, @next isn't actively used at the moment. The regular lighthouse@latest is currently more stable and ahead of @next.

TIL, thanks.

The changes from 3 to 4 were a lot more minor than 2 to 3 and most users shouldn't have a problem just using 4. The breaking changes that did happen were called out in https://github.com/GoogleChrome/lighthouse/releases/tag/v4.0.0

Great info thanks, some bedtime reading

I think my (first) problems running my app are because of this line (I didn't write this): const DOMHelpers = require('lighthouse/lighthouse-core/lib/dom-helpers');

yielding Cannot find module 'lighthouse/lighthouse-core/lib/dom-helpers'

I'm guessing you've removed DOMHelpers https://www.npmjs.com/package/dom-helpers (?) as a dependency, and so using it will no longer work? So I could force it to be installed as a dep or just a regular npm module and change the require path?

upugo-dev avatar Feb 25 '19 18:02 upugo-dev

Oh didn't realize folks were depending on that! We moved those helpers to another file

https://github.com/GoogleChrome/lighthouse/blob/master/lighthouse-core/lib/page-functions.js

I converted this issue to track exposing those in a non-breaking way going forward.

patrickhulce avatar Feb 25 '19 19:02 patrickhulce

Cool, thanks Patrick, I re-wrote using DOMHelper externally, and seems to run program fine, just ironing out some kinks with my results processing.

I will try now using the page-functions helper library, but any idea if getElementsInDocumentString: getElementsInDocument.toString() (L225) is equivalent to what was DOMHelpers.getElementsInDocumentFnString ?

upugo-dev avatar Feb 25 '19 19:02 upugo-dev

@upugo-dev should be AFAIK

patrickhulce avatar Feb 25 '19 19:02 patrickhulce

we decided not to do this for now.

We can encourage folks to head to page-functions.js and copy/paste out of it, but fundamentally these are just regular functions to be run in a web page. There's no Lighthouse magic sauce it would be helpful for folks to have access to, so let's not commit to it as part of our API.

brendankenny avatar Apr 15 '19 22:04 brendankenny

we decided not to do this for now.

We can encourage folks to head to page-functions.js and copy/paste out of it, but fundamentally these are just regular functions to be run in a web page. There's no Lighthouse magic sauce it would be helpful for folks to have access to, so let's not commit to it as part of our API.

That makes a lot of sense, thanks for following up.

upugo-dev avatar Apr 16 '19 09:04 upugo-dev