This is an automated email from the ASF dual-hosted git repository.

qiuxiafan pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/skywalking-client-js.git


The following commit(s) were added to refs/heads/master by this push:
     new f568d4a  feat: report data by `navigator.sendbeacon` when the event is 
`beforeunload` (#88)
f568d4a is described below

commit f568d4acdacda0f26bcf7eb2d11a55c902f1e313
Author: Alice2222 <623387...@qq.com>
AuthorDate: Mon May 23 22:54:31 2022 +0800

    feat: report data by `navigator.sendbeacon` when the event is 
`beforeunload` (#88)
---
 src/errors/promise.ts  |  1 +
 src/services/report.ts | 12 ++++++++++++
 src/services/task.ts   |  3 ++-
 src/trace/segment.ts   |  2 +-
 4 files changed, 16 insertions(+), 2 deletions(-)

diff --git a/src/errors/promise.ts b/src/errors/promise.ts
index 543a9a1..19664e6 100644
--- a/src/errors/promise.ts
+++ b/src/errors/promise.ts
@@ -47,6 +47,7 @@ class PromiseErrors extends Base {
           stack: event.reason.stack,
           collector: options.collector,
         };
+
         this.traceInfo();
       } catch (error) {
         console.log(error);
diff --git a/src/services/report.ts b/src/services/report.ts
index 96ddae4..bc70e5f 100644
--- a/src/services/report.ts
+++ b/src/services/report.ts
@@ -65,5 +65,17 @@ class Report {
     };
     xhr.send(JSON.stringify(data));
   }
+
+  public sendByBeacon(data: any) {
+    if (!this.url) {
+      return;
+    }
+    if (typeof navigator.sendBeacon === 'function') {
+      navigator.sendBeacon(this.url, JSON.stringify(data));
+      return;
+    }
+
+    this.sendByXhr(data);
+  }
 }
 export default Report;
diff --git a/src/services/task.ts b/src/services/task.ts
index 544c9d0..0b4b7cb 100644
--- a/src/services/task.ts
+++ b/src/services/task.ts
@@ -30,6 +30,7 @@ class TaskQueue {
     if (!(this.queues && this.queues.length)) {
       return;
     }
+
     new Report('ERRORS', this.collector).sendByXhr(this.queues);
     this.queues = [];
   }
@@ -39,7 +40,7 @@ class TaskQueue {
       if (!this.queues.length) {
         return;
       }
-      new Report('ERRORS', this.collector).sendByXhr(this.queues);
+      new Report('ERRORS', this.collector).sendByBeacon(this.queues);
     });
   }
 }
diff --git a/src/trace/segment.ts b/src/trace/segment.ts
index b8ea454..cfc1558 100644
--- a/src/trace/segment.ts
+++ b/src/trace/segment.ts
@@ -30,7 +30,7 @@ export default function traceSegment(options: 
CustomOptionsType) {
     if (!segments.length) {
       return;
     }
-    new Report('SEGMENTS', options.collector).sendByXhr(segments);
+    new Report('SEGMENTS', options.collector).sendByBeacon(segments);
   });
   //report per options.traceTimeInterval min
   setInterval(() => {

Reply via email to