Skip to content

Commit

Permalink
fix: sentry Illegal invocation on sendBeacon
Browse files Browse the repository at this point in the history
  • Loading branch information
fastner committed Aug 13, 2024
1 parent 6c30c3e commit dcf3bd5
Showing 1 changed file with 14 additions and 5 deletions.
19 changes: 14 additions & 5 deletions app/components/hooks/webVitals.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,25 @@ async function sendToAnalytics(metric: Metric) {
}
const body = JSON.stringify(data)

// eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
if (navigator.sendBeacon) {
navigator.sendBeacon("/analytics/performance", body)
} else {
await fetch("/analytics/performance", {
const doSendViaFetch = async () => {
return fetch("/analytics/performance", {
body,
method: "POST",
keepalive: true
})
}

// eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
if (navigator.sendBeacon) {
try {
navigator.sendBeacon("/analytics/performance", body)
} catch {
// Fallback to fetch
await doSendViaFetch()
}
} else {
await doSendViaFetch()
}
}

export function useWebVitals() {
Expand Down

0 comments on commit dcf3bd5

Please sign in to comment.