Writing · JavaScript · Web · Front end
ResizeObserver loop completed with undelivered notifications: the cause and the fix
What the ResizeObserver loop error means, why setting state inside the callback triggers it, and the one-frame deferral that stops it reaching your error handler.
My web app reports uncaught errors to a small endpoint, and anything that arrives there pages me. One of the first messages it ever sent was this:
ResizeObserver loop completed with undelivered notifications.
Older versions of Chrome word the same condition as ResizeObserver loop limit exceeded. Nothing on the page was broken. No user had seen anything wrong. But it had fired through window.onerror, so it looked exactly like a crash. This article explains what the browser is actually telling you, which code pattern causes it, and how I stopped causing it instead of only muting it.
What the message means
A ResizeObserver callback doesn't run at an arbitrary time. The browser delivers resize notifications at one specific point in each frame: after it has calculated layout and before it paints. That placement is what makes the API useful, because your callback sees final sizes and can still change things before the user sees the frame.
It also creates an obvious problem. If your callback changes the page, layout has to run again, and that may resize more observed elements. The browser handles this with a loop: deliver notifications, lay out again, check for new size changes, deliver again. To guarantee the loop ends, each pass may only notify elements that sit deeper in the DOM tree than the shallowest element notified in the previous pass. Changes can ripple downwards, but a callback can't keep re-triggering the element it was called for.
When a pass ends and there are still size changes the browser isn't allowed to deliver, it gives up for this frame, postpones them to the next one, and dispatches an error event on the window with the message above. So the message means: a resize callback changed the size of something at the same depth or higher, in the same frame. Usually that something is the observed element itself.
The postponed notifications are not lost. They arrive one frame later. That is why the page keeps working, and why this "error" is closer to a warning. It is still worth fixing, for two reasons. The skipped pass means the user can see one frame of the wrong layout. And any error monitoring you run will count it as an uncaught exception.
The pattern that causes it
In my case it was a toolbar that collapses its buttons as it gets narrower. The component observed its own width and stored it in state:
useEffect(() => {
const observer = new ResizeObserver(([entry]) => {
setWidth(entry.contentRect.width); // re-renders inside the callback
});
observer.observe(barRef.current);
return () => observer.disconnect();
}, []);
This looks harmless, and with many frameworks it is the first thing anyone writes. The trouble is what the state update does. In Preact (and in React, for updates flushed synchronously), setting state inside the callback can re-render the component and run its layout effects before the callback's delivery pass has finished. My toolbar's render then folded a button into an overflow menu, which changed the toolbar's content and so its size. The observed element was resized inside its own notification. The browser's next check found a new change on an element it was no longer allowed to notify in this frame, and the error fired.
It never happened on every resize. It needed a resize that crossed one of the collapse thresholds, which is why it looked random in the reports.
The fix: leave the frame before changing anything
The rule I now follow is simple: a ResizeObserver callback only records what it saw. It changes nothing until the next frame. A requestAnimationFrame hop is enough:
function useElementWidth(ref) {
const [width, setWidth] = useState(0);
useEffect(() => {
let frame = 0;
const observer = new ResizeObserver(([entry]) => {
const next = entry.contentRect.width;
cancelAnimationFrame(frame);
frame = requestAnimationFrame(() => setWidth(next));
});
observer.observe(ref.current);
return () => {
cancelAnimationFrame(frame);
observer.disconnect();
};
}, [ref]);
return width;
}
The state update now happens at the start of the following frame, outside the delivery loop. If that render resizes the toolbar again, the observer simply reports it in that frame's own delivery pass, as a fresh notification. There is nothing left over for the browser to complain about.
Three details matter:
- Cancel the pending frame when a newer notification arrives and when the component unmounts. Otherwise a late callback can set state on a component that is gone.
- Keep the measurement in one hook. Both of my toolbars use the same one, so nobody writes a new observer with a state update directly in the callback.
- The cost is one frame of latency, about 16 ms. For collapsing toolbar buttons nobody can see it. If you need the change to land in the same frame, write to the DOM directly inside the callback in a way that can't resize the observed element, such as changing a child that is absolutely positioned.
Things that look like fixes and aren't
- Wrapping the callback in try/catch. Your callback doesn't throw. The browser raises the error itself after the loop, so there is nothing to catch.
- Debouncing with a timer. It works, but it adds visible lag and still needs the same cleanup. A single animation frame is the smallest delay that leaves the loop.
- Only filtering the message in your error handler. This hides your own layout feedback loops along with everyone else's. I do filter it (see below), but only after removing the cause from my own code.
Why I still drop it in error reporting
After the fix, the message still arrived now and then. Browser extensions and third-party scripts run ResizeObservers in the page too, and their loops raise the same window-level event, which my handler hears. The event carries a message and no useful stack, so there is nothing to tell me whose observer it was.
So the reporter drops this exact message, and its older Chrome wording, before sending anything. I added the same rule to the server endpoint that receives the reports. That second copy is not redundant. The app is served from a service-worker cache, which means some visitors keep running an old bundle for weeks after I ship a fix. A filter that exists only in the newest client does nothing about reports from the old ones. I wrote more about that problem in Your crash reporter is paging you for someone else's code.
Summary
- The message means a resize callback caused another resize the browser couldn't deliver in the same frame. Delivery is postponed, not lost.
- The usual cause is a state update inside the callback whose render changes the observed element's size.
- Record the size in the callback and apply it in
requestAnimationFrame. Cancel the frame on new notifications and on unmount. - Filter the message in error reporting only after your own observers are clean, and filter it on the server too if old clients can linger.
