@axiomhq/react
Version:
The official React package for Axiom
66 lines (65 loc) • 2.2 kB
JavaScript
"use client";
;
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
const React = require("react");
const webVitals = require("web-vitals");
function _interopNamespaceDefault(e) {
const n = Object.create(null, { [Symbol.toStringTag]: { value: "Module" } });
if (e) {
for (const k in e) {
if (k !== "default") {
const d = Object.getOwnPropertyDescriptor(e, k);
Object.defineProperty(n, k, d.get ? d : {
enumerable: true,
get: () => e[k]
});
}
}
}
n.default = e;
return Object.freeze(n);
}
const React__namespace = /* @__PURE__ */ _interopNamespaceDefault(React);
function useReportWebVitals(pushMetrics, flushMetrics) {
const pushMetricsRef = React__namespace.useRef(pushMetrics);
const flushMetricsRef = React__namespace.useRef(flushMetrics);
React__namespace.useEffect(() => {
const effectFlushMetrics = () => {
flushMetricsRef.current();
};
webVitals.onCLS(pushMetricsRef.current);
webVitals.onFID(pushMetricsRef.current);
webVitals.onLCP(pushMetricsRef.current);
webVitals.onINP(pushMetricsRef.current);
webVitals.onFCP(pushMetricsRef.current);
webVitals.onTTFB(pushMetricsRef.current);
document.addEventListener("visibilitychange", effectFlushMetrics);
return () => {
document.removeEventListener("visibilitychange", effectFlushMetrics);
};
}, []);
}
const transformWebVitalsMetric = (metric) => {
return {
webVital: metric,
_time: (/* @__PURE__ */ new Date()).getTime(),
source: "web-vital",
path: window.location.pathname
};
};
const createWebVitalsComponent = (logger) => {
const sendMetrics = (metric) => {
logger.raw(transformWebVitalsMetric(metric));
};
const flushMetrics = () => {
logger.flush();
};
return () => {
useReportWebVitals(sendMetrics, flushMetrics);
return /* @__PURE__ */ React__namespace.createElement(React__namespace.Fragment, null);
};
};
exports.createWebVitalsComponent = createWebVitalsComponent;
exports.transformWebVitalsMetric = transformWebVitalsMetric;
exports.useReportWebVitals = useReportWebVitals;
//# sourceMappingURL=web-vitals.cjs.map