Skip to content

Commit

Permalink
ENH: adding debugging mode and different eventlistener
Browse files Browse the repository at this point in the history
  • Loading branch information
eagerterrier committed Jul 14, 2023
1 parent 49985b8 commit 164299a
Showing 1 changed file with 15 additions and 1 deletion.
16 changes: 15 additions & 1 deletion src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {
import useEvent from './use-event';

const noOp = () => {};
let webVitalsDebug = false;

const webVitalsBase = {
age: 0,
Expand Down Expand Up @@ -60,6 +61,15 @@ const sendBeacon = (rawBeacon, reportingEndpoint, reportParams) => {
? appendReportParams(reportingEndpoint, reportParams)
: reportingEndpoint;

if (webVitalsDebug === true) {
console.log('WEBVITALS DEBUG IS ON');
console.log(`In production, WebVitals data would be sent to ${beaconTarget} with the following payload`);
console.dir(rawBeacon);
return new Promise((resolve, reject) => {
resolve();
});
}

if (navigator.sendBeacon) {
const headers = { type: 'application/reports+json' };
const blob = new Blob([beacon], headers);
Expand Down Expand Up @@ -88,8 +98,12 @@ const useWebVitals = ({
loggerCallback = noOp,
sampleRate = 100,
reportParams,
webVitalsListener = 'pagehide',
debug = false,
}) => {
let pageLoadTime;
webVitalsDebug = debug;

const [status, setStatus] = useState({ error: false });
const shouldSendVitals = enabled && shouldSample(sampleRate);

Expand All @@ -111,7 +125,7 @@ const useWebVitals = ({
sendBeacon(beacon, reportingEndpoint, reportParams).catch(loggerCallback);
};

useEvent('pagehide', shouldSendVitals ? sendVitals : noOp);
useEvent(webVitalsListener, shouldSendVitals ? sendVitals : noOp);

useEffect(() => {
try {
Expand Down

0 comments on commit 164299a

Please sign in to comment.