Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

core(fr): convert main-document-content gatherer #12470

Merged
merged 6 commits into from
May 13, 2021

Conversation

adamraine
Copy link
Member

Ref #11313

@adamraine adamraine requested a review from a team as a code owner May 12, 2021 16:50
@adamraine adamraine requested review from patrickhulce and removed request for a team May 12, 2021 16:50
@google-cla google-cla bot added the cla: yes label May 12, 2021
* @param {number} [timeout]
* @return {Promise<string>}
*/
static async getRequestContent(session, requestId, timeout = 1000) {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This felt like the most convenient location for this function. I'm open to other suggestions though.

Copy link
Collaborator

@patrickhulce patrickhulce May 12, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This was my first thought as well, but it does feel a bit weird to introduce session into this file.

My other thought was on NetworkMonitor. How does that feel to you?

I think I could go either way.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It also feels weird to create a new NetworkMonitor(session) just to use this function with session. I think I prefer leaving as is.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if it's a gathering helper method it seems like it should live under driver/, but it seems like it should just go in a new file if none of the existing helper modules are a good home for it? If the stricter timeout is important so we want people getting response content this way, it should preferably not be tucked in somewhere that's already relatively large and has an important separate purpose or people are just going to call Network.getResponseBody directly because they won't know it's there

Copy link
Member

@brendankenny brendankenny May 12, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

actually, looking at the original PR (#4718), the stricter Network.getResponseBody timeout was specifically for working around using the protocol via the extension chrome.debugger API and there wasn't an issue in devtools/CLI. The extension doesn't use the protocol anymore, so should we just get rid of the getRequestContent method and everyone can call sendCommand('Network.getResponseBody', {requestId}) directly?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it should preferably not be tucked in somewhere that's already relatively large and has an important separate purpose or people are just going to call Network.getResponseBody directly because they won't know it's there

This was my concern with mingling session work in this file as well. You've boosted me beyond "could go either way" to "should probably stay in driver/ somewhere" :)

so should we just get rid of the getRequestContent method and everyone can call sendCommand('Network.getResponseBody', {requestId}) directly?

I would lean no. Defaulting this to a low timeout is a good thing, and the detail with NetworkRequest.getRequestIdForBackend is easy to forget, so bundling those together at least is necessary (otherwise your exact same argument of missing an argument tucked away somewhere is likely to happen here).

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ah yeah, I missed NetworkRequest.getRequestIdForBackend in there.

The timeout still seems like cruft (should the comment point to this discussion now since #4718 can't happen anymore? :P) but it's pre-existing so 🤷

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It also feels weird to create a new NetworkMonitor(session) just to use this function with session. I think I prefer leaving as is.

I was thinking just a static method like it is here. The alternative I see is a solo file.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have moved it to a new file, network.js

tsconfig.json Outdated
@@ -52,6 +52,7 @@
"lighthouse-core/test/gather/gatherers/http-redirect-test.js",
"lighthouse-core/test/gather/gatherers/js-usage-test.js",
"lighthouse-core/test/gather/gatherers/link-elements-test.js",
"lighthouse-core/test/gather/gatherers/main-document-content-test.js",
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Type checking would be a pain, and we probably won't need this test once afterPass is deprecated.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nooooo, but you've been so great about all the tests so far, and this is a nice opportunity to expand! :D

* @param {number} [timeout]
* @return {Promise<string>}
*/
static async getRequestContent(session, requestId, timeout = 1000) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

while we're in the reorg mode...WDYT about fetchResponseBodyFromCache?

  • fetch to signal that it's an operation that takes some time over the protcol, not just a getter
  • FromCache to signify that it's not re-requesting anything, it's just trying to get it from memory cache (FromMemory or FromMemoryCache alternates?)
  • ResponseBody because it is weird that we say we're getting the request content when it's the response content, request is sort of implicit in all of these operations

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maybe fetch should be reserved for things fetched over the network, though?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maybe fetch should be reserved for things fetched over the network, though?

That was the point of FromCache, an alternate suggestion? fetchFromCache has a weaker "over the network" connection to me than get's connection to "returns immediately"

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess I always found the name descriptive enough except for the request part you mentioned, so getResponseContent seems ok to me :)

If it was on NetworkRequest I could definitely see the issue with get sounding like just a local variable, but between the async and being in a driver/* file, I'd just assume it's doing something over the protocol, but maybe that's just because I already know how it works.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

how's about getResponseContentFromCache then? the current method makes it sound like the content will be available much more often than it actually is (if we're making wishes I'd also want to have this | undefined like our DOM utilities do when this thing throws).

Copy link
Collaborator

@patrickhulce patrickhulce left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

impl looks great!

jest.mock('../../../computed/network-records.js');

describe('FR compat', () => {
/** @type {MainDocumentContent} */
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

a trick I like, is to just double initialize these (in declaration and beforeEach) to avoid duplicating the type annotation

e.g. let gatherer = new MainDocumentGatherer();

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(don't need to change it, just offering an alternative that comes in handy when the types get more complex :)

tsconfig.json Outdated
@@ -52,6 +52,7 @@
"lighthouse-core/test/gather/gatherers/http-redirect-test.js",
"lighthouse-core/test/gather/gatherers/js-usage-test.js",
"lighthouse-core/test/gather/gatherers/link-elements-test.js",
"lighthouse-core/test/gather/gatherers/main-document-content-test.js",
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nooooo, but you've been so great about all the tests so far, and this is a nice opportunity to expand! :D

Copy link
Collaborator

@patrickhulce patrickhulce left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

B-E-A-UTIFUL

lighthouse-core/gather/driver/network.js Outdated Show resolved Hide resolved
@@ -127,11 +127,34 @@ function mockDriverModule(driverProvider) {
};
}

function createMockContext() {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nice 👍

const devtoolsLog
= /** @type {LH.DevtoolsLog} */ (require('../../fixtures/traces/lcp-m78.devtools.log.json'));

describe('FR compat', () => {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

oh this file

chef's kiss gif

@adamraine
Copy link
Member Author

I'm going to merge because I want to use createMockContext in the next PR. I can do a follow up if there are any strong opinions on naming/location of things.

@adamraine adamraine merged commit 8769ff8 into master May 13, 2021
@adamraine adamraine deleted the fr-main-document-content branch May 13, 2021 15:54
@brendankenny
Copy link
Member

I can do a follow up if there are any strong opinions on naming/location of things.

network.js wfm and the rest is just bikeshedding :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants