Skip to content

Commit

Permalink
Merge pull request #775 from microsoft/mn/cleanupObs
Browse files Browse the repository at this point in the history
Fix memory leak issues with mutation observers
  • Loading branch information
swaathee authored Feb 25, 2025
2 parents 656b5c0 + e270de4 commit 855e415
Show file tree
Hide file tree
Showing 9 changed files with 22 additions and 14 deletions.
2 changes: 1 addition & 1 deletion lerna.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"packages": [
"packages/*"
],
"version": "0.7.69",
"version": "0.7.70",
"npmClient": "yarn",
"useWorkspaces": true
}
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "clarity",
"private": true,
"version": "0.7.69",
"version": "0.7.70",
"repository": "https://github.com/microsoft/clarity.git",
"author": "Sarvesh Nagpal <[email protected]>",
"license": "MIT",
Expand Down
4 changes: 2 additions & 2 deletions packages/clarity-decode/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "clarity-decode",
"version": "0.7.69",
"version": "0.7.70",
"description": "An analytics library that uses web page interactions to generate aggregated insights",
"author": "Microsoft Corp.",
"license": "MIT",
Expand All @@ -26,7 +26,7 @@
"url": "https://github.com/Microsoft/clarity/issues"
},
"dependencies": {
"clarity-js": "^0.7.67"
"clarity-js": "^0.7.70"
},
"devDependencies": {
"@rollup/plugin-commonjs": "^24.0.0",
Expand Down
8 changes: 4 additions & 4 deletions packages/clarity-devtools/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "clarity-devtools",
"version": "0.7.69",
"version": "0.7.70",
"private": true,
"description": "Adds Clarity debugging support to browser devtools",
"author": "Microsoft Corp.",
Expand All @@ -24,9 +24,9 @@
"url": "https://github.com/Microsoft/clarity/issues"
},
"dependencies": {
"clarity-decode": "^0.7.67",
"clarity-js": "^0.7.67",
"clarity-visualize": "^0.7.67"
"clarity-decode": "^0.7.70",
"clarity-js": "^0.7.70",
"clarity-visualize": "^0.7.70"
},
"devDependencies": {
"@rollup/plugin-node-resolve": "^15.0.0",
Expand Down
4 changes: 2 additions & 2 deletions packages/clarity-devtools/static/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
"manifest_version": 2,
"name": "Microsoft Clarity Developer Tools",
"description": "Clarity helps you understand how users are interacting with your website.",
"version": "0.7.69",
"version_name": "0.7.69",
"version": "0.7.70",
"version_name": "0.7.70",
"minimum_chrome_version": "50",
"devtools_page": "devtools.html",
"icons": {
Expand Down
2 changes: 1 addition & 1 deletion packages/clarity-js/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "clarity-js",
"version": "0.7.69",
"version": "0.7.70",
"description": "An analytics library that uses web page interactions to generate aggregated insights",
"author": "Microsoft Corp.",
"license": "MIT",
Expand Down
2 changes: 1 addition & 1 deletion packages/clarity-js/src/core/version.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
let version = "0.7.69";
let version = "0.7.70";
export default version;
8 changes: 8 additions & 0 deletions packages/clarity-js/src/layout/mutation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ let throttleDelay: number = null;
let activePeriod = null;
let history: MutationHistory = {};
let criticalPeriod = null;
let observedNodes: WeakMap<Node, MutationObserver> = new WeakMap<Node, MutationObserver>();

// We ignore mutations if these attributes are updated
const IGNORED_ATTRIBUTES = ["data-google-query-id", "data-load-complete", "data-google-container-id"];
Expand All @@ -47,6 +48,7 @@ export function start(): void {
activePeriod = 0;
history = {};
criticalPeriod = 0;
observedNodes = new WeakMap<Node, MutationObserver>();

// Some popular open source libraries, like styled-components, optimize performance
// by injecting CSS using insertRule API vs. appending text node. A side effect of
Expand Down Expand Up @@ -117,10 +119,16 @@ export function observe(node: Node): void {
// For this reason, we need to wire up mutations every time we see a new shadow dom.
// Also, wrap it inside a try / catch. In certain browsers (e.g. legacy Edge), observer on shadow dom can throw errors
try {
// Cleanup old observer if present.
if (observedNodes.has(node)) {
observedNodes.get(node)?.disconnect();
}

let m = api(Constant.MutationObserver);
let observer = m in window ? new window[m](measure(handle) as MutationCallback) : null;
if (observer) {
observer.observe(node, { attributes: true, childList: true, characterData: true, subtree: true });
observedNodes.set(node, observer);
observers.push(observer);
}
} catch (e) {
Expand Down
4 changes: 2 additions & 2 deletions packages/clarity-visualize/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "clarity-visualize",
"version": "0.7.69",
"version": "0.7.70",
"description": "An analytics library that uses web page interactions to generate aggregated insights",
"author": "Microsoft Corp.",
"license": "MIT",
Expand All @@ -27,7 +27,7 @@
"url": "https://github.com/Microsoft/clarity/issues"
},
"dependencies": {
"clarity-decode": "^0.7.67"
"clarity-decode": "^0.7.70"
},
"devDependencies": {
"@rollup/plugin-commonjs": "^24.0.0",
Expand Down

0 comments on commit 855e415

Please sign in to comment.