Skip to content

Commit 6f8ef9b

Browse files
committed
Fix(Network): Fix possible "Cannot read property" error by sendBeacon. (issue #615, #629)
1 parent 968f3e2 commit 6f8ef9b

File tree

4 files changed

+21
-6
lines changed

4 files changed

+21
-6
lines changed

CHANGELOG.md

+5-1
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,17 @@
11
English | [简体中文](./CHANGELOG_CN.md)
22

3+
## 3.15.2 (2023-06-??)
4+
5+
- `Fix(Network)` Fix possible "Cannot read property" error by `sendBeacon`. (issue #615, #629)
6+
7+
38
## 3.15.1 (2023-06-01)
49

510
- `Feat(Netwrk)` Add new option `network.ignoreUrlRegExp` to skip some requests. (PR #623)
611
- `Fix(Core)` Fix prototype pollution in `vConsole.setOption()`. (issue #616 #621)
712
- `Fix(Core)` Fix plugin event `ready` triggering before its HTML finishes rendering. (issue #591)
813
- `Fix(Log)` Reset group state when `console.clear()` is called. (issue #611)
914
- `Fix(Log)` Compatible with iOS (less than 13.4) that does not support `ResizeObserver`, but there may be a potential performance issue when printing a large number of logs. (issue #610)
10-
- `Fix(Network)` Fix possible "Cannot read property" error by `sendBeacon`. (issue #615)
1115

1216

1317
## 3.15.0 (2022-11-02)

CHANGELOG_CN.md

+5-1
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,17 @@
11
[English](./CHANGELOG.md) | 简体中文
22

3+
## 3.15.2 (2023-06-??)
4+
5+
- `Fix(Network)` 修复可能由 `sendBeacon` 引发的 "Cannot read property" 错误。(issue #615, #629)
6+
7+
38
## 3.15.1 (2023-06-01)
49

510
- `Feat(Netwrk)` 新增配置项 `network.ignoreUrlRegExp` 以跳过一些请求。 (PR #623)
611
- `Fix(Core)` 修复 `vConsole.setOption()` 中可能存在的原型污染问题。 (issue #616 #621)
712
- `Fix(Core)` 修复插件事件 `ready` 在插件完成渲染前就被触发的问题。 (issue #591)
813
- `Fix(Log)` 修复调用 `console.clear()` 时没有重置 group 层级的问题。 (issue #611)
914
- `Fix(Log)` 兼容 iOS(小于 13.4)不支持 `ResizeObserver` 的情况,代价是打印大批量日志可能会有性能问题。 (issue #610)
10-
- `Fix(Network)` 修复可能由 `sendBeacon` 引发的 "Cannot read property" 错误。 (issue #615)
1115

1216

1317
## 3.15.0 (2022-11-02)

src/network/beacon.proxy.ts

+9-2
Original file line numberDiff line numberDiff line change
@@ -64,9 +64,16 @@ export class BeaconProxyHandler<T extends typeof navigator.sendBeacon> implement
6464
}
6565

6666
export class BeaconProxy {
67-
public static origSendBeacon = navigator.sendBeacon;
67+
public static origSendBeacon = window?.navigator?.sendBeacon;
68+
69+
public static hasSendBeacon() {
70+
return !!BeaconProxy.origSendBeacon;
71+
}
6872

6973
public static create(onUpdateCallback: IOnUpdateCallback) {
70-
return new Proxy(navigator.sendBeacon, new BeaconProxyHandler(onUpdateCallback));
74+
if (!BeaconProxy.hasSendBeacon()) {
75+
return undefined;
76+
}
77+
return new Proxy(BeaconProxy.origSendBeacon, new BeaconProxyHandler(onUpdateCallback));
7178
}
7279
}

src/network/network.model.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ export class VConsoleNetworkModel extends VConsoleModel {
3636
if (window.hasOwnProperty('fetch')) {
3737
window.fetch = FetchProxy.origFetch;
3838
}
39-
if (!!window.navigator.sendBeacon) {
39+
if (BeaconProxy.hasSendBeacon()) {
4040
window.navigator.sendBeacon = BeaconProxy.origSendBeacon;
4141
}
4242
}
@@ -105,7 +105,7 @@ export class VConsoleNetworkModel extends VConsoleModel {
105105
* @private
106106
*/
107107
private mockSendBeacon() {
108-
if (!window?.navigator?.sendBeacon) {
108+
if (!BeaconProxy.hasSendBeacon()) {
109109
return;
110110
}
111111
window.navigator.sendBeacon = BeaconProxy.create((item: VConsoleNetworkRequestItem) => {

0 commit comments

Comments
 (0)