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

Wait until idle before starting detection #978

Merged

Conversation

westonruter
Copy link
Member

@westonruter westonruter commented Feb 9, 2024

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

Screenshot 2024-02-09 10 55 19

Trace with ILO before change

Screenshot 2024-02-09 10 56 49

Trace with ILO after change

Screenshot 2024-02-09 10 57 57

Checklist

  • PR has either [Focus] or Infrastructure label.
  • PR has a [Type] label.
  • PR has a milestone or the no milestone label.

@westonruter westonruter added [Type] Enhancement A suggestion for improvement of an existing feature [Focus] Images no milestone PRs that do not have a defined milestone for release [Plugin] Optimization Detective Issues for the Optimization Detective plugin labels Feb 9, 2024
Copy link

github-actions bot commented Feb 9, 2024

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 props-bot label.

If you're merging code through a pull request on GitHub, copy and paste the following into the bottom of the merge commit message.

Co-authored-by: westonruter <[email protected]>
Co-authored-by: adamsilverstein <[email protected]>

To understand the WordPress project's expectations around crediting contributors, please review the Contributor Attribution page in the Core Handbook.

Base automatically changed from fix/optimizing-with-stale-url-metrics to feature/image-loading-optimization February 9, 2024 20:13
@adamsilverstein
Copy link
Member

Additionally, it now yields to main before initiating the fetch request to submit the data to the server, additionally breaking up tasks.

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.
Copy link
Member

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?

Copy link
Member Author

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.

Copy link
Member

@adamsilverstein adamsilverstein left a 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.

@adamsilverstein adamsilverstein self-requested a review February 9, 2024 21:29
Copy link
Member

@adamsilverstein adamsilverstein left a comment

Choose a reason for hiding this comment

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

Superb!

@westonruter
Copy link
Member Author

Additionally, it now yields to main before initiating the fetch request to submit the data to the server, additionally breaking up tasks.

I wonder if you could avoid sending here entirely with a Beacon API approach?

@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 Content-Type header, but I can see this is not the case. I'm going to open that in a separate PR.

@westonruter westonruter merged commit 7761a9b into feature/image-loading-optimization Feb 9, 2024
8 checks passed
@westonruter westonruter deleted the add/ilo-wait-until-idle branch February 9, 2024 21:55
@westonruter
Copy link
Member Author

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 Content-Type header, but I can see this is not the case. I'm going to open that in a separate PR.

@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:

const response = await fetch( restApiEndpoint, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'X-WP-Nonce': restApiNonce,
},
body: JSON.stringify( urlMetrics ),
} );
if ( response.status === 200 ) {
setStorageLock( getCurrentTime() );
}

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 fetch code with the Beacon API if that didn't end up being needed:

	const url = new URL( restApiEndpoint );
	url.searchParams.append( '_wpnonce', restApiNonce );
	navigator.sendBeacon(
		url,
		new Blob( [ JSON.stringify( urlMetrics ) ], {
			type: 'application/json',
		} )
	);

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
no milestone PRs that do not have a defined milestone for release [Plugin] Optimization Detective Issues for the Optimization Detective plugin [Type] Enhancement A suggestion for improvement of an existing feature
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants