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

Allow extensions to opt-in to using the web-vitals attribution build via the od_use_web_vitals_attribution_build filter #1759

Merged
merged 45 commits into from
Jan 14, 2025

Conversation

swissspidy
Copy link
Member

Summary

Fixes #1736

Idea: add a "dot" on relevant elements (e.g. LCP elements). Click on the dot to learn more about the element.

Screenshot 2024-12-18 at 21 13 55

Relevant technical choices

  • Allows loading web-vitals attribution build

@swissspidy swissspidy added [Type] Enhancement A suggestion for improvement of an existing feature [Plugin] Optimization Detective Issues for the Optimization Detective plugin labels Dec 18, 2024
Co-authored-by: Weston Ruter <westonruter@google.com>
@swissspidy swissspidy added the no milestone PRs that do not have a defined milestone for release label Dec 19, 2024
@swissspidy
Copy link
Member Author

With fresh eyes I was able to fix the anchor positioning and styling a bit. This looks much better now:

Screen.Recording.2025-01-10.at.15.42.34.mov

Of course it needs a lot of tidying up still, but it seems to more or less work.

Now we just need to figure out where to put all of this code. don't necessarily like putting it into its own GitHub repo where it's not easily discoverable.


Aside:

There is one test failure now:

1) Test_OD_URL_Metric::test_get_json_schema
root/inpData
Failed asserting that false is true.

🤔

@swissspidy swissspidy changed the title [TRY] Optimization Detective debug helper Optimization Detective: Support adding attribution build & INP Jan 13, 2025
@westonruter
Copy link
Member

That means this debug helper can't actually place this "dot" because figure.wp-block-image.aligncenter.size-medium.wp-lightbox-container>img.wp-image-44.hide doesn't exist at the time the document is loaded.

image

Humm. Yeah. There's a similar issue for what to do if content is added to the page by JS and the slow interaction occurs for an element in that added content. Basically, we can't rely on the interactionTarget to be present at the time the page loads. Then the question is when do we query for them? We could add a MutationObserver which watches for changes to the page, and with some heavy debouncing, re-query the page for the selectors. Then we could find the elements later on during the life of the page.

This, however, won't address the related problem of the hide class being added to the lightbox. Well, at the moment the hide class is added, the element is seen anymore so there is no point to try to show a dot on it! Really what we'd want to is to capture the selector for the element before the user interacted with it, so that we know what potentially will cause a slow interaction?

@swissspidy
Copy link
Member Author

Sounds good. I started extracting the relevant code to https://github.com/swissspidy/od-debug-helper and made this PR all about adding the web-vitals.js attribution build.

What's missing is passing the INP data to OD extensions.

--

Really what we'd want to is to capture the selector for the element before the user interacted with it, so that we know what potentially will cause a slow interaction?

Yes I think so.

@westonruter
Copy link
Member

Something else to consider is to not rely on the selector as provided by the attribution build but rather to construct an XPath to the element using the same scheme the plugin uses server-side. This would eliminate reliance on the class names to select the element. It would likely, however, break in other ways, such as when JS adds or removes nodes preceding the node in the tree.

plugins/optimization-detective/detect.js Outdated Show resolved Hide resolved
plugins/optimization-detective/detect.js Outdated Show resolved Hide resolved
plugins/optimization-detective/detect.js Outdated Show resolved Hide resolved
plugins/optimization-detective/types.ts Outdated Show resolved Hide resolved
plugins/optimization-detective/types.ts Outdated Show resolved Hide resolved
@westonruter westonruter changed the title Optimization Detective: Support adding attribution build & INP Optimization Detective: Support adding attribution build ~& INP~ Jan 13, 2025
@westonruter westonruter changed the title Optimization Detective: Support adding attribution build ~& INP~ Optimization Detective: Support adding attribution build Jan 13, 2025
@westonruter westonruter changed the title Optimization Detective: Support adding attribution build Allow extensions to opt-in to using the Web Vitals attribution build Jan 14, 2025
@westonruter westonruter removed the no milestone PRs that do not have a defined milestone for release label Jan 14, 2025
@swissspidy
Copy link
Member Author

Something else to consider is to not rely on the selector as provided by the attribution build but rather to construct an XPath to the element using the same scheme the plugin uses server-side. This would eliminate reliance on the class names to select the element. It would likely, however, break in other ways, such as when JS adds or removes nodes preceding the node in the tree.

Yes, I thought about that too but found it too cumbersome at first. But I now just got Gemini to write a DOM element -> XPath selector for me so now I guess I'll use that one for now :) Then I don't need any JS to query by selector for now.

While it won't address the case of dynamically added elements, I think that's good enough for now for this debug helper.

@swissspidy swissspidy marked this pull request as ready for review January 14, 2025 09:55
Copy link

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: swissspidy <swissspidy@git.wordpress.org>
Co-authored-by: westonruter <westonruter@git.wordpress.org>
Co-authored-by: felixarntz <flixos90@git.wordpress.org>

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

plugins/optimization-detective/detection.php Outdated Show resolved Hide resolved
plugins/optimization-detective/detect.js Outdated Show resolved Hide resolved
Comment on lines 500 to 504
/**
*
* @param {LCPMetric|LCPMetricWithAttribution} metric
*/
( metric ) => {
Copy link
Member

Choose a reason for hiding this comment

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

Could this go back to being defined inline? The type is picked up for me in PhpStorm:

image

Suggested change
/**
*
* @param {LCPMetric|LCPMetricWithAttribution} metric
*/
( metric ) => {
( /** @type {LCPMetric|LCPMetricWithAttribution} */ metric ) => {

Copy link
Member Author

Choose a reason for hiding this comment

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

But your PhpStorm clearly only sees LCPMetric, not LCPMetricWithAttribution

Copy link
Member

Choose a reason for hiding this comment

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

Good point!

What's strange to me is why this typing is even needed.

With 3614c13 I added more typing for the attribution report functions. Then PHPStorm reports that the arg is detected as a LCPMetricWithAttribution:

image

But TypeScript is giving a warning about it being implicit any:

image

Nevertheless, onLCP is defined as being either OnLCPFunction or OnLCPWithAttributionFunction:

image

So I would have thought that this would be later be reflected in the type of the arg here. But instead it is showing up as any, even though the common ReportOpts arg is carried through:

image

@westonruter westonruter changed the title Allow extensions to opt-in to using the Web Vitals attribution build Allow extensions to opt-in to using the web-vitals attribution build via the od_use_web_vitals_attribution_build filter Jan 14, 2025
@swissspidy swissspidy merged commit 8f1669d into trunk Jan 14, 2025
21 checks passed
@swissspidy swissspidy deleted the try/1736-debug-detective branch January 14, 2025 19:46
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
[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.

Explore opportunities to use Optimization Detective to identify INP problems
3 participants