-
Notifications
You must be signed in to change notification settings - Fork 106
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
Wait until idle before starting detection #978
Wait until idle before starting detection #978
Conversation
The following accounts have interacted with this PR and/or linked issues. I will continue to update these lists as activity occurs. You can also manually ask me to refresh this list by adding the If you're merging code through a pull request on GitHub, copy and paste the following into the bottom of the merge commit message.
To understand the WordPress project's expectations around crediting contributors, please review the Contributor Attribution page in the Core Handbook. |
I wonder if you could avoid sending here entirely with a Beacon API approach? |
return; | ||
} | ||
|
||
// Prevent detection when page is not scrolled to the initial viewport. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this makes me wonder if on the server side we should potentially avoid using the captured data when the link is apparently to a non scrolled to top position - ie. when it contains an anchor hash. Do you do that already or what do you think?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That effectively is what this is doing, yes.
As for server-side detection of this, the URL fragment is not sent along in the HTTP request for the page, so the server won't be able to see it. (Which is why annoyingly URL fragments can be lost when having to authenticate.) It would have to be checked client-side. And often a URL fragment may not refer to an element on the page so that would require checking the DOM to see if it exists. But we only care in the end if the user is not in the first viewport, so that's what checking the scroll position does, I think in a more reliable way.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks good! Couple of small questions.
Co-authored-by: Weston Ruter <[email protected]>
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Superb!
@adamsilverstein That's a great question! I don't have a good reason why this shouldn't be used instead, other than initially it didn't seem it could be used to send a |
7761a9b
into
feature/image-loading-optimization
@adamsilverstein Oh, I remember the more important reason. It is setting a client-side lock to prevent attempting to send more URL metrics when the REST API response is successful: performance/modules/images/image-loading-optimization/detection/detect.js Lines 363 to 374 in 7761a9b
This isn't possible with the Beacon API because the response is not available. In any case, I did work out how to replace the const url = new URL( restApiEndpoint );
url.searchParams.append( '_wpnonce', restApiNonce );
navigator.sendBeacon(
url,
new Blob( [ JSON.stringify( urlMetrics ) ], {
type: 'application/json',
} )
); |
Summary
Fixes #894
This rearranges the detection logic to defer doing any DOM operations until after the page has fully loaded and the CPU is idle. Additionally, it now yields to main before initiating the fetch request to submit the data to the server, additionally breaking up tasks.
Performance Profiles
With 6X CPU throttling, the plugin active with the change applied shows a marginal improvement where tasks are further broken up.
Trace without ILO
Trace with ILO before change
Trace with ILO after change
Checklist
[Focus]
orInfrastructure
label.[Type]
label.no milestone
label.