All files / varjoliitokauppa/components ClientMonitoring.tsx

72.72% Statements 24/33
57.14% Branches 8/14
60% Functions 6/10
71.87% Lines 23/32

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125                                              3x   3x   3x 1x       1x   1x 1x 1x                             3x                               3x 2x 1x                             2x 2x 1x     1x                             1x                 1x 1x   1x 1x 1x       2x    
'use client';
 
import { useEffect } from 'react';
import { useReportWebVitals } from 'next/web-vitals';
 
type ClientErrorPayload = {
  message: string;
  stack?: string;
  name?: string;
  filename?: string;
  lineno?: number;
  colno?: number;
};
 
type MonitoringPayload = {
  type: 'web-vital' | 'client-error';
  url: string;
  timestamp: number;
  metric?: Record<string, unknown>;
  error?: ClientErrorPayload;
};
 
const MONITORING_ENDPOINT =
  process.env.NEXT_PUBLIC_MONITORING_ENDPOINT || '/api/monitoring';
 
const isProduction = () => process.env.NODE_ENV === 'production';
 
const sendPayload = (payload: MonitoringPayload) => {
  Iif (!isProduction()) {
    return;
  }
 
  const body = JSON.stringify(payload);
 
  Eif (typeof navigator !== 'undefined' && navigator.sendBeacon) {
    navigator.sendBeacon(MONITORING_ENDPOINT, body);
    return;
  }
 
  fetch(MONITORING_ENDPOINT, {
    method: 'POST',
    headers: {
      'content-type': 'application/json',
    },
    body,
    keepalive: true,
  }).catch(() => {
    // Swallow network errors to avoid cascading failures in the client.
  });
};
 
const toErrorPayload = (error: unknown): ClientErrorPayload => {
  if (error instanceof Error) {
    return {
      name: error.name,
      message: error.message,
      stack: error.stack,
    };
  }
 
  if (typeof error === 'string') {
    return { message: error };
  }
 
  return { message: 'Unknown error' };
};
 
export const ClientMonitoring = () => {
  useReportWebVitals((metric) => {
    sendPayload({
      type: 'web-vital',
      url: window.location.href,
      timestamp: Date.now(),
      metric: {
        id: metric.id,
        name: metric.name,
        value: metric.value,
        label: metric.label,
        rating: metric.rating,
        navigationType: metric.navigationType,
      },
    });
  });
 
  useEffect(() => {
    if (!isProduction()) {
      return;
    }
 
    const handleError = (event: ErrorEvent) => {
      sendPayload({
        type: 'client-error',
        url: window.location.href,
        timestamp: Date.now(),
        error: {
          ...toErrorPayload(event.error),
          message: event.message,
          filename: event.filename,
          lineno: event.lineno,
          colno: event.colno,
        },
      });
    };
 
    const handleRejection = (event: PromiseRejectionEvent) => {
      sendPayload({
        type: 'client-error',
        url: window.location.href,
        timestamp: Date.now(),
        error: toErrorPayload(event.reason),
      });
    };
 
    window.addEventListener('error', handleError);
    window.addEventListener('unhandledrejection', handleRejection);
 
    return () => {
      window.removeEventListener('error', handleError);
      window.removeEventListener('unhandledrejection', handleRejection);
    };
  }, []);
 
  return null;
};