-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
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
base: develop
Are you sure you want to change the base?
Conversation
</head> | ||
<body> | ||
<div id="content"> | ||
Some content |
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.
There needs to be something visible on the page because we (rightfully) only send CLS measurements if FCP was also recorded.
8976584
to
0ded265
Compare
size-limit report 📦
|
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.
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.
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.
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.
So this is a fun one:
The
PerformanceObserver
emitsLayoutShift
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.