Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Avoid undelivered notifications error #1335

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
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(() => {
Copy link
Contributor

Choose a reason for hiding this comment

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

Unfortunately the requestAnimationFrame API is not supported on all browsers that the JS tracker v3 supports. Could we check if the API is available in the browser and avoid using it if not?

readBrowserPropertiesTask = null;
cachedProperties = readBrowserProperties();
}
});
}
});
resizeObserver.observe(document.body);
Expand Down