0
Fork 0
mirror of https://github.com/TryGhost/Ghost.git synced 2025-02-03 23:00:14 -05:00

Fixed <GhExploreIframe> leaking event listeners and throwing errors in tests

no issue

- added cleanup of the `message` event handler added to `window` when the component is destroyed
- added a guard to the event handler method to abort early if a message is received whilst the component is being destroyed
This commit is contained in:
Kevin Ansfield 2022-11-03 10:25:43 +00:00
parent 86751f902c
commit dac5cca899

View file

@ -7,26 +7,37 @@ export default class GhExploreIframe extends Component {
@service router; @service router;
@service feature; @service feature;
willDestroy() {
super.willDestroy(...arguments);
window.removeEventListener('message', this.handleIframeMessage);
}
@action @action
setup() { setup() {
this.explore.getExploreIframe().src = this.explore.getIframeURL(); this.explore.getExploreIframe().src = this.explore.getIframeURL();
window.addEventListener('message', this.handleIframeMessage);
}
window.addEventListener('message', async (event) => { @action
// only process messages coming from the explore iframe async handleIframeMessage(event) {
if (event?.data && this.explore.getIframeURL().includes(event?.origin)) { if (this.isDestroyed || this.isDestroying) {
if (event.data?.request === 'apiUrl') { return;
this._handleUrlRequest(); }
}
if (event.data?.route) { // only process messages coming from the explore iframe
this._handleRouteUpdate(event.data); if (event?.data && this.explore.getIframeURL().includes(event?.origin)) {
} if (event.data?.request === 'apiUrl') {
this._handleUrlRequest();
if (event.data?.siteData) {
this._handleSiteDataUpdate(event.data);
}
} }
});
if (event.data?.route) {
this._handleRouteUpdate(event.data);
}
if (event.data?.siteData) {
this._handleSiteDataUpdate(event.data);
}
}
} }
// The iframe can send route updates to navigate to within Admin, as some routes // The iframe can send route updates to navigate to within Admin, as some routes