Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 11 additions & 3 deletions packages/browser-utils/src/metrics/web-vitals/lib/initUnique.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,16 @@ const instanceMap: WeakMap<object, unknown> = new WeakMap();
* identity object was previously used.
*/
export function initUnique<T>(identityObj: object, ClassObj: new () => T): T {
if (!instanceMap.get(identityObj)) {
instanceMap.set(identityObj, new ClassObj());
try {
if (!instanceMap.get(identityObj)) {
instanceMap.set(identityObj, new ClassObj());
}
return instanceMap.get(identityObj)! as T;
} catch (e) {
// --- START Sentry-custom code (try/catch wrapping) ---
// Fix for cases where identityObj is not a valid key for WeakMap (sometimes a problem in Safari)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

l: Is this issue easily reproducible? If so it'd be nice if we can somehow add a E2E test, if not I'm fine as is

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, sadly it's not :/

// Just return a new instance without caching it in instanceMap
return new ClassObj();
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Missing test for fix PR per review rules

Low Severity · Bugbot Rules

Per the review rules: "When reviewing a fix PR, check if the PR includes at least one unit, integration or e2e test that tests the regression this PR fixes." This fix PR adds error handling for invalid WeakMap keys in Safari but the diff contains no accompanying test to verify the fix works correctly. A test that simulates the Safari TypeError scenario would help prevent regressions.

Fix in Cursor Fix in Web

}
return instanceMap.get(identityObj)! as T;
// --- END Sentry-custom code ---
}
Loading