Closed hsablonniere closed 9 months ago
As said by Elantha
Most of the time the situation isn't really an error, just a notification. That's probably why Chrome and Firefox have decided not to report the errors in the console.
Because of that, I would not try to implement the solution 1.
I find the lit-labs implementation very smart so I would be ok with solution 2.
I agree, I like solution 2 better.
Thank you for the analysis, it helps so much with decision making! :pray:
Thanks a lot for all the context and explanation! :raised_hands: Here's my take based on that:
About the solution 1 within the tests: we went for a quick & dirty solution when we first implemented a11y tests. We also catch other errors (a ChartJS / Canvas issue if I remember correctly). Like the resize observer issue, we chose not to "silence" it at the time because we were unsure of its impact. It would be better to silence it and open a proper issue about this?
The context
We use a
ResizeObserver
for some components so they can adapt their style and/or template, depending on their size (and not the size of the viewport). We mostly adapt on the width, not the height.We wrote a "mixin" called
withResizeObserver
to hide the boilerplate and provide 2 higher level APIs:_onResize()
method on the component and the mixin calls it on each resizeNOTE: we recently modified the mixin to prevent some layout shifts https://github.com/CleverCloud/clever-components/commit/07cc7283ee11dc41d9b27984d60e22af5459d816
The "problem"
When you use a
ResizeObserver
and do something that triggers a resize inside the resize callback, you can potentially create an infinite loop.Thankfully, the spec has a section about this:
TL;DR: browsers must prevent infinite loops and generate an error if it happens.
ResizeObserver loop limit exceeded
ResizeObserver loop completed with undelivered notifications
ResizeObserver loop completed with undelivered notifications
Even when the error is NOT visible in the devtools, it is generated and thrown:
error
events onwindow
.Background research
I read many issues, articles and StackOverflow questions:
The conclusion is: this is not really an error you need to fix but more of a warning you can ignore (most of the time).
The solutions
If our goal is to stop seeing these errors in our CI and error reports, I see 2 solutions.
Solution 1: Prevent the error from happening in the first place
This article by Jon Elantha explains everything about the whole problem (in a react context). He proposes a technique for solution 1.
🙂 Pros:
🤬 Cons:
getBoundingClientRect()
which you should try minimize for performance reasonsunobserve
+observe
which I have no idea if it's a performance reasonsResizeObserver
Solution 2: Prevent the error message from being passed to our tooling
The @lit-labs/virtualizer repo uses a
ResizeObserver
. They don't try to prevent the error from happening but they have a way to silence the error messages so CI and error reports don't see them.Their documentation have a section about
ResizeObserver
loop errors:https://github.com/lit/lit/blob/main/packages/labs/virtualizer/README.md#resizeobserver-loop-limit-exceeded-errors
They provide a helper function to ignore these errors in test environments.
🙂 Pros:
<cc-logs>
componentResizeObserver
:🤬 Cons:
window.onerror
Open questions:
A. Should we use this helper function in production?
B. If we use this helper function in production:
The proposition
For our tests
For production:
window.onerror
WDYT?
The plan
cc-logs
branch to be merged (it brings the @lit-labs/virtualizer dependency)WDYT?