From ddc5f375bebe8e537e61ca288931380a4c25eea9 Mon Sep 17 00:00:00 2001 From: Torsten Dittmann Date: Fri, 14 Oct 2022 18:01:58 +0200 Subject: [PATCH] chore: upgrade web vitals --- package-lock.json | 14 +++++++------- package.json | 2 +- src/lib/helpers/vitals.ts | 21 ++++++++++++++------- src/routes/+layout.svelte | 20 +++++++++++--------- 4 files changed, 33 insertions(+), 24 deletions(-) diff --git a/package-lock.json b/package-lock.json index 0e9b8d8d5..2fb6a49b2 100644 --- a/package-lock.json +++ b/package-lock.json @@ -14,7 +14,7 @@ "echarts": "^5.4.0", "prismjs": "^1.29.0", "tippy.js": "^6.3.7", - "web-vitals": "^2.1.4" + "web-vitals": "^3.0.3" }, "devDependencies": { "@playwright/test": "^1.26.1", @@ -7531,9 +7531,9 @@ } }, "node_modules/web-vitals": { - "version": "2.1.4", - "resolved": "https://registry.npmjs.org/web-vitals/-/web-vitals-2.1.4.tgz", - "integrity": "sha512-sVWcwhU5mX6crfI5Vd2dC4qchyTqxV8URinzt25XqVh+bHEPGH4C3NPrNionCP7Obx59wrYEbNlw4Z8sjALzZg==" + "version": "3.0.3", + "resolved": "https://registry.npmjs.org/web-vitals/-/web-vitals-3.0.3.tgz", + "integrity": "sha512-8K6cl3alkVhwv7RQm3WAWFzvP9dBTH4CwOE7G3DfQDEnBJTdtNgX5XMqgE7aE79wac/kJ8fHWftUfH3AVxxbBw==" }, "node_modules/webidl-conversions": { "version": "7.0.0", @@ -13334,9 +13334,9 @@ } }, "web-vitals": { - "version": "2.1.4", - "resolved": "https://registry.npmjs.org/web-vitals/-/web-vitals-2.1.4.tgz", - "integrity": "sha512-sVWcwhU5mX6crfI5Vd2dC4qchyTqxV8URinzt25XqVh+bHEPGH4C3NPrNionCP7Obx59wrYEbNlw4Z8sjALzZg==" + "version": "3.0.3", + "resolved": "https://registry.npmjs.org/web-vitals/-/web-vitals-3.0.3.tgz", + "integrity": "sha512-8K6cl3alkVhwv7RQm3WAWFzvP9dBTH4CwOE7G3DfQDEnBJTdtNgX5XMqgE7aE79wac/kJ8fHWftUfH3AVxxbBw==" }, "webidl-conversions": { "version": "7.0.0", diff --git a/package.json b/package.json index 5d2bde148..77a87fa12 100644 --- a/package.json +++ b/package.json @@ -24,7 +24,7 @@ "echarts": "^5.4.0", "prismjs": "^1.29.0", "tippy.js": "^6.3.7", - "web-vitals": "^2.1.4" + "web-vitals": "^3.0.3" }, "devDependencies": { "@playwright/test": "^1.26.1", diff --git a/src/lib/helpers/vitals.ts b/src/lib/helpers/vitals.ts index 22a35f211..25d3248e6 100644 --- a/src/lib/helpers/vitals.ts +++ b/src/lib/helpers/vitals.ts @@ -1,4 +1,6 @@ -import { getCLS, getFCP, getFID, getLCP, getTTFB, type Metric } from 'web-vitals'; +import { page } from '$app/stores'; +import { get } from 'svelte/store'; +import type { Metric } from 'web-vitals'; type Options = { params: @@ -57,14 +59,19 @@ function sendToAnalytics(metric: Metric, options: Options) { }); } -export function webVitals(options: Options) { +export function reportWebVitals(metric: Metric) { try { - getFID((metric) => sendToAnalytics(metric, options)); - getTTFB((metric) => sendToAnalytics(metric, options)); - getLCP((metric) => sendToAnalytics(metric, options)); - getCLS((metric) => sendToAnalytics(metric, options)); - getFCP((metric) => sendToAnalytics(metric, options)); + sendToAnalytics(metric, createWebVitalsOptions()); } catch (err) { console.error('[Analytics]', err); } } + +function createWebVitalsOptions(): Options { + const ctx = get(page); + return { + path: ctx.url.pathname, + params: ctx.params, + analyticsId: window.VERCEL_ANALYTICS_ID || '' + }; +} diff --git a/src/routes/+layout.svelte b/src/routes/+layout.svelte index 430811845..03498f54f 100644 --- a/src/routes/+layout.svelte +++ b/src/routes/+layout.svelte @@ -3,13 +3,14 @@ import { page } from '$app/stores'; import { user } from '$lib/stores/user'; import { redirectTo } from '$lib/stores/organization'; + import { onCLS, onFID, onLCP } from 'web-vitals'; + import { reportWebVitals } from '$lib/helpers/vitals'; import { onMount } from 'svelte'; import { base } from '$app/paths'; import { browser, dev } from '$app/environment'; import { app } from '$lib/stores/app'; import Notifications from '$lib/layout/notifications.svelte'; import Loading from './_loading.svelte'; - import { webVitals } from '$lib/helpers/vitals'; let loaded = false; @@ -21,6 +22,15 @@ const acceptedRoutes = ['/login', '/register', '/recover', '/invite']; onMount(async () => { + /** + * Reporting Web Vitals. + */ + if (!dev && window.VERCEL_ANALYTICS_ID) { + onCLS(reportWebVitals); + onFID(reportWebVitals); + onLCP(reportWebVitals); + } + try { if (!$user) { await user.fetchUser(); @@ -38,14 +48,6 @@ } }); - $: if (!dev && browser && window.VERCEL_ANALYTICS_ID) { - webVitals({ - path: $page.url.pathname, - params: $page.params, - analyticsId: window.VERCEL_ANALYTICS_ID - }); - } - $: { if (browser) { if ($app.theme === 'auto') {