From 4c2ff7f6cbd9127b9fc18648dd1af0d6742ab7e0 Mon Sep 17 00:00:00 2001 From: izayl Date: Mon, 16 Oct 2023 16:58:16 +0800 Subject: [PATCH] fix: beacon throw --- src/logger.ts | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/src/logger.ts b/src/logger.ts index 7992f229..82ac2e52 100644 --- a/src/logger.ts +++ b/src/logger.ts @@ -181,7 +181,13 @@ export class Logger { } else if (config.isBrowser && isVercel && navigator.sendBeacon) { // sendBeacon fails if message size is greater than 64kb, so // we fall back to fetch. - if (!navigator.sendBeacon(url, body)) { + // Navigator has to be bound to ensure it does not error in some browsers + // https://xgwang.me/posts/you-may-not-know-beacon/#it-may-throw-error%2C-be-sure-to-catch + try { + if (!navigator.sendBeacon.bind(navigator)(url, body)) { + return sendFallback(); + } + } catch (error) { return sendFallback(); } } else {