You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
A Typescript example to point to from the docs would be helpful at points. I was recently trying to configure Speed Insights and ran into a few TS errors.
// vitals.tsimporttype{Metric}from'web-vitals';import{getCLS,getFCP,getFID,getLCP,getTTFB}from'web-vitals';exporttypeAnalyticsOptions={params: Record<string,string>;path: string;analyticsId: string;debug?: true;};constvitalsUrl='https://vitals.vercel-analytics.com/v1/vitals';interfaceNavigatorWithConnectionextendsNavigator{connection?: {effectiveType?: string;};}functiongetConnectionSpeed(): string{constnav: NavigatorWithConnection=navigator;if(nav.connection&&nav.connection.effectiveType){returnnav.connection.effectiveType;}return'';}functionsendToAnalytics(metric: Metric,options: AnalyticsOptions){constpage=Object.entries(options.params).reduce((acc,[key,value])=>acc.replace(value,`[${key}]`),options.path);constbody={dsn: options.analyticsId,id: metric.id,
page,href: location.href,event_name: metric.name,value: metric.value.toString(),speed: getConnectionSpeed()};if(options.debug){console.log('[Analytics]',metric.name,JSON.stringify(body,null,2));}constblob=newBlob([newURLSearchParams(body).toString()],{// This content type is necessary for `sendBeacon`:type: 'application/x-www-form-urlencoded'});if(navigator.sendBeacon){navigator.sendBeacon(vitalsUrl,blob);}elsefetch(vitalsUrl,{body: blob,method: 'POST',credentials: 'omit',keepalive: true});}exportfunctionwebVitals(options: AnalyticsOptions): void{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));}catch(err){console.error('[Analytics]',err);}}
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
-
Idea
A Typescript example to point to from the docs would be helpful at points. I was recently trying to configure Speed Insights and ran into a few TS errors.
Beta Was this translation helpful? Give feedback.
All reactions