Skip to content

Commit

Permalink
Avoid undelivered notifications error
Browse files Browse the repository at this point in the history
  • Loading branch information
AngusMorton committed Jul 30, 2024
1 parent 36e1d63 commit e9c77be
Showing 1 changed file with 11 additions and 5 deletions.
16 changes: 11 additions & 5 deletions libraries/browser-tracker-core/src/helpers/browser_props.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,20 +18,26 @@ function useResizeObserver(): boolean {
}

let resizeObserverInitialized = false;
let readBrowserPropertiesTask: number | null = null;
function initializeResizeObserver() {
if (resizeObserverInitialized) {
return;
}
if(!document || !document.body || !document.documentElement) {
if (!document || !document.body || !document.documentElement) {
return;
}
resizeObserverInitialized = true;

const resizeObserver = new ResizeObserver((entries) => {
for (let entry of entries) {
if (entry.target === document.body || entry.target === document.documentElement) {
const resizeObserver = new ResizeObserver(() => {
if (!readBrowserPropertiesTask) {
// The browser property lookup causes a forced synchronous layout when offsets/sizes are
// queried. It's possible that the forced synchronous layout causes the ResizeObserver
// to be fired again, leading to an infinite loop and the "ResizeObserver loop completed
// with undelivered notifications" error.
readBrowserPropertiesTask = requestAnimationFrame(() => {
readBrowserPropertiesTask = null;
cachedProperties = readBrowserProperties();
}
});
}
});
resizeObserver.observe(document.body);
Expand Down

0 comments on commit e9c77be

Please sign in to comment.