diff --git a/README.md b/README.md index aa7835f..bfc1c43 100644 --- a/README.md +++ b/README.md @@ -58,13 +58,14 @@ I'm open to contributions! If you find any bugs, have suggestions, or want to ad This project is licensed under the GPLv3 License. Feel free to explore, learn, and have fun! Some dependencies may hold different licenses but are in compliance with GPLv3: -- `MIT`: Compatible with GPLv3. -- `Apache 2.0`: Compatible with GPLv3. -- `BSD-3-Clause`: Compatible with GPLv3. -- `BSD-2-Clause`: Compatible with GPLv3. -- `ISC`: Compatible with GPLv3. -- `Python-2.0`: Compatible with GPLv3. (Note: Python has its own license, and version 2.0 is compatible with GPLv3). -- `CC-BY-4.0`: This is a Creative Commons license, which is not a software license. It's generally not recommended to include CC licenses in software projects due to potential compatibility issues. This might cause complications if you choose GPLv3. -- `CC0-1.0`: Not a software license, but it is explicitly designed to waive all copyrights, making it effectively compatible with GPLv3. -- `0BSD`: Compatible with GPLv3. -- `(MIT OR CC0-1.0)`: MIT is compatible with GPLv3, and CC0-1.0 is effectively compatible with GPLv3. \ No newline at end of file + +- `MIT`: Compatible with GPLv3. +- `Apache 2.0`: Compatible with GPLv3. +- `BSD-3-Clause`: Compatible with GPLv3. +- `BSD-2-Clause`: Compatible with GPLv3. +- `ISC`: Compatible with GPLv3. +- `Python-2.0`: Compatible with GPLv3. (Note: Python has its own license, and version 2.0 is compatible with GPLv3). +- `CC-BY-4.0`: This is a Creative Commons license, which is not a software license. It's generally not recommended to include CC licenses in software projects due to potential compatibility issues. This might cause complications if you choose GPLv3. +- `CC0-1.0`: Not a software license, but it is explicitly designed to waive all copyrights, making it effectively compatible with GPLv3. +- `0BSD`: Compatible with GPLv3. +- `(MIT OR CC0-1.0)`: MIT is compatible with GPLv3, and CC0-1.0 is effectively compatible with GPLv3. diff --git a/src/lib/vitals.ts b/src/lib/vitals.ts index b1fcab0..48f9159 100644 --- a/src/lib/vitals.ts +++ b/src/lib/vitals.ts @@ -5,77 +5,82 @@ const vitalsUrl = 'https://vitals.vercel-analytics.com/v1/vitals'; // Improve type safety by defining the navigator.connection type interface NavigatorWithConnection extends Navigator { - connection: { - effectiveType: string; - }; + connection: { + effectiveType: string; + }; } // eslint-disable-next-line @typescript-eslint/no-explicit-any type Params = Record; // Define a type for 'params' function getConnectionSpeed() { - return 'connection' in navigator && 'connection' && 'effectiveType' in (navigator as NavigatorWithConnection).connection - ? (navigator as NavigatorWithConnection).connection.effectiveType - : ''; + return 'connection' in navigator && + 'connection' && + 'effectiveType' in (navigator as NavigatorWithConnection).connection + ? (navigator as NavigatorWithConnection).connection.effectiveType + : ''; } -function sendToAnalytics(metric: Metric, options: { - params: Params; - path: string; - analyticsId: string; - debug: boolean; -}) { - const page = Object.entries(options.params).reduce( - (acc, [key, value]) => acc.replace(value, `[${key}]`), - options.path - ); +function sendToAnalytics( + metric: Metric, + options: { + params: Params; + path: string; + analyticsId: string; + debug: boolean; + } +) { + const page = Object.entries(options.params).reduce( + (acc, [key, value]) => acc.replace(value, `[${key}]`), + options.path + ); - const body = { - dsn: options.analyticsId, - id: metric.id, - page, - href: location.href, - event_name: metric.name, - value: metric.value.toString(), - speed: getConnectionSpeed(), - }; + const body = { + 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('[Web Vitals]', metric.name, JSON.stringify(body, null, 2)); - } + if (options.debug) { + console.log('[Web Vitals]', metric.name, JSON.stringify(body, null, 2)); + } - // Serialize body to a URLSearchParams object - const searchParams = new URLSearchParams(body); + // Serialize body to a URLSearchParams object + const searchParams = new URLSearchParams(body); - // The type 'Record' is compatible with 'URLSearchParams' - const blob = new Blob([searchParams.toString()], { - type: 'application/x-www-form-urlencoded', - }); - if (navigator.sendBeacon) { - navigator.sendBeacon(vitalsUrl, blob); - } else { - fetch(vitalsUrl, { - body: blob, - method: 'POST', - credentials: 'omit', - keepalive: true, - }); - } + // The type 'Record' is compatible with 'URLSearchParams' + const blob = new Blob([searchParams.toString()], { + type: 'application/x-www-form-urlencoded' + }); + if (navigator.sendBeacon) { + navigator.sendBeacon(vitalsUrl, blob); + } else { + fetch(vitalsUrl, { + body: blob, + method: 'POST', + credentials: 'omit', + keepalive: true + }); + } } export function webVitals(options: { - params: Params; // Use the defined 'Params' type here - path: string; - analyticsId: string; - debug: boolean; + params: Params; // Use the defined 'Params' type here + path: string; + analyticsId: string; + debug: boolean; }) { - 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('[Web Vitals]', err); - } + 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('[Web Vitals]', err); + } } diff --git a/vite.config.ts b/vite.config.ts index 0f83cd6..198ac4e 100644 --- a/vite.config.ts +++ b/vite.config.ts @@ -3,10 +3,10 @@ import { defineConfig } from 'vite'; export default defineConfig({ plugins: [sveltekit()], - ssr: { - noExternal: ['three'] - }, - define: { - 'import.meta.env.VERCEL_ANALYTICS_ID': JSON.stringify(process.env.VERCEL_ANALYTICS_ID) - } + ssr: { + noExternal: ['three'] + }, + define: { + 'import.meta.env.VERCEL_ANALYTICS_ID': JSON.stringify(process.env.VERCEL_ANALYTICS_ID) + } });