'use strict'; var React = require('react'); var react = require('@remix-run/react'); var webVitals = require('web-vitals'); function _interopNamespace(e) { if (e && e.__esModule) return e; var n = Object.create(null); if (e) { Object.keys(e).forEach(function (k) { if (k !== 'default') { var d = Object.getOwnPropertyDescriptor(e, k); Object.defineProperty(n, k, d.get ? d : { enumerable: true, get: function () { return e[k]; } }); } }); } n.default = e; return Object.freeze(n); } var React__namespace = /*#__PURE__*/_interopNamespace(React); // src/index.ts var vitalsUrl = "https://vitals.vercel-analytics.com/v1/vitals"; function sendToAnalytics(metric, options) { let page = Object.entries(options.params).reduce((acc, [key, value]) => { return value ? acc.replace(value, `[${key}]`) : ``; }, options.path); let body = { dsn: options.analyticsId, // qPgJqYH9LQX5o31Ormk8iWhCxZO id: metric.id, // v2-1653884975443-1839479248192 page, // /blog/[slug] href: location.href, // https://{my-example-app-name-here}/blog/my-test event_name: metric.name, // TTFB value: metric.value.toString(), // 60.20000000298023 speed: getConnectionSpeed() // 4g }; if (options.debug) { onDebug(metric.name, body); } let blob = new Blob([new URLSearchParams(body).toString()], { // This content type is necessary for `sendBeacon` type: "application/x-www-form-urlencoded" }); if (navigator.sendBeacon) { navigator.sendBeacon(vitalsUrl, blob); } else { fetch(vitalsUrl, { method: "POST", body: blob, credentials: "omit", keepalive: true }); } } var isRegistered = false; function WebVitals(options) { let location2 = react.useLocation(); let params = react.useParams(); React__namespace.useEffect(() => { (() => { if (isRegistered) { return; } isRegistered = true; let fullOptions = { ...options, params, path: location2.pathname }; try { webVitals.onFID((metric) => sendToAnalytics(metric, fullOptions)); webVitals.onTTFB((metric) => sendToAnalytics(metric, fullOptions)); webVitals.onLCP((metric) => sendToAnalytics(metric, fullOptions)); webVitals.onCLS((metric) => sendToAnalytics(metric, fullOptions)); webVitals.onFCP((metric) => sendToAnalytics(metric, fullOptions)); webVitals.onINP((metric) => sendToAnalytics(metric, fullOptions)); } catch (error) { onError(error); } })(); }, [location2.hash, location2.pathname, location2.search, options, params]); return null; } function getConnectionSpeed() { return "connection" in navigator && navigator["connection"] && // @ts-expect-error "effectiveType" in navigator["connection"] ? navigator["connection"]["effectiveType"] : ""; } function onError(error) { console.error(`[@mcansh/remix-web-vitals]`, error); } function onDebug(label, ...args) { console.debug(label, ...args); } exports.WebVitals = WebVitals; //# sourceMappingURL=out.js.map //# sourceMappingURL=index.cjs.map