-
Notifications
You must be signed in to change notification settings - Fork 19.8k
Fix (Saving Images): Fixed a window proxy issue when saving images in the micro-frontend framework. #21391
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: master
Are you sure you want to change the base?
Conversation
… the micro-frontend framework. Added the `pickEventView` function to handle the window proxy issue in the micro-frontend framework, ensuring that the save image event is triggered correctly in different environments.
|
Thanks for your contribution! Please DO NOT commit the files in dist, i18n, and ssr/client/dist folders in a non-release pull request. These folders are for release use only. |
| $a.download = title + '.' + type; | ||
| $a.target = '_blank'; | ||
| $a.href = url; | ||
| const evt = new MouseEvent('click', { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I am not familiar with the micro-frontend framework. Instead of dispatching a click event, does $a.click() work in that environment?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hi, happy to discuss this further!
In micro-frontend environments, window is often a proxy object (e.g., in qiankun, Micro-app, etc.), which can cause issues when creating MouseEvent with new MouseEvent(..., { view: window }) — for example, throwing errors or behaving unexpectedly.
I believe the key point isn't whether to use element.click() or dispatchEvent, but rather: which actual window object should be used as the view when creating the event.
That’s why this PR replaces document.defaultView with pickEventView() — a safe utility that:
- Tries to use
window.rawWindow(available in frameworks likeMicro-app, wherewindow.proxyholds the realrawWindow). - Falls back to
document.defaultViewif that’s accessible. - Finally defaults to
windowif all else fails.
This ensures the MouseEvent always has a valid, real window as its view, regardless of whether window is a proxy — making the event creation robust in micro-frontend setups.
Importantly, this change doesn’t alter how events are dispatched (we still use dispatchEvent), but it significantly improves reliability by ensuring the correct view context is used.
This makes ECharts more resilient in micro-frontend applications without introducing any behavioral changes.
Thanks for raising this question — happy to discuss further! 😊
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for your explanation. I mean, we can try using $a.click() rather than manually creating and dispatching a click event, since $a.click() doesn't need to pass the window object. If this works in micro-frontend frameworks, I think we can remove this code block.
Fix (Saving Images)
Added the
pickEventViewfunction to handle the window proxy issue in the micro-frontend framework, ensuring that the save image event is triggered correctly in different environments.Brief Information
This pull request is in the type of:
What does this PR do?
This PR fixes an issue where the save image functionality failed in micro-frontend environments due to window proxy interference. The introduced
pickEventViewfunction correctly identifies the appropriate view context for event handling, ensuring consistent behavior across different sandboxed or proxy-ed window environments.Fixed issues
Details
Before: What was the problem?
In micro-frontend architectures (e.g., using Module Federation or iframe-based isolation), the
windowobject is often proxied or sandboxed. When triggering the save image event, the originalwindowreference was used directly, leading to failures in event dispatching or incorrect DOM context resolution. This caused the save image feature to be non-functional or inconsistent across environments.After: How does it behave after the fixing?
With the addition of the
pickEventViewutility function, the system now dynamically resolves the correct view context (e.g., the actualwindowordocumentfrom the current execution context) before triggering the save image event. This ensures the event is properly dispatched and handled regardless of the micro-frontend sandboxing mechanism. The save image functionality now works reliably in all supported environments, including isolated or proxied frames.Document Info
One of the following should be checked.
Other information
This fix is particularly important for users adopting ECharts in micro-frontend setups using tools like Webpack Module Federation, Single-SPA, Micro-App, or custom sandboxing solutions. The
pickEventViewfunction is designed to be lightweight and reusable across similar contexts.