-
Notifications
You must be signed in to change notification settings - Fork 426
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
Add support for generating custom targets in the attribution build #585
Conversation
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.
Can you update $
to _
?
And add examples to README?
Other than that LGTM.
Co-authored-by: Barry Pollard <[email protected]>
I believe I already did. Are you still see examples of |
Yes. In LCP and CLS ones that were part of this PR not the last one. For example: https://github.com/GoogleChrome/web-vitals/blob/custom-target/src/attribution/onCLS.ts#L72 |
Ahh, right. Updated! |
@tunetheweb @brendankenny are you both happy with |
SGTM My only slight concern is that typescript gets slightly messier for most users now that Target is an Also if users return the Node instead then they may not consider this memory "leak" issue? We could insist (especially given the Or we could always return the target using our string selector and have an additional, optional, return field for this (in which case |
We could potentially type it as
In all cases we only reference these values via a WeakMap keyed on performance entries, so in the worst case scenario the max number DOM nodes that would be "leaked" would be equal to the number of performance entries stored by the attribution object. Another option would be to document that the |
If this was a strongly desired goal, the way to do this would probably be to parameterize the attribution objects with the target type (defaulting to export interface INPAttribution<Target=unknown> {
//...
interactionTarget: Target;
// ...
}
export interface INPMetricWithAttribution<Target=unknown> extends INPMetric {
attribution: INPAttribution<Target>;
} Then for export const onINP = <Target>(
onReport: (metric: INPMetricWithAttribution<NoInfer<Target>>) => void,
opts: ReportOpts<Target> = {},
) => { ( There are probably some things I'm forgetting (there's trickiness between type parameters in function parameters vs the parameter of a callback which is itself a parameter...) but that should mostly work. Whether or not the extra complexity and maintenance is worth it is a judgement call though :) |
I'm not sure it IS worth that complexity. So I'm back to:
I'm starting to veer towards 3. Probably without the ability to override our WDYT? |
oh, like defaulting to
But that's going to take some more work to get it in sync with the |
I think I'm leaning toward enforcing a At the end of the day, you can't send a DOM element to a server anyway, so at some point you're gonna have to convert it to a string regardless. The only exception to this would be things like DevTools or browser extensions, and I'm fine with having those consumers jump through extra hoops to get the node reference. |
LGTM but I was waiting for the README updates before giving the formal OK. |
Gah! Forgot to run PTAL. |
Co-authored-by: Barry Pollard <[email protected]>
Fixes #561 and #580.
This PR is an alternative approach to #562 (built on top of my proposed changes in #583) that also adds a new top-level
generateTarget
configuration option which allows developers to pass in their own function—as an alternative to the libraries internalgetSelector()
function.All of the "target" attribution fields that previously reported the result of the internal
getSelector()
function will now report whatever value is returned from the passedgenerateTarget()
function.I believe there are two benefits to this approach:
Breaking changes in this PR include (similar to #562):
LCPAttribution.element
has been renamed toLCPAttribution.target
(for consistency, similar to Save a reference to the LCP and CLS targets in case they are removed from the DOM #562).LCPAttribution.interactionTargetElement
has been removed (to address the issue in Keeping elements around for attribution can leak memory #580).