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

fix(browser): Set artificial zero-value CLS measurement to report no layout shift #12989

Open
wants to merge 5 commits into
base: develop
Choose a base branch
from

Conversation

Lms24
Copy link
Member

@Lms24 Lms24 commented Jul 19, 2024

So this is a fun one:

The PerformanceObserver emits LayoutShift entries whenever a layout shift occurs. If no layour shift occurs (which is great!), the observer will never emit any entries. Makes sense so far!

Unfortunately, this is problematic for the Sentry product/UI. We can't differentiate between a CLS of 0 and not having received CLS data at all. So in both cases, we'd show users that the CLS score simply is not available. When in fact, it can be 0, which is a very good score.

This PR adds a workaround to artificially set a CLS of 0 right at the start of listening to CLS events. This way, we can differentiate between a CLS of 0 and no CLS at all. If a layout shift occurs later, the real CLS value will be emitted and the 0 value will be overwritten. We also only send this artificial 0 value if the browser supports reporting the layout-shift entry type to avoid skewing the CLS score.

Note

There was some discussion around if this change should be guarded with an experimental flag. I don't think that this change has a high-impact potential for the majority of interactive applications (like the Sentry frontend for example). For example, even our docs, which appear rather static, do expose a tiny bit if layout shift on all pages as there's some dynamic data fetching and UI update going on to retrieve the latest package version. Also, I couldn't find a single page in Sentry with no layout shift. In both projects, there are a number of pages with 0 layout shift but as far as I can tell, this is because we round tiny layout shift measurements to 0. So this change wouldn't have any effects on such pages.
This change really affects apps the most that have fully statically generated pages (e.g. Astro by default or other SSG-created apps). And even then, there could still be layout shift (e.g. fonts loaded lazily or scripts injecting elements).
Adding to this, we have tests that show that actual layout shift (>0) is still reported correctly. I think it's fine to ship this with the next release. Also discussed this briefly with some members of the team and they agreed. Happy to still guard it if reviewers would prefer it though.

@Lms24 Lms24 self-assigned this Jul 19, 2024
</head>
<body>
<div id="content">
Some content
Copy link
Member Author

@Lms24 Lms24 Jul 19, 2024

Choose a reason for hiding this comment

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

There needs to be something visible on the page because we (rightfully) only send CLS measurements if FCP was also recorded.

@Lms24 Lms24 force-pushed the lms/fix-browser-cls-zero-values branch from 8976584 to 0ded265 Compare July 19, 2024 14:09
@Lms24 Lms24 requested a review from bcoe July 19, 2024 14:26
Copy link
Contributor

github-actions bot commented Jul 19, 2024

size-limit report 📦

Path Size % Change Change
@sentry/browser 22.5 KB - -
@sentry/browser (incl. Tracing) 34.87 KB +0.08% +27 B 🔺
@sentry/browser (incl. Tracing, Replay) 71.22 KB +0.05% +34 B 🔺
@sentry/browser (incl. Tracing, Replay) - with treeshaking flags 64.48 KB +0.05% +31 B 🔺
@sentry/browser (incl. Tracing, Replay with Canvas) 75.56 KB +0.05% +32 B 🔺
@sentry/browser (incl. Tracing, Replay, Feedback) 88.25 KB +0.04% +31 B 🔺
@sentry/browser (incl. Tracing, Replay, Feedback, metrics) 90.1 KB +0.04% +33 B 🔺
@sentry/browser (incl. metrics) 26.81 KB - -
@sentry/browser (incl. Feedback) 39.53 KB - -
@sentry/browser (incl. sendFeedback) 27.13 KB - -
@sentry/browser (incl. FeedbackAsync) 31.84 KB - -
@sentry/react 25.26 KB - -
@sentry/react (incl. Tracing) 37.86 KB +0.07% +26 B 🔺
@sentry/vue 26.65 KB - -
@sentry/vue (incl. Tracing) 36.7 KB +0.08% +29 B 🔺
@sentry/svelte 22.64 KB - -
CDN Bundle 23.75 KB - -
CDN Bundle (incl. Tracing) 36.53 KB +0.08% +27 B 🔺
CDN Bundle (incl. Tracing, Replay) 70.85 KB +0.04% +27 B 🔺
CDN Bundle (incl. Tracing, Replay, Feedback) 76.13 KB +0.04% +25 B 🔺
CDN Bundle - uncompressed 69.64 KB - -
CDN Bundle (incl. Tracing) - uncompressed 108.43 KB +0.12% +124 B 🔺
CDN Bundle (incl. Tracing, Replay) - uncompressed 219.67 KB +0.06% +124 B 🔺
CDN Bundle (incl. Tracing, Replay, Feedback) - uncompressed 232.71 KB +0.06% +124 B 🔺
@sentry/nextjs (client) 37.62 KB +0.08% +30 B 🔺
@sentry/sveltekit (client) 35.47 KB +0.08% +26 B 🔺
@sentry/node 115.71 KB - -
@sentry/node - without tracing 90.1 KB - -
@sentry/aws-serverless 99.51 KB - -

View base workflow run

Copy link
Contributor

@edwardgou-sentry edwardgou-sentry left a comment

Choose a reason for hiding this comment

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

I don't have any objections to this, I think it makes sense!

I'd like to confirm that it's okay to send a CLS measurement but no CLS attribution/source element as additional information.

I don't believe we apply any additional logic to cls sources in relay (we don't use this for calculation), and it just gets saved as a tag, so we should be safe here.

Copy link
Member Author

@Lms24 Lms24 Jul 22, 2024

Choose a reason for hiding this comment

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

Note: We have 3 more integration tests covering >0 CLS values which all still pass. So I don't think that this change somehow causes actual layout shift values to be ignored.

@Lms24 Lms24 marked this pull request as ready for review July 22, 2024 11:26
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.

3 participants