Skip to content

Commit c041a5a

Browse files
committed
fix: 🐛 修复性能插件中导入方式
1 parent b0adccf commit c041a5a

2 files changed

Lines changed: 51 additions & 51 deletions

File tree

packages/monitor-sdk/src/plugins/performance/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import fetch from './utils/fetch';
2-
import { observerEntries } from './utils/observerEntries';
2+
import observerEntries from './utils/observerEntries';
33
import observerFCP from './utils/observerFcp';
44
import observerLCP from './utils/observerLcp';
55
import observerLoad from './utils/observerLoad';

packages/monitor-sdk/src/plugins/performance/utils/xhr.ts

Lines changed: 50 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -1,71 +1,71 @@
1+
import { TraceSubTypeEnum, TraceTypeEnum } from '../../../common/enum';
2+
import { lazyReportBatch } from '../../../common/report';
3+
import { urlToJson } from '../../../common/utils';
4+
import { AjaxType } from '../../../types';
5+
16
export const originalProto = XMLHttpRequest.prototype;
2-
export const originalOpen = XMLHttpRequest.prototype.open;
3-
export const originalSend = XMLHttpRequest.prototype.send;
7+
export const originalSend = originalProto.send;
8+
export const originalOpen = originalProto.open;
49

5-
// Map to store metadata for each XMLHttpRequest instance
6-
const xhrMetadata = new WeakMap<
7-
XMLHttpRequest,
8-
{
9-
url?: string | URL;
10-
method?: string;
10+
// 扩展 XMLHttpRequest 类型,允许自定义属性
11+
declare global {
12+
interface XMLHttpRequest {
1113
startTime?: number;
1214
endTime?: number;
1315
duration?: number;
14-
status?: number;
16+
method?: string;
17+
url?: string;
1518
}
16-
>();
19+
}
1720

1821
function overwriteOpenAndSend() {
19-
originalProto.open = function (
20-
this: XMLHttpRequest,
22+
originalProto.open = function newOpen(
2123
method: string,
2224
url: string | URL,
23-
async?: boolean,
24-
username?: string | null,
25-
password?: string | null,
25+
async: boolean = true,
26+
username?: string,
27+
password?: string,
2628
) {
27-
// Store metadata in the WeakMap
28-
xhrMetadata.set(this, { url, method });
29-
(originalOpen as any).call(this, method, url, async, username, password);
29+
// 这将保留原始的 open 方法签名,并确保 async、username 和 password 可选
30+
this.url = url.toString(); // 可能需要转为 string 类型
31+
this.method = method;
32+
originalOpen.apply(this, [method, url, async, username, password]);
3033
};
3134

32-
originalProto.send = function (...args) {
33-
const startTime = performance.now();
34-
const xhr = this as XMLHttpRequest;
35-
36-
// Update startTime in the metadata
37-
const metadata = xhrMetadata.get(xhr) || {};
38-
metadata.startTime = startTime;
39-
xhrMetadata.set(xhr, metadata);
40-
41-
const onloadEnd = () => {
42-
const endTime = performance.now();
43-
const duration = endTime - (metadata.startTime || 0);
44-
const status = xhr.status;
35+
originalProto.send = function newSend(
36+
...args: [Document | XMLHttpRequestBodyInit | null | undefined]
37+
) {
38+
this.addEventListener('loadstart', () => {
39+
this.startTime = Date.now();
40+
});
4541

46-
// Update metadata with endTime, duration, and status
47-
Object.assign(metadata, { endTime, duration, status });
42+
const onLoaded = () => {
43+
this.endTime = Date.now();
44+
this.duration = (this.endTime ?? 0) - (this.startTime ?? 0);
45+
const { url, method, startTime, endTime, duration, status } = this;
46+
const params = (args[0] ? args[0] : urlToJson(url as string)) as string;
4847

49-
const reportData = {
50-
url: metadata.url,
51-
method: metadata.method,
52-
endTime: metadata.endTime,
53-
duration: metadata.duration,
54-
status: metadata.status,
55-
type: 'performance',
56-
success: (status >= 200 && status < 300) || status === 304,
57-
subType: 'xhr',
48+
const reportData: AjaxType = {
49+
status,
50+
duration,
51+
startTime,
52+
endTime,
53+
url,
54+
method: method?.toUpperCase(),
55+
type: TraceTypeEnum.performance,
56+
success: status >= 200 && status < 300,
57+
subType: TraceSubTypeEnum.xhr,
58+
pageUrl: window.location.href,
59+
params,
60+
timestamp: new Date().getTime(),
5861
};
59-
60-
// Remove the event listener to avoid memory leaks
61-
xhr.removeEventListener('loadend', onloadEnd, true);
62-
63-
// You can now use `reportData` for reporting purposes
64-
console.log(reportData); // Example: Replace with actual reporting logic
62+
// todo: 发送数据
63+
lazyReportBatch(reportData);
64+
this.removeEventListener('loadend', onLoaded, true);
6565
};
6666

67-
xhr.addEventListener('loadend', onloadEnd, true);
68-
(originalSend as any).call(xhr, ...args);
67+
this.addEventListener('loadend', onLoaded, true);
68+
originalSend.apply(this, args);
6969
};
7070
}
7171

0 commit comments

Comments
 (0)