Skip to content

Commit

Permalink
fix(track): don't throw on revoked proxies @W-17739481 (#5192)
Browse files Browse the repository at this point in the history
  • Loading branch information
wjhsf authored Feb 5, 2025
1 parent e0437d3 commit 649f200
Show file tree
Hide file tree
Showing 6 changed files with 52 additions and 1 deletion.
16 changes: 15 additions & 1 deletion packages/@lwc/engine-core/src/framework/mutation-logger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,18 @@ function safelyCallGetter(target: any, key: PropertyKey) {
}
}

function isRevokedProxy(target: object) {
try {
// `str in obj` will never throw for normal objects or active proxies,
// but the operation is not allowed for revoked proxies
// eslint-disable-next-line @typescript-eslint/no-unused-expressions
'' in target;
return false;
} catch (_) {
return true;
}
}

/**
* Flush all the logs we've written so far and return the current logs.
*/
Expand Down Expand Up @@ -126,7 +138,9 @@ export function trackTargetForMutationLogging(key: PropertyKey, target: any) {
// Guard against recursive objects - don't traverse forever
return;
}
if (isObject(target) && !isNull(target)) {

// Revoked proxies (e.g. window props in LWS sandboxes) throw an error if we try to track them
if (isObject(target) && !isNull(target) && !isRevokedProxy(target)) {
// only track non-primitives; others are invalid as WeakMap keys
targetsToPropertyKeys.set(target, key);

Expand Down
Empty file.
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<x-component>
<template shadowrootmode="open">
I should render!
</template>
</x-component>
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export const tagName = 'x-component';
export { default } from 'x/component';
export * from 'x/component';
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<template>
I should render!
</template>
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import { LightningElement, track } from 'lwc';
import tmpl from './component.html';

const { revoke, proxy } = Proxy.revocable({}, {});
revoke();

export default class Rehydration extends LightningElement {
// Doesn't need to be used, just needs to be tracked; see W-17739481
@track reactive = proxy;

connectedCallback() {
Promise.resolve().then(() => {
this.reactive = 1;
});
}

render() {
if (!this.rendered) {
this.rendered = true;
} else {
throw new Error('Reactivity should be disabled on SSR.');
}

return tmpl;
}
}

0 comments on commit 649f200

Please sign in to comment.