-
Notifications
You must be signed in to change notification settings - Fork 113
fix: widget isn't destroyed on unmount #588
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
base: main
Are you sure you want to change the base?
Conversation
@yawuxi is attempting to deploy a commit to the Cloudinary DevX Team on Vercel. A member of the Team first needs to authorize it. |
If you're not satisfied with the approach of using a ref in the dependency array, I have a few other possible solutions to the problem. |
I spent some more time on this and realized that the cleanup actually works—it works if the unmount doesn't happen immediately. However, it doesn't work if the asynchronous operation inside useEffect() runs during the mount, which causes two instances to be created. Well, it seems that to make this work, I really need to remove this part of the code.
I’d appreciate any feedback, thanks! |
In the latest commit, I found a solution to keep the automatic initialization of the iframe while removing its duplicate. I suggest simply tracking the component's state using a ref. I look forward to your feedback. Thank you! |
Hello! I am pinging the pull request since there was no response, and the error is critical for me as it causes my E2E tests to fail. |
@yawuxi Thank you so much for both the PR, and the ping. Your description of the problem/solution makes sense but I have not made the time to do an actual review. I hope to early next week. |
@eportis-cloudinary Thank you for your response, alright, I’ll wait for the feedback, thanks! |
@yawuxi I finally (!) had some time to try to wrap my brain around this today. TL;DR I am still not sure what the best course of action is and am asking for a second opinion. Adding the isMounted ref makes total sense, but I am trying to wrap my mind around whether or not there are other race conditions that can be triggered in between the time handleOnLoad is called and requestIdelCallback runs and we actually create the widget. If there are, the best course of action might be removing the triggerOnIdle wrapper. Thank you once again for all of the time you put into understanding and clearly communicating about the problem, and exploring a range of potential remedies. And again sincere apologies for the long wait. |
@eportis-cloudinary Hi! Yes, I believe removing |
Hey! I've looked at the problem and I propose a bit different approach to keep the original intent of Please see and test my branch. |
Description
Since the component is mounted, unmounted, and then mounted again, the onLoad event on the <Script /> component from next/script fires multiple times. As a result, two iframes are created instead of one.
I noticed that you implemented a useEffect() to remove the old widget, but it does not work as intended. Below, I will explain why it fails and provide possible solution.
Ensure useEffect() Executes Properly. Currently, the code contains the following useEffect() for cleanup:
This should clean up the previous widget, but the issue is that it does not track updates to the widget.current variable. How to Fix It?
Instead of using an empty dependency array [], update it to track widget.current:
This ensures the cleanup runs correctly when widget.current changes, preventing multiple iframe creations.
Issue Ticket Number
Fixes #587
(#587)
Type of change
Checklist