Skip to content

Commit

Permalink
fix(web-vitals): Check for valid entry in updatedCLS (#3816)
Browse files Browse the repository at this point in the history
* fix: Check for valid entry in updatedCLS
* fix: Make sure sessionEntries is a non-empty array
  • Loading branch information
AbhiPrasad authored Jul 19, 2021
1 parent d79b6a7 commit f8a4d60
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 2 deletions.
2 changes: 1 addition & 1 deletion packages/tracing/src/browser/web-vitals/getCLS.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ export const getCLS = (onReport: ReportHandler, reportAllChanges?: boolean): voi
let report: ReturnType<typeof bindReporter>;

const entryHandler = (entry: LayoutShift): void => {
if (!entry.hadRecentInput) {
if (entry && !entry.hadRecentInput) {
(metric.value as number) += entry.value;
metric.entries.push(entry);
if (report) {
Expand Down
4 changes: 3 additions & 1 deletion packages/tracing/src/browser/web-vitals/getUpdatedCLS.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,8 @@ export const getUpdatedCLS = (onReport: ReportHandler, reportAllChanges?: boolea

const entryHandler = (entry: LayoutShift): void => {
// Only count layout shifts without recent user input.
if (!entry.hadRecentInput) {
// TODO: Figure out why entry can be undefined
if (entry && !entry.hadRecentInput) {
const firstSessionEntry = sessionEntries[0];
const lastSessionEntry = sessionEntries[sessionEntries.length - 1];

Expand All @@ -52,6 +53,7 @@ export const getUpdatedCLS = (onReport: ReportHandler, reportAllChanges?: boolea
// entry in the current session. Otherwise, start a new session.
if (
sessionValue &&
sessionEntries.length !== 0 &&
entry.startTime - lastSessionEntry.startTime < 1000 &&
entry.startTime - firstSessionEntry.startTime < 5000
) {
Expand Down

0 comments on commit f8a4d60

Please sign in to comment.