UNPKG

@sentry/browser

Version:
31 lines (30 loc) • 1.35 kB
import { DEBUG_BUILD } from "../debug-build.js"; import { WINDOW } from "../helpers.js"; import { SPAN_STATUS_ERROR, debug, getActiveSpan, getRootSpan, spanToJSON } from "@sentry/core"; import { SENTRY_CANCELLATION_REASON, SENTRY_OP } from "@sentry/conventions/attributes"; // src/tracing/backgroundtab.ts /** * Add a listener that cancels and finishes a transaction when the global * document is hidden. */ function registerBackgroundTabDetection() { if (WINDOW.document) WINDOW.document.addEventListener("visibilitychange", () => { const activeSpan = getActiveSpan(); if (!activeSpan) return; const rootSpan = getRootSpan(activeSpan); if (WINDOW.document.hidden && rootSpan) { const cancelledStatus = "cancelled"; const { attributes: { [SENTRY_OP]: op }, status } = spanToJSON(rootSpan); if (DEBUG_BUILD) debug.log(`[Tracing] Transaction: ${cancelledStatus} -> since tab moved to the background, op: ${op}`); if (status === "ok") rootSpan.setStatus({ code: SPAN_STATUS_ERROR, message: cancelledStatus }); rootSpan.setAttribute(SENTRY_CANCELLATION_REASON, "document.hidden"); rootSpan.end(); } }); else DEBUG_BUILD && debug.warn("[Tracing] Could not set up background tab detection due to lack of global document"); } export { registerBackgroundTabDetection }; //# sourceMappingURL=backgroundtab.js.map