From b1dcd9d2980ae20538cb67654603ade31558d87b Mon Sep 17 00:00:00 2001 From: Petr Heinz Date: Wed, 16 Aug 2023 13:58:34 +0200 Subject: [PATCH 1/3] Add keepalive: true option to fetch in Browser package --- packages/browser/package.json | 2 +- packages/browser/src/browser.ts | 5 ++-- packages/browser/yarn.lock | 45 +++++++++++++++------------------ 3 files changed, 24 insertions(+), 28 deletions(-) diff --git a/packages/browser/package.json b/packages/browser/package.json index 14ba198..655b4bc 100644 --- a/packages/browser/package.json +++ b/packages/browser/package.json @@ -55,7 +55,7 @@ "dependencies": { "@logtail/core": "^0.4.6", "@logtail/tools": "^0.4.6", - "cross-fetch": "^3.0.4" + "whatwg-fetch": "^3.6.17" }, "gitHead": "0f816cacc21b352576a5707741f9151aa1481041" } diff --git a/packages/browser/src/browser.ts b/packages/browser/src/browser.ts index a11d449..2bdf007 100644 --- a/packages/browser/src/browser.ts +++ b/packages/browser/src/browser.ts @@ -1,4 +1,4 @@ -import fetch from "cross-fetch"; +import 'whatwg-fetch'; import { ILogtailLog, ILogtailOptions } from "@logtail/types"; import { Base } from "@logtail/core"; @@ -25,7 +25,8 @@ export class Browser extends Base { // Awaiting: https://bugs.chromium.org/p/chromium/issues/detail?id=571722 // "User-Agent": getUserAgent() }, - body: JSON.stringify(logs) + body: JSON.stringify(logs), + keepalive: true, } ); diff --git a/packages/browser/yarn.lock b/packages/browser/yarn.lock index b181b29..31e4aeb 100644 --- a/packages/browser/yarn.lock +++ b/packages/browser/yarn.lock @@ -26,25 +26,25 @@ "@babel/helper-validator-identifier" "^7.19.1" to-fast-properties "^2.0.0" -"@logtail/core@^0.4.0": - version "0.4.0" - resolved "https://registry.yarnpkg.com/@logtail/core/-/core-0.4.0.tgz#a8e510756bf9fd1b2cc6c901225eb03685919ca8" - integrity sha512-xItEU5vY2LvcJyAVkxKKZ3UvG3CsK2pMvhuLDntlC+US1LlNcfQmVzvInN5j5iXl0kjNO6n+0EY2YyhhOgjWCQ== +"@logtail/core@^0.4.6": + version "0.4.6" + resolved "https://registry.yarnpkg.com/@logtail/core/-/core-0.4.6.tgz#873aea3b7c79d209b07c1fc0834bf2dc34909f94" + integrity sha512-lUGLtxZoZGgA8WoEOkH3XhldeM7J4+Ou3eIUaxUY0BHMgQSumWIsXlqEaPn6zxLM+b0cyam8z9HH9kUBBnr6Sw== dependencies: - "@logtail/tools" "^0.4.0" - "@logtail/types" "^0.4.0" + "@logtail/tools" "^0.4.6" + "@logtail/types" "^0.4.6" -"@logtail/tools@^0.4.0": - version "0.4.0" - resolved "https://registry.yarnpkg.com/@logtail/tools/-/tools-0.4.0.tgz#ff1bed193a005b12007df510f6ce294b8f518207" - integrity sha512-7lSKdJTq7NMUvriMhkf6cTxY9QlXi+YLLuHxbSOKI56HpJN8qJGPVcmOdzlbk8pJofe9RnME8hBMLTHAD3bmsw== +"@logtail/tools@^0.4.6": + version "0.4.6" + resolved "https://registry.yarnpkg.com/@logtail/tools/-/tools-0.4.6.tgz#99b9d0f5cac4ffad740d734d311b0ce5a42c2918" + integrity sha512-fcUg/OUTgdaEYUI0SQXQXOeX51+0Xh5r+3ypfbIrovMpeEkyAT12ivs+0vXX1y3uF+XoAYWWXNULGMtUl5KLZg== dependencies: - "@logtail/types" "^0.4.0" + "@logtail/types" "^0.4.6" -"@logtail/types@^0.4.0": - version "0.4.0" - resolved "https://registry.yarnpkg.com/@logtail/types/-/types-0.4.0.tgz#39c28e08f805657c70107071dc166866153563a8" - integrity sha512-2CR3w7Xf5rKWbUlgiaBKeVrqWuWnVBz0Ymycw/gYwupVI0fb6Ameid9fHfeM5LI/gbIO3ZTIMADlA0FvFbuXMQ== +"@logtail/types@^0.4.6": + version "0.4.6" + resolved "https://registry.yarnpkg.com/@logtail/types/-/types-0.4.6.tgz#522cf4ee68daff00aab9362fcd0ff78ba97660e0" + integrity sha512-+dlM4hk9MfA9Wes3lk8vBk1P0X+eXHxKkQg8kn6U+A1n8DlL3d4ptFkt19R6axVIvDQRNZ8mAZ0CcYsCxCnPrg== dependencies: js "^0.1.0" @@ -745,12 +745,6 @@ create-hmac@^1.1.0, create-hmac@^1.1.4, create-hmac@^1.1.7: safe-buffer "^5.0.1" sha.js "^2.4.8" -cross-fetch@^3.0.4: - version "3.1.2" - resolved "https://registry.yarnpkg.com/cross-fetch/-/cross-fetch-3.1.2.tgz#ee0c2f18844c4fde36150c2a4ddc068d20c1bc41" - dependencies: - node-fetch "2.6.1" - cross-spawn@^6.0.5: version "6.0.5" resolved "https://registry.yarnpkg.com/cross-spawn/-/cross-spawn-6.0.5.tgz#4a5ec7c64dfae22c3a14124dbacdee846d80cbc4" @@ -1813,10 +1807,6 @@ nock@^10.0.6: qs "^6.5.1" semver "^5.5.0" -node-fetch@2.6.1: - version "2.6.1" - resolved "https://registry.yarnpkg.com/node-fetch/-/node-fetch-2.6.1.tgz#045bd323631f76ed2e2b55573394416b639a0052" - node-libs-browser@^2.2.1: version "2.2.1" resolved "https://registry.yarnpkg.com/node-libs-browser/-/node-libs-browser-2.2.1.tgz#b64f513d18338625f90346d27b0d235e631f6425" @@ -2838,6 +2828,11 @@ webpack@^4.39.2: watchpack "^1.7.4" webpack-sources "^1.4.1" +whatwg-fetch@^3.6.17: + version "3.6.17" + resolved "https://registry.yarnpkg.com/whatwg-fetch/-/whatwg-fetch-3.6.17.tgz#009bbbfc122b227b74ba1ff31536b3a1a0e0e212" + integrity sha512-c4ghIvG6th0eudYwKZY5keb81wtFz9/WeAHAoy8+r18kcWlitUIrmGFQ2rWEl4UCKUilD3zCLHOIPheHx5ypRQ== + which-boxed-primitive@^1.0.1: version "1.0.2" resolved "https://registry.yarnpkg.com/which-boxed-primitive/-/which-boxed-primitive-1.0.2.tgz#13757bc89b209b049fe5d86430e21cf40a89a8e6" From fe982a0d05dfbc162b0966548f373aae85fd1703 Mon Sep 17 00:00:00 2001 From: Petr Heinz Date: Wed, 16 Aug 2023 14:49:13 +0200 Subject: [PATCH 2/3] Revert to cross-fetch, but use it as polyfill --- packages/browser/package.json | 2 +- packages/browser/src/browser.ts | 2 +- packages/browser/yarn.lock | 15 ++++++++++----- 3 files changed, 12 insertions(+), 7 deletions(-) diff --git a/packages/browser/package.json b/packages/browser/package.json index 655b4bc..14ba198 100644 --- a/packages/browser/package.json +++ b/packages/browser/package.json @@ -55,7 +55,7 @@ "dependencies": { "@logtail/core": "^0.4.6", "@logtail/tools": "^0.4.6", - "whatwg-fetch": "^3.6.17" + "cross-fetch": "^3.0.4" }, "gitHead": "0f816cacc21b352576a5707741f9151aa1481041" } diff --git a/packages/browser/src/browser.ts b/packages/browser/src/browser.ts index 2bdf007..75fa916 100644 --- a/packages/browser/src/browser.ts +++ b/packages/browser/src/browser.ts @@ -1,4 +1,4 @@ -import 'whatwg-fetch'; +import 'cross-fetch/polyfill'; import { ILogtailLog, ILogtailOptions } from "@logtail/types"; import { Base } from "@logtail/core"; diff --git a/packages/browser/yarn.lock b/packages/browser/yarn.lock index 31e4aeb..19699f6 100644 --- a/packages/browser/yarn.lock +++ b/packages/browser/yarn.lock @@ -745,6 +745,12 @@ create-hmac@^1.1.0, create-hmac@^1.1.4, create-hmac@^1.1.7: safe-buffer "^5.0.1" sha.js "^2.4.8" +cross-fetch@^3.0.4: + version "3.1.2" + resolved "https://registry.yarnpkg.com/cross-fetch/-/cross-fetch-3.1.2.tgz#ee0c2f18844c4fde36150c2a4ddc068d20c1bc41" + dependencies: + node-fetch "2.6.1" + cross-spawn@^6.0.5: version "6.0.5" resolved "https://registry.yarnpkg.com/cross-spawn/-/cross-spawn-6.0.5.tgz#4a5ec7c64dfae22c3a14124dbacdee846d80cbc4" @@ -1807,6 +1813,10 @@ nock@^10.0.6: qs "^6.5.1" semver "^5.5.0" +node-fetch@2.6.1: + version "2.6.1" + resolved "https://registry.yarnpkg.com/node-fetch/-/node-fetch-2.6.1.tgz#045bd323631f76ed2e2b55573394416b639a0052" + node-libs-browser@^2.2.1: version "2.2.1" resolved "https://registry.yarnpkg.com/node-libs-browser/-/node-libs-browser-2.2.1.tgz#b64f513d18338625f90346d27b0d235e631f6425" @@ -2828,11 +2838,6 @@ webpack@^4.39.2: watchpack "^1.7.4" webpack-sources "^1.4.1" -whatwg-fetch@^3.6.17: - version "3.6.17" - resolved "https://registry.yarnpkg.com/whatwg-fetch/-/whatwg-fetch-3.6.17.tgz#009bbbfc122b227b74ba1ff31536b3a1a0e0e212" - integrity sha512-c4ghIvG6th0eudYwKZY5keb81wtFz9/WeAHAoy8+r18kcWlitUIrmGFQ2rWEl4UCKUilD3zCLHOIPheHx5ypRQ== - which-boxed-primitive@^1.0.1: version "1.0.2" resolved "https://registry.yarnpkg.com/which-boxed-primitive/-/which-boxed-primitive-1.0.2.tgz#13757bc89b209b049fe5d86430e21cf40a89a8e6" From 06be28cf26b0a04e842d06c4ccfaeddfba43341c Mon Sep 17 00:00:00 2001 From: Petr Heinz Date: Wed, 16 Aug 2023 15:55:29 +0200 Subject: [PATCH 3/3] Browser: configure flush on page leave --- packages/browser/src/browser.ts | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/packages/browser/src/browser.ts b/packages/browser/src/browser.ts index 75fa916..cb05b6d 100644 --- a/packages/browser/src/browser.ts +++ b/packages/browser/src/browser.ts @@ -43,5 +43,19 @@ export class Browser extends Base { // Set the throttled sync function this.setSync(sync); + + this.configureFlushOnPageLeave(); + } + + private configureFlushOnPageLeave() { + if (typeof document === 'undefined') { + return; + } + + document.addEventListener('visibilitychange', () => { + if (document.visibilityState === 'hidden') { + this.flush(); + } + }); } }