From a0316b470c1e788510cb24448e6d5b6dd4ce4759 Mon Sep 17 00:00:00 2001 From: Okan Sahin Date: Tue, 6 Aug 2024 18:39:13 +0200 Subject: [PATCH] Address PR feedback --- libs/utils/logWebVitals.js | 8 +++----- libs/utils/utils.js | 13 +++++-------- 2 files changed, 8 insertions(+), 13 deletions(-) diff --git a/libs/utils/logWebVitals.js b/libs/utils/logWebVitals.js index f6196b1bb0..8c092fb5e4 100644 --- a/libs/utils/logWebVitals.js +++ b/libs/utils/logWebVitals.js @@ -1,13 +1,13 @@ const LANA_CLIENT_ID = 'pageperf'; -let lanaSent = false; +let lanaSent; function sendToLana(lanaData) { if (lanaSent) return; const ua = window.navigator.userAgent; Object.assign(lanaData, { - chromeVer: (ua.match(/Chrome\/(\d+\.\d+\.\d+\.\d+)/) || [])[1] || '', + chromeVer: ua.match(/Chrome\/(\d+\.\d+\.\d+\.\d+)/)?.[1] || '', country: sessionStorage.getItem('akamai') || '', // eslint-disable-next-line compat/compat downlink: window.navigator?.connection?.downlink || '', @@ -41,9 +41,7 @@ function observeCLS(lanaData) { /* c8 ignore start */ new PerformanceObserver((entryList) => { for (const entry of entryList.getEntries()) { - if (!entry.hadRecentInput) { - cls += entry.value; - } + if (!entry.hadRecentInput) cls += entry.value; } lanaData.cls = cls.toPrecision(4); }).observe({ type: 'layout-shift', buffered: true }); diff --git a/libs/utils/utils.js b/libs/utils/utils.js index 1f0bab3f96..e897310fec 100644 --- a/libs/utils/utils.js +++ b/libs/utils/utils.js @@ -999,18 +999,15 @@ export function scrollToHashedElement(hash) { } function logPagePerf() { + if (getMetadata('pageperf') !== 'on') return; const isChrome = () => { const nav = window.navigator; return nav.userAgent.includes('Chrome') && nav.vendor.includes('Google'); }; - - if (getMetadata('pageperf') === 'on' && isChrome()) { - const sampleRate = parseInt(getMetadata('pageperf-rate'), 10) || 50; - if (Math.random() * 100 <= sampleRate) { - import('./logWebVitals.js') - .then((mod) => mod.default(getConfig().mep, getMetadata('pageperf-delay') || 1000)); - } - } + const sampleRate = parseInt(getMetadata('pageperf-rate'), 10) || 50; + if (!isChrome() || Math.random() * 100 > sampleRate) return; + import('./logWebVitals.js') + .then((mod) => mod.default(getConfig().mep, getMetadata('pageperf-delay') || 1000)); } export async function loadDeferred(area, blocks, config) {