Skip to content

Commit 7a975c9

Browse files
s1gr1dnicohrubec
andauthored
fix(web-vitals): Add error handling for invalid object keys in WeakMap (#18809)
A customer experienced the following issue on iOS Safari 18.6.2: `TypeError: WeakMap keys must be objects or non-registered symbols`. The culprit is probably in web vitals `initUnique` function (which is vendored in). This fix adds a try/catch to handle edge cases where invalid keys are passed to WeakMap, returning a new instance without caching when validation fails. Closes #18810 (added automatically) Co-authored-by: Nicolas Hrubec <[email protected]>
1 parent ca02322 commit 7a975c9

File tree

1 file changed

+11
-3
lines changed

1 file changed

+11
-3
lines changed

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)