Skip to content

Commit ede440d

Browse files
committed
fix(web-vitals): Add error handling for invalid object keys in WeakMap
1 parent 6903407 commit ede440d

1 file changed

Lines changed: 11 additions & 3 deletions

File tree

packages/browser-utils/src/metrics/web-vitals/lib/initUnique.ts

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,16 @@ const instanceMap: WeakMap<object, unknown> = new WeakMap();
2222
* identity object was previously used.
2323
*/
2424
export function initUnique<T>(identityObj: object, ClassObj: new () => T): T {
25-
if (!instanceMap.get(identityObj)) {
26-
instanceMap.set(identityObj, new ClassObj());
25+
try {
26+
if (!instanceMap.get(identityObj)) {
27+
instanceMap.set(identityObj, new ClassObj());
28+
}
29+
return instanceMap.get(identityObj)! as T;
30+
} catch (e) {
31+
// --- START Sentry-custom code (try/catch wrapping) ---
32+
// Fix for cases where identityObj is not a valid key for WeakMap (sometimes a problem in Safari)
33+
// Just return a new instance without caching it in instanceMap
34+
return new ClassObj();
2735
}
28-
return instanceMap.get(identityObj)! as T;
36+
// --- END Sentry-custom code ---
2937
}

0 commit comments

Comments
 (0)