Skip to content

Commit

Permalink
web: Prevent postMessage before iframe is loaded
Browse files Browse the repository at this point in the history
  • Loading branch information
laymonage committed Feb 26, 2023
1 parent 776dc3e commit 364940b
Showing 1 changed file with 6 additions and 2 deletions.
8 changes: 6 additions & 2 deletions web/src/giscus.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ export class GiscusWidget extends LitElement {
private __session = '';
private _iframeRef: Ref<HTMLIFrameElement> = createRef();
private messageEventHandler = this.handleMessageEvent.bind(this);
private hasLoaded = false;

get iframeRef() {
return this._iframeRef?.value;
Expand Down Expand Up @@ -217,8 +218,9 @@ export class GiscusWidget extends LitElement {
}

private sendMessage<T>(message: T) {
if (!this.iframeRef || !this.iframeRef.src.startsWith(this.host)) return;
this.iframeRef?.contentWindow?.postMessage({ giscus: message }, this.host);
if (!this.iframeRef || !this.iframeRef.contentWindow || !this.hasLoaded)
return;
this.iframeRef.contentWindow.postMessage({ giscus: message }, this.host);
}

private updateConfig() {
Expand All @@ -245,6 +247,8 @@ export class GiscusWidget extends LitElement {
firstUpdated() {
this.iframeRef?.addEventListener('load', () => {
this.iframeRef?.classList.remove('loading');
this.hasLoaded = true;
// Make sure to update the config in case the iframe is loaded lazily.
this.updateConfig();
});
}
Expand Down

1 comment on commit 364940b

@vercel
Copy link

@vercel vercel bot commented on 364940b Feb 26, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

giscus-component – ./

giscus-component.vercel.app
giscus-component-git-main-giscus.vercel.app
giscus-component-giscus.vercel.app

Please sign in to comment.