Skip to content

Commit 649f200

Browse files
authored
fix(track): don't throw on revoked proxies @W-17739481 (#5192)
1 parent e0437d3 commit 649f200

File tree

6 files changed

+52
-1
lines changed

6 files changed

+52
-1
lines changed

packages/@lwc/engine-core/src/framework/mutation-logger.ts

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,18 @@ function safelyCallGetter(target: any, key: PropertyKey) {
6060
}
6161
}
6262

63+
function isRevokedProxy(target: object) {
64+
try {
65+
// `str in obj` will never throw for normal objects or active proxies,
66+
// but the operation is not allowed for revoked proxies
67+
// eslint-disable-next-line @typescript-eslint/no-unused-expressions
68+
'' in target;
69+
return false;
70+
} catch (_) {
71+
return true;
72+
}
73+
}
74+
6375
/**
6476
* Flush all the logs we've written so far and return the current logs.
6577
*/
@@ -126,7 +138,9 @@ export function trackTargetForMutationLogging(key: PropertyKey, target: any) {
126138
// Guard against recursive objects - don't traverse forever
127139
return;
128140
}
129-
if (isObject(target) && !isNull(target)) {
141+
142+
// Revoked proxies (e.g. window props in LWS sandboxes) throw an error if we try to track them
143+
if (isObject(target) && !isNull(target) && !isRevokedProxy(target)) {
130144
// only track non-primitives; others are invalid as WeakMap keys
131145
targetsToPropertyKeys.set(target, key);
132146

packages/@lwc/engine-server/src/__tests__/fixtures/track-revoked-proxy-fails/error.txt

Whitespace-only changes.
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<x-component>
2+
<template shadowrootmode="open">
3+
I should render!
4+
</template>
5+
</x-component>
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
export const tagName = 'x-component';
2+
export { default } from 'x/component';
3+
export * from 'x/component';
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
<template>
2+
I should render!
3+
</template>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import { LightningElement, track } from 'lwc';
2+
import tmpl from './component.html';
3+
4+
const { revoke, proxy } = Proxy.revocable({}, {});
5+
revoke();
6+
7+
export default class Rehydration extends LightningElement {
8+
// Doesn't need to be used, just needs to be tracked; see W-17739481
9+
@track reactive = proxy;
10+
11+
connectedCallback() {
12+
Promise.resolve().then(() => {
13+
this.reactive = 1;
14+
});
15+
}
16+
17+
render() {
18+
if (!this.rendered) {
19+
this.rendered = true;
20+
} else {
21+
throw new Error('Reactivity should be disabled on SSR.');
22+
}
23+
24+
return tmpl;
25+
}
26+
}

0 commit comments

Comments
 (0)