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

feat(utils): Add function to extract relevant component name #9921

Merged
merged 3 commits into from
Dec 20, 2023

Conversation

0Calories
Copy link
Contributor

Adds a utility function to get the frontend component name from a given DOM element. It also adjusts htmlTreeAsString to use the component name when applicable, when building out the string.

This is one part of this PR, splitting it up into multiple smaller PRs scoped to specific features:
#9855

@0Calories 0Calories self-assigned this Dec 19, 2023
Copy link
Contributor

github-actions bot commented Dec 19, 2023

size-limit report 📦

Path Size
@sentry/browser (incl. Tracing, Replay, Feedback) - Webpack (gzipped) 75.43 KB (+0.05% 🔺)
@sentry/browser (incl. Tracing, Replay) - Webpack (gzipped) 66.82 KB (+0.05% 🔺)
@sentry/browser (incl. Tracing, Replay) - Webpack with treeshaking flags (gzipped) 60.42 KB (+0.06% 🔺)
@sentry/browser (incl. Tracing) - Webpack (gzipped) 31.43 KB (+0.12% 🔺)
@sentry/browser (incl. Feedback) - Webpack (gzipped) 30.07 KB (+0.11% 🔺)
@sentry/browser - Webpack (gzipped) 21.73 KB (+0.18% 🔺)
@sentry/browser (incl. Tracing, Replay, Feedback) - ES6 CDN Bundle (gzipped) 72.83 KB (+0.06% 🔺)
@sentry/browser (incl. Tracing, Replay) - ES6 CDN Bundle (gzipped) 64.55 KB (+0.06% 🔺)
@sentry/browser (incl. Tracing) - ES6 CDN Bundle (gzipped) 30.75 KB (+0.12% 🔺)
@sentry/browser - ES6 CDN Bundle (gzipped) 22.8 KB (+0.15% 🔺)
@sentry/browser (incl. Tracing, Replay) - ES6 CDN Bundle (minified & uncompressed) 202.47 KB (+0.06% 🔺)
@sentry/browser (incl. Tracing) - ES6 CDN Bundle (minified & uncompressed) 92.39 KB (+0.13% 🔺)
@sentry/browser - ES6 CDN Bundle (minified & uncompressed) 67.45 KB (+0.17% 🔺)
@sentry/browser (incl. Tracing) - ES5 CDN Bundle (gzipped) 33.65 KB (+0.11% 🔺)
@sentry/react (incl. Tracing, Replay) - Webpack (gzipped) 67.2 KB (+0.06% 🔺)
@sentry/react - Webpack (gzipped) 21.76 KB (+0.18% 🔺)
@sentry/nextjs Client (incl. Tracing, Replay) - Webpack (gzipped) 83.97 KB (+0.04% 🔺)
@sentry/nextjs Client - Webpack (gzipped) 48.6 KB (+0.07% 🔺)
@sentry-internal/feedback - Webpack (gzipped) 16.38 KB (+0.19% 🔺)

@@ -86,6 +86,11 @@ function _htmlElementAsString(el: unknown, keyAttrs?: string[]): string {
return '';
}

// If using the component name annotation plugin, this value may be available on the DOM node
if (elem instanceof HTMLElement && elem.dataset && elem.dataset['sentryComponent']) {
Copy link
Member

Choose a reason for hiding this comment

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

l: We might have to check if HTMLElement is defined globally and if this will break typescript types when folks are running in Node only environments (utils is technically meant to be isomorphic).

I guess we can't extract this util out because replay relies on it cc @mydea

Copy link
Member

Choose a reason for hiding this comment

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

As per feedback from Luca, can we check if dom types are included when we add this change?

We can do that by looking at the type definitions of the built assets and checking for dom.d.ts being included.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Added some checks to prevent breaking types when running in Node 👍


let currentElem = elem as SimpleNode;
const MAX_TRAVERSE_HEIGHT = 5;
for (let i = 0; i < MAX_TRAVERSE_HEIGHT; i++) {
Copy link
Member

Choose a reason for hiding this comment

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

The issue with this traversing is that it might pick the wrong element, maybe we should do 2-3 to be even more conservative.

But sticky with 5 sounds fine, we can test more in different codebases and see what it looks like.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yeah I left a comment on the first PR talking about this as well. Looking to test it with 5 at the moment, and we can decrease the value later on if we find that it is too aggressive

@0Calories 0Calories merged commit d152364 into develop Dec 20, 2023
95 checks passed
@0Calories 0Calories deleted the feat/component-name-util branch December 20, 2023 19:14
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants