@be-link/cls-logger 1.0.11 → 1.0.12
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +430 -68
- package/dist/ClsLoggerCore.d.ts +37 -5
- package/dist/ClsLoggerCore.d.ts.map +1 -1
- package/dist/index.esm.js +379 -62
- package/dist/index.js +379 -62
- package/dist/index.umd.js +379 -62
- package/dist/mini/behaviorMonitor.d.ts.map +1 -1
- package/dist/mini/deviceInfo.d.ts.map +1 -1
- package/dist/mini/errorMonitor.d.ts.map +1 -1
- package/dist/mini.esm.js +278 -49
- package/dist/mini.js +278 -49
- package/dist/types.d.ts +41 -3
- package/dist/types.d.ts.map +1 -1
- package/dist/web/deviceInfo.d.ts.map +1 -1
- package/dist/web/errorMonitor.d.ts.map +1 -1
- package/dist/web/performanceMonitor.d.ts.map +1 -1
- package/dist/web.esm.js +303 -38
- package/dist/web.js +303 -38
- package/package.json +1 -1
package/dist/web.esm.js
CHANGED
|
@@ -178,6 +178,11 @@ class ClsLoggerCore {
|
|
|
178
178
|
this.batchTimerDueAt = null;
|
|
179
179
|
this.initTs = 0;
|
|
180
180
|
this.startupDelayMs = 0;
|
|
181
|
+
this.startupMaxSize = 0; // 启动窗口内的 maxSize,0 表示使用默认计算值
|
|
182
|
+
this.useIdleCallback = false;
|
|
183
|
+
this.idleTimeout = 3000;
|
|
184
|
+
this.pendingIdleCallback = null; // requestIdleCallback 的 id
|
|
185
|
+
this.visibilityCleanup = null;
|
|
181
186
|
// 参考文档:失败缓存 + 重试
|
|
182
187
|
this.failedCacheKey = 'cls_failed_logs';
|
|
183
188
|
this.failedCacheMax = 200;
|
|
@@ -201,7 +206,7 @@ class ClsLoggerCore {
|
|
|
201
206
|
}
|
|
202
207
|
return 'browser';
|
|
203
208
|
}
|
|
204
|
-
init(options) {
|
|
209
|
+
async init(options) {
|
|
205
210
|
this.initTs = Date.now();
|
|
206
211
|
const topicId = options?.tencentCloud?.topicID ?? options?.topic_id ?? options?.topicID ?? this.topicId;
|
|
207
212
|
const endpoint = options?.tencentCloud?.endpoint ?? options?.endpoint ?? this.endpoint;
|
|
@@ -210,7 +215,7 @@ class ClsLoggerCore {
|
|
|
210
215
|
if (!topicId) {
|
|
211
216
|
// eslint-disable-next-line no-console
|
|
212
217
|
console.warn('ClsLogger.init 没有传 topicID/topic_id');
|
|
213
|
-
return;
|
|
218
|
+
return false;
|
|
214
219
|
}
|
|
215
220
|
const nextEnvType = options.envType ?? this.detectEnvType();
|
|
216
221
|
// envType/endpoint/retryTimes 变化时:重置 client(以及可能的 sdk)
|
|
@@ -247,15 +252,21 @@ class ClsLoggerCore {
|
|
|
247
252
|
this.batchMaxSize = options.batch?.maxSize ?? this.batchMaxSize;
|
|
248
253
|
this.batchIntervalMs = options.batch?.intervalMs ?? this.batchIntervalMs;
|
|
249
254
|
this.startupDelayMs = options.batch?.startupDelayMs ?? this.startupDelayMs;
|
|
255
|
+
this.useIdleCallback = options.batch?.useIdleCallback ?? this.useIdleCallback;
|
|
256
|
+
this.idleTimeout = options.batch?.idleTimeout ?? this.idleTimeout;
|
|
257
|
+
// startupMaxSize:启动窗口内的队列阈值,默认为 maxSize * 10(至少 200)
|
|
258
|
+
this.startupMaxSize = options.batch?.startupMaxSize ?? Math.max(this.batchMaxSize * 10, 200);
|
|
250
259
|
this.failedCacheKey = options.failedCacheKey ?? this.failedCacheKey;
|
|
251
260
|
this.failedCacheMax = options.failedCacheMax ?? this.failedCacheMax;
|
|
252
261
|
// 预热(避免首条日志触发 import/初始化开销)
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
262
|
+
const sdkReadyPromise = this.getInstance()
|
|
263
|
+
.then(() => true)
|
|
264
|
+
.catch(() => false);
|
|
256
265
|
if (this.enabled) {
|
|
257
266
|
// 启动时尝试发送失败缓存
|
|
258
267
|
this.flushFailed();
|
|
268
|
+
// 添加页面可见性监听(确保页面关闭时数据不丢失)
|
|
269
|
+
this.setupVisibilityListener();
|
|
259
270
|
// 初始化后立即启动请求监听
|
|
260
271
|
this.startRequestMonitor(options.requestMonitor);
|
|
261
272
|
// 初始化后立即启动错误监控/性能监控
|
|
@@ -264,6 +275,7 @@ class ClsLoggerCore {
|
|
|
264
275
|
// 初始化后立即启动行为埋点(PV/UV/点击)
|
|
265
276
|
this.startBehaviorMonitor(options.behaviorMonitor);
|
|
266
277
|
}
|
|
278
|
+
return sdkReadyPromise;
|
|
267
279
|
}
|
|
268
280
|
getBaseFields() {
|
|
269
281
|
let auto = undefined;
|
|
@@ -292,6 +304,96 @@ class ClsLoggerCore {
|
|
|
292
304
|
return auto;
|
|
293
305
|
return undefined;
|
|
294
306
|
}
|
|
307
|
+
/**
|
|
308
|
+
* 设置页面可见性监听
|
|
309
|
+
* - visibilitychange: 页面隐藏时使用 sendBeacon 发送队列
|
|
310
|
+
* - pagehide: 作为移动端 fallback
|
|
311
|
+
*/
|
|
312
|
+
setupVisibilityListener() {
|
|
313
|
+
if (typeof document === 'undefined' || typeof window === 'undefined')
|
|
314
|
+
return;
|
|
315
|
+
// 避免重复监听
|
|
316
|
+
if (this.visibilityCleanup)
|
|
317
|
+
return;
|
|
318
|
+
const handleVisibilityChange = () => {
|
|
319
|
+
if (document.visibilityState === 'hidden') {
|
|
320
|
+
// 使用微任务延迟 flush,确保 web-vitals 等第三方库的 visibilitychange 回调先执行
|
|
321
|
+
// 这样 LCP/CLS/INP 等指标能先入队,再被 flush 发送
|
|
322
|
+
// 注意:queueMicrotask 比 setTimeout(0) 更可靠,不会被延迟太久
|
|
323
|
+
queueMicrotask(() => {
|
|
324
|
+
this.flushBatchSync();
|
|
325
|
+
});
|
|
326
|
+
}
|
|
327
|
+
};
|
|
328
|
+
const handlePageHide = () => {
|
|
329
|
+
// pagehide 不能延迟,因为浏览器可能立即关闭页面
|
|
330
|
+
// 但 pagehide 通常在 visibilitychange 之后触发,此时队列应该已经包含 web-vitals 指标
|
|
331
|
+
this.flushBatchSync();
|
|
332
|
+
};
|
|
333
|
+
document.addEventListener('visibilitychange', handleVisibilityChange);
|
|
334
|
+
window.addEventListener('pagehide', handlePageHide);
|
|
335
|
+
this.visibilityCleanup = () => {
|
|
336
|
+
document.removeEventListener('visibilitychange', handleVisibilityChange);
|
|
337
|
+
window.removeEventListener('pagehide', handlePageHide);
|
|
338
|
+
};
|
|
339
|
+
}
|
|
340
|
+
/**
|
|
341
|
+
* 同步发送内存队列(使用 sendBeacon)
|
|
342
|
+
* - 用于页面关闭时确保数据发送
|
|
343
|
+
* - sendBeacon 不可用时降级为缓存到 localStorage
|
|
344
|
+
*/
|
|
345
|
+
flushBatchSync() {
|
|
346
|
+
if (this.memoryQueue.length === 0)
|
|
347
|
+
return;
|
|
348
|
+
// 清除定时器
|
|
349
|
+
if (this.batchTimer) {
|
|
350
|
+
try {
|
|
351
|
+
if (this.useIdleCallback && typeof cancelIdleCallback !== 'undefined') {
|
|
352
|
+
cancelIdleCallback(this.batchTimer);
|
|
353
|
+
}
|
|
354
|
+
else {
|
|
355
|
+
clearTimeout(this.batchTimer);
|
|
356
|
+
}
|
|
357
|
+
}
|
|
358
|
+
catch {
|
|
359
|
+
// ignore
|
|
360
|
+
}
|
|
361
|
+
this.batchTimer = null;
|
|
362
|
+
}
|
|
363
|
+
this.batchTimerDueAt = null;
|
|
364
|
+
const logs = [...this.memoryQueue];
|
|
365
|
+
this.memoryQueue = [];
|
|
366
|
+
// 优先使用 sendBeacon(页面关闭时可靠发送)
|
|
367
|
+
if (typeof navigator !== 'undefined' && typeof navigator.sendBeacon === 'function') {
|
|
368
|
+
try {
|
|
369
|
+
const payload = this.buildSendBeaconPayload(logs);
|
|
370
|
+
const blob = new Blob([payload], { type: 'application/json' });
|
|
371
|
+
const url = `${this.endpoint}/structuredlog?topic_id=${this.topicId}`;
|
|
372
|
+
const success = navigator.sendBeacon(url, blob);
|
|
373
|
+
if (!success) {
|
|
374
|
+
// sendBeacon 返回 false 时,降级缓存
|
|
375
|
+
this.cacheFailedReportLogs(logs);
|
|
376
|
+
}
|
|
377
|
+
}
|
|
378
|
+
catch {
|
|
379
|
+
this.cacheFailedReportLogs(logs);
|
|
380
|
+
}
|
|
381
|
+
}
|
|
382
|
+
else {
|
|
383
|
+
// 不支持 sendBeacon,降级缓存到 localStorage
|
|
384
|
+
this.cacheFailedReportLogs(logs);
|
|
385
|
+
}
|
|
386
|
+
}
|
|
387
|
+
/**
|
|
388
|
+
* 构建 sendBeacon 的 payload
|
|
389
|
+
*/
|
|
390
|
+
buildSendBeaconPayload(logs) {
|
|
391
|
+
const logList = logs.map((log) => this.buildReportFields(log));
|
|
392
|
+
return JSON.stringify({
|
|
393
|
+
source: this.source,
|
|
394
|
+
logs: logList,
|
|
395
|
+
});
|
|
396
|
+
}
|
|
295
397
|
startRequestMonitor(requestMonitor) {
|
|
296
398
|
if (this.requestMonitorStarted)
|
|
297
399
|
return;
|
|
@@ -537,32 +639,75 @@ class ClsLoggerCore {
|
|
|
537
639
|
return;
|
|
538
640
|
}
|
|
539
641
|
this.memoryQueue.push(log);
|
|
540
|
-
|
|
642
|
+
const now = Date.now();
|
|
643
|
+
// 判断是否在启动合并窗口内
|
|
644
|
+
const inStartupWindow = this.startupDelayMs > 0 && now - this.initTs < this.startupDelayMs;
|
|
645
|
+
// 启动窗口内使用 startupMaxSize,正常情况使用 batchMaxSize
|
|
646
|
+
const effectiveMaxSize = inStartupWindow ? this.startupMaxSize : this.batchMaxSize;
|
|
647
|
+
if (this.memoryQueue.length >= effectiveMaxSize) {
|
|
541
648
|
void this.flushBatch();
|
|
542
649
|
return;
|
|
543
650
|
}
|
|
544
|
-
const now = Date.now();
|
|
545
651
|
const desiredDueAt = this.getDesiredBatchFlushDueAt(now);
|
|
546
652
|
const desiredDelay = Math.max(0, desiredDueAt - now);
|
|
547
653
|
if (!this.batchTimer) {
|
|
548
654
|
this.batchTimerDueAt = desiredDueAt;
|
|
655
|
+
this.scheduleFlush(desiredDelay);
|
|
656
|
+
return;
|
|
657
|
+
}
|
|
658
|
+
// 启动合并窗口内:如果当前 timer 会"更早"触发,则延后到窗口结束,尽量减少多次发送
|
|
659
|
+
if (this.batchTimerDueAt !== null && this.batchTimerDueAt < desiredDueAt) {
|
|
660
|
+
this.cancelScheduledFlush();
|
|
661
|
+
this.batchTimerDueAt = desiredDueAt;
|
|
662
|
+
this.scheduleFlush(desiredDelay);
|
|
663
|
+
}
|
|
664
|
+
}
|
|
665
|
+
/**
|
|
666
|
+
* 调度批量发送
|
|
667
|
+
* - 先使用 setTimeout 保证最小延迟(desiredDelay)
|
|
668
|
+
* - 若开启 useIdleCallback,在延迟结束后等待浏览器空闲再执行
|
|
669
|
+
*/
|
|
670
|
+
scheduleFlush(desiredDelay) {
|
|
671
|
+
if (this.useIdleCallback && typeof requestIdleCallback !== 'undefined') {
|
|
672
|
+
// 先 setTimeout 保证最小延迟,再 requestIdleCallback 在空闲时执行
|
|
673
|
+
this.batchTimer = setTimeout(() => {
|
|
674
|
+
const idleId = requestIdleCallback(() => {
|
|
675
|
+
this.pendingIdleCallback = null;
|
|
676
|
+
void this.flushBatch();
|
|
677
|
+
}, { timeout: this.idleTimeout });
|
|
678
|
+
this.pendingIdleCallback = idleId;
|
|
679
|
+
}, desiredDelay);
|
|
680
|
+
}
|
|
681
|
+
else {
|
|
549
682
|
this.batchTimer = setTimeout(() => {
|
|
550
683
|
void this.flushBatch();
|
|
551
684
|
}, desiredDelay);
|
|
552
|
-
return;
|
|
553
685
|
}
|
|
554
|
-
|
|
555
|
-
|
|
686
|
+
}
|
|
687
|
+
/**
|
|
688
|
+
* 取消已调度的批量发送
|
|
689
|
+
* - 同时清理 setTimeout 和可能的 requestIdleCallback
|
|
690
|
+
*/
|
|
691
|
+
cancelScheduledFlush() {
|
|
692
|
+
// 清理 setTimeout
|
|
693
|
+
if (this.batchTimer) {
|
|
556
694
|
try {
|
|
557
695
|
clearTimeout(this.batchTimer);
|
|
558
696
|
}
|
|
559
697
|
catch {
|
|
560
698
|
// ignore
|
|
561
699
|
}
|
|
562
|
-
this.
|
|
563
|
-
|
|
564
|
-
|
|
565
|
-
|
|
700
|
+
this.batchTimer = null;
|
|
701
|
+
}
|
|
702
|
+
// 清理可能的 pendingIdleCallback
|
|
703
|
+
if (this.pendingIdleCallback !== null && typeof cancelIdleCallback !== 'undefined') {
|
|
704
|
+
try {
|
|
705
|
+
cancelIdleCallback(this.pendingIdleCallback);
|
|
706
|
+
}
|
|
707
|
+
catch {
|
|
708
|
+
// ignore
|
|
709
|
+
}
|
|
710
|
+
this.pendingIdleCallback = null;
|
|
566
711
|
}
|
|
567
712
|
}
|
|
568
713
|
getDesiredBatchFlushDueAt(nowTs) {
|
|
@@ -575,7 +720,7 @@ class ClsLoggerCore {
|
|
|
575
720
|
}
|
|
576
721
|
return nowTs + this.batchIntervalMs;
|
|
577
722
|
}
|
|
578
|
-
info(message, data = {}) {
|
|
723
|
+
info(message, data = {}, options) {
|
|
579
724
|
let msg = '';
|
|
580
725
|
let extra = {};
|
|
581
726
|
if (message instanceof Error) {
|
|
@@ -591,9 +736,18 @@ class ClsLoggerCore {
|
|
|
591
736
|
extra = data;
|
|
592
737
|
}
|
|
593
738
|
const payload = normalizeFlatFields({ message: msg, ...extra }, 'info');
|
|
594
|
-
|
|
739
|
+
const log = { type: 'info', data: payload, timestamp: Date.now() };
|
|
740
|
+
// info 默认走批量队列,支持 immediate 选项立即发送
|
|
741
|
+
if (options?.immediate) {
|
|
742
|
+
void this.sendReportLogs([log]).catch(() => {
|
|
743
|
+
this.cacheFailedReportLogs([log]);
|
|
744
|
+
});
|
|
745
|
+
}
|
|
746
|
+
else {
|
|
747
|
+
this.report(log);
|
|
748
|
+
}
|
|
595
749
|
}
|
|
596
|
-
warn(message, data = {}) {
|
|
750
|
+
warn(message, data = {}, options) {
|
|
597
751
|
let msg = '';
|
|
598
752
|
let extra = {};
|
|
599
753
|
if (message instanceof Error) {
|
|
@@ -609,9 +763,18 @@ class ClsLoggerCore {
|
|
|
609
763
|
extra = data;
|
|
610
764
|
}
|
|
611
765
|
const payload = normalizeFlatFields({ message: msg, ...extra }, 'warn');
|
|
612
|
-
|
|
766
|
+
const log = { type: 'warn', data: payload, timestamp: Date.now() };
|
|
767
|
+
// warn 默认走批量队列,支持 immediate 选项立即发送
|
|
768
|
+
if (options?.immediate) {
|
|
769
|
+
void this.sendReportLogs([log]).catch(() => {
|
|
770
|
+
this.cacheFailedReportLogs([log]);
|
|
771
|
+
});
|
|
772
|
+
}
|
|
773
|
+
else {
|
|
774
|
+
this.report(log);
|
|
775
|
+
}
|
|
613
776
|
}
|
|
614
|
-
error(message, data = {}) {
|
|
777
|
+
error(message, data = {}, options) {
|
|
615
778
|
let msg = '';
|
|
616
779
|
let extra = {};
|
|
617
780
|
if (message instanceof Error) {
|
|
@@ -627,7 +790,17 @@ class ClsLoggerCore {
|
|
|
627
790
|
extra = data;
|
|
628
791
|
}
|
|
629
792
|
const payload = normalizeFlatFields({ message: msg, ...extra }, 'error');
|
|
630
|
-
|
|
793
|
+
const log = { type: 'error', data: payload, timestamp: Date.now() };
|
|
794
|
+
// error 默认即时上报,除非显式指定 immediate: false
|
|
795
|
+
const immediate = options?.immediate ?? true;
|
|
796
|
+
if (immediate) {
|
|
797
|
+
void this.sendReportLogs([log]).catch(() => {
|
|
798
|
+
this.cacheFailedReportLogs([log]);
|
|
799
|
+
});
|
|
800
|
+
}
|
|
801
|
+
else {
|
|
802
|
+
this.report(log);
|
|
803
|
+
}
|
|
631
804
|
}
|
|
632
805
|
track(trackType, data = {}) {
|
|
633
806
|
if (!trackType)
|
|
@@ -642,10 +815,7 @@ class ClsLoggerCore {
|
|
|
642
815
|
* 立即发送内存队列
|
|
643
816
|
*/
|
|
644
817
|
async flushBatch() {
|
|
645
|
-
|
|
646
|
-
clearTimeout(this.batchTimer);
|
|
647
|
-
this.batchTimer = null;
|
|
648
|
-
}
|
|
818
|
+
this.cancelScheduledFlush();
|
|
649
819
|
this.batchTimerDueAt = null;
|
|
650
820
|
if (this.memoryQueue.length === 0)
|
|
651
821
|
return;
|
|
@@ -751,7 +921,14 @@ class ClsLoggerCore {
|
|
|
751
921
|
// 先清空,再尝试发送
|
|
752
922
|
writeStringStorage(this.failedCacheKey, JSON.stringify([]));
|
|
753
923
|
this.memoryQueue.unshift(...logs);
|
|
754
|
-
|
|
924
|
+
// 触发定时器而非直接 flush,以尊重 startupDelayMs 配置
|
|
925
|
+
if (!this.batchTimer) {
|
|
926
|
+
const now = Date.now();
|
|
927
|
+
const desiredDueAt = this.getDesiredBatchFlushDueAt(now);
|
|
928
|
+
const desiredDelay = Math.max(0, desiredDueAt - now);
|
|
929
|
+
this.batchTimerDueAt = desiredDueAt;
|
|
930
|
+
this.scheduleFlush(desiredDelay);
|
|
931
|
+
}
|
|
755
932
|
}
|
|
756
933
|
/**
|
|
757
934
|
* 统计/计数类日志:按字段展开上报(若 data 为空默认 1)
|
|
@@ -1045,6 +1222,13 @@ function installWebRequestMonitor(report, opts = {}) {
|
|
|
1045
1222
|
const DEFAULT_MAX_TEXT = 4000;
|
|
1046
1223
|
const DEFAULT_DEDUPE_WINDOW_MS = 3000;
|
|
1047
1224
|
const DEFAULT_DEDUPE_MAX_KEYS = 200;
|
|
1225
|
+
/** 默认忽略的无意义错误信息 */
|
|
1226
|
+
const DEFAULT_IGNORE_MESSAGES = [
|
|
1227
|
+
'Script error.',
|
|
1228
|
+
'Script error',
|
|
1229
|
+
/^ResizeObserver loop/,
|
|
1230
|
+
'Permission was denied',
|
|
1231
|
+
];
|
|
1048
1232
|
function truncate$2(s, maxLen) {
|
|
1049
1233
|
if (!s)
|
|
1050
1234
|
return s;
|
|
@@ -1057,11 +1241,22 @@ function sampleHit$1(sampleRate) {
|
|
|
1057
1241
|
return false;
|
|
1058
1242
|
return Math.random() < sampleRate;
|
|
1059
1243
|
}
|
|
1244
|
+
/** 检查消息是否应该被忽略 */
|
|
1245
|
+
function shouldIgnoreMessage(message, ignorePatterns) {
|
|
1246
|
+
if (!message || ignorePatterns.length === 0)
|
|
1247
|
+
return false;
|
|
1248
|
+
return ignorePatterns.some((pattern) => {
|
|
1249
|
+
if (typeof pattern === 'string') {
|
|
1250
|
+
return message === pattern || message.includes(pattern);
|
|
1251
|
+
}
|
|
1252
|
+
return pattern.test(message);
|
|
1253
|
+
});
|
|
1254
|
+
}
|
|
1060
1255
|
function getPagePath$1() {
|
|
1061
1256
|
try {
|
|
1062
1257
|
if (typeof window === 'undefined')
|
|
1063
1258
|
return '';
|
|
1064
|
-
return window.location?.
|
|
1259
|
+
return window.location?.href ?? '';
|
|
1065
1260
|
}
|
|
1066
1261
|
catch {
|
|
1067
1262
|
return '';
|
|
@@ -1152,6 +1347,9 @@ function installBrowserErrorMonitor(report, options) {
|
|
|
1152
1347
|
};
|
|
1153
1348
|
if (event && typeof event === 'object' && 'message' in event) {
|
|
1154
1349
|
payload.message = truncate$2(String(event.message ?? ''), options.maxTextLength);
|
|
1350
|
+
// 检查是否应该忽略此错误
|
|
1351
|
+
if (shouldIgnoreMessage(String(event.message ?? ''), options.ignoreMessages))
|
|
1352
|
+
return;
|
|
1155
1353
|
payload.filename = truncate$2(String(event.filename ?? ''), 500);
|
|
1156
1354
|
payload.lineno = typeof event.lineno === 'number' ? event.lineno : undefined;
|
|
1157
1355
|
payload.colno = typeof event.colno === 'number' ? event.colno : undefined;
|
|
@@ -1190,6 +1388,9 @@ function installBrowserErrorMonitor(report, options) {
|
|
|
1190
1388
|
return;
|
|
1191
1389
|
const reason = event?.reason;
|
|
1192
1390
|
const e = normalizeErrorLike(reason, options.maxTextLength);
|
|
1391
|
+
// 检查是否应该忽略此错误
|
|
1392
|
+
if (shouldIgnoreMessage(e.message, options.ignoreMessages))
|
|
1393
|
+
return;
|
|
1193
1394
|
const payload = {
|
|
1194
1395
|
pagePath: getPagePath$1(),
|
|
1195
1396
|
source: 'unhandledrejection',
|
|
@@ -1219,6 +1420,7 @@ function installWebErrorMonitor(report, opts = {}) {
|
|
|
1219
1420
|
maxTextLength: raw.maxTextLength ?? DEFAULT_MAX_TEXT,
|
|
1220
1421
|
dedupeWindowMs: raw.dedupeWindowMs ?? DEFAULT_DEDUPE_WINDOW_MS,
|
|
1221
1422
|
dedupeMaxKeys: raw.dedupeMaxKeys ?? DEFAULT_DEDUPE_MAX_KEYS,
|
|
1423
|
+
ignoreMessages: raw.ignoreMessages ?? DEFAULT_IGNORE_MESSAGES,
|
|
1222
1424
|
};
|
|
1223
1425
|
installBrowserErrorMonitor(report, options);
|
|
1224
1426
|
}
|
|
@@ -1257,7 +1459,7 @@ function getPagePath() {
|
|
|
1257
1459
|
try {
|
|
1258
1460
|
if (typeof window === 'undefined')
|
|
1259
1461
|
return '';
|
|
1260
|
-
return window.location?.
|
|
1462
|
+
return window.location?.href ?? '';
|
|
1261
1463
|
}
|
|
1262
1464
|
catch {
|
|
1263
1465
|
return '';
|
|
@@ -1429,9 +1631,25 @@ function installBrowserPerformanceMonitor(report, options) {
|
|
|
1429
1631
|
if (!name || shouldIgnoreUrl(name, ignoreUrls))
|
|
1430
1632
|
continue;
|
|
1431
1633
|
const initiatorType = String(entry?.initiatorType ?? '');
|
|
1432
|
-
//
|
|
1634
|
+
// 关注 fetch/xhr/img/script(同时允许 css/other 但不强制)
|
|
1433
1635
|
if (!['xmlhttprequest', 'fetch', 'img', 'script', 'css'].includes(initiatorType))
|
|
1434
1636
|
continue;
|
|
1637
|
+
// 时序分解(便于定位慢在哪个阶段)
|
|
1638
|
+
const domainLookupStart = entry.domainLookupStart ?? 0;
|
|
1639
|
+
const domainLookupEnd = entry.domainLookupEnd ?? 0;
|
|
1640
|
+
const connectStart = entry.connectStart ?? 0;
|
|
1641
|
+
const connectEnd = entry.connectEnd ?? 0;
|
|
1642
|
+
const requestStart = entry.requestStart ?? 0;
|
|
1643
|
+
const responseStart = entry.responseStart ?? 0;
|
|
1644
|
+
const responseEnd = entry.responseEnd ?? 0;
|
|
1645
|
+
const dns = domainLookupEnd - domainLookupStart;
|
|
1646
|
+
const tcp = connectEnd - connectStart;
|
|
1647
|
+
const ttfb = responseStart - requestStart;
|
|
1648
|
+
const download = responseEnd - responseStart;
|
|
1649
|
+
// 缓存检测:transferSize=0 且 decodedBodySize>0 表示缓存命中
|
|
1650
|
+
const transferSize = entry.transferSize ?? 0;
|
|
1651
|
+
const decodedBodySize = entry.decodedBodySize ?? 0;
|
|
1652
|
+
const cached = transferSize === 0 && decodedBodySize > 0;
|
|
1435
1653
|
const payload = {
|
|
1436
1654
|
pagePath: getPagePath(),
|
|
1437
1655
|
metric: 'resource',
|
|
@@ -1439,16 +1657,26 @@ function installBrowserPerformanceMonitor(report, options) {
|
|
|
1439
1657
|
url: truncate$1(name, options.maxTextLength),
|
|
1440
1658
|
startTime: typeof entry?.startTime === 'number' ? entry.startTime : undefined,
|
|
1441
1659
|
duration: typeof entry?.duration === 'number' ? entry.duration : undefined,
|
|
1660
|
+
// 时序分解(跨域资源可能为 0)
|
|
1661
|
+
dns: dns > 0 ? Math.round(dns) : undefined,
|
|
1662
|
+
tcp: tcp > 0 ? Math.round(tcp) : undefined,
|
|
1663
|
+
ttfb: ttfb > 0 ? Math.round(ttfb) : undefined,
|
|
1664
|
+
download: download > 0 ? Math.round(download) : undefined,
|
|
1665
|
+
// 缓存标记
|
|
1666
|
+
cached,
|
|
1442
1667
|
};
|
|
1443
|
-
//
|
|
1444
|
-
if (
|
|
1445
|
-
payload.transferSize =
|
|
1446
|
-
if (typeof entry?.encodedBodySize === 'number')
|
|
1668
|
+
// 尺寸字段(跨域资源可能为 0)
|
|
1669
|
+
if (transferSize > 0)
|
|
1670
|
+
payload.transferSize = transferSize;
|
|
1671
|
+
if (typeof entry?.encodedBodySize === 'number' && entry.encodedBodySize > 0) {
|
|
1447
1672
|
payload.encodedBodySize = entry.encodedBodySize;
|
|
1448
|
-
|
|
1449
|
-
|
|
1450
|
-
|
|
1673
|
+
}
|
|
1674
|
+
if (decodedBodySize > 0)
|
|
1675
|
+
payload.decodedBodySize = decodedBodySize;
|
|
1676
|
+
// 协议和状态
|
|
1677
|
+
if (typeof entry?.nextHopProtocol === 'string' && entry.nextHopProtocol) {
|
|
1451
1678
|
payload.nextHopProtocol = entry.nextHopProtocol;
|
|
1679
|
+
}
|
|
1452
1680
|
if (typeof entry?.responseStatus === 'number')
|
|
1453
1681
|
payload.status = entry.responseStatus;
|
|
1454
1682
|
report(options.reportType, payload);
|
|
@@ -1526,7 +1754,7 @@ function writeUvMeta(key, meta) {
|
|
|
1526
1754
|
function getWebPagePath() {
|
|
1527
1755
|
if (typeof window === 'undefined')
|
|
1528
1756
|
return '';
|
|
1529
|
-
return window.location?.
|
|
1757
|
+
return window.location?.href || '';
|
|
1530
1758
|
}
|
|
1531
1759
|
function buildCommonUvFields(uvId, uvMeta, isFirstVisit) {
|
|
1532
1760
|
return {
|
|
@@ -1837,7 +2065,7 @@ function getBrowserDeviceInfo(options) {
|
|
|
1837
2065
|
if (options.includeNetwork) {
|
|
1838
2066
|
try {
|
|
1839
2067
|
const conn = navigator.connection || navigator.mozConnection || navigator.webkitConnection;
|
|
1840
|
-
if (conn &&
|
|
2068
|
+
if (conn && typeof conn === 'object') {
|
|
1841
2069
|
if (typeof conn.effectiveType === 'string')
|
|
1842
2070
|
out.netEffectiveType = conn.effectiveType;
|
|
1843
2071
|
if (typeof conn.downlink === 'number')
|
|
@@ -1852,6 +2080,43 @@ function getBrowserDeviceInfo(options) {
|
|
|
1852
2080
|
// ignore
|
|
1853
2081
|
}
|
|
1854
2082
|
}
|
|
2083
|
+
if (options.includeNetworkType) {
|
|
2084
|
+
try {
|
|
2085
|
+
const conn = navigator.connection || navigator.mozConnection || navigator.webkitConnection;
|
|
2086
|
+
if (conn && typeof conn === 'object') {
|
|
2087
|
+
const type = conn.type;
|
|
2088
|
+
const eff = conn.effectiveType;
|
|
2089
|
+
let val = '';
|
|
2090
|
+
if (type === 'wifi' || type === 'ethernet') {
|
|
2091
|
+
val = 'WIFI';
|
|
2092
|
+
}
|
|
2093
|
+
else if (type === 'cellular' || type === 'wimax') {
|
|
2094
|
+
if (eff === 'slow-2g' || eff === '2g')
|
|
2095
|
+
val = '2G';
|
|
2096
|
+
else if (eff === '3g')
|
|
2097
|
+
val = '3G';
|
|
2098
|
+
else if (eff === '4g')
|
|
2099
|
+
val = '4G';
|
|
2100
|
+
else
|
|
2101
|
+
val = '4G';
|
|
2102
|
+
}
|
|
2103
|
+
else {
|
|
2104
|
+
// type 未知或不支持,降级使用 effectiveType
|
|
2105
|
+
if (eff === 'slow-2g' || eff === '2g')
|
|
2106
|
+
val = '2G';
|
|
2107
|
+
else if (eff === '3g')
|
|
2108
|
+
val = '3G';
|
|
2109
|
+
else if (eff === '4g')
|
|
2110
|
+
val = '4G';
|
|
2111
|
+
}
|
|
2112
|
+
if (val)
|
|
2113
|
+
out.networkType = val;
|
|
2114
|
+
}
|
|
2115
|
+
}
|
|
2116
|
+
catch {
|
|
2117
|
+
// ignore
|
|
2118
|
+
}
|
|
2119
|
+
}
|
|
1855
2120
|
return out;
|
|
1856
2121
|
}
|
|
1857
2122
|
function createWebDeviceInfoBaseFields(opts) {
|
|
@@ -1881,7 +2146,7 @@ function createWebDeviceInfoBaseFields(opts) {
|
|
|
1881
2146
|
try {
|
|
1882
2147
|
const g = globalThis;
|
|
1883
2148
|
const extra = g[globalKey];
|
|
1884
|
-
if (extra &&
|
|
2149
|
+
if (extra && typeof extra === 'object')
|
|
1885
2150
|
return { ...base, ...extra };
|
|
1886
2151
|
}
|
|
1887
2152
|
catch {
|