Skip to content

Commit

Permalink
web: Ensure custom element is only registered once
Browse files Browse the repository at this point in the history
If it's registered twice, there might be a problem in the user's setup.

However, instead of letting the error be thrown, we'll just silently
ignore it and hope that the registered element is compatible with how
they use it.

Fixes #1043.
  • Loading branch information
laymonage committed Jul 1, 2023
1 parent cd70036 commit 0e44e65
Showing 1 changed file with 8 additions and 1 deletion.
9 changes: 8 additions & 1 deletion web/src/giscus.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,17 @@ import { html, css, LitElement, PropertyDeclaration } from 'lit';
import { customElement, property } from 'lit/decorators.js';
import { createRef, ref, Ref } from 'lit/directives/ref.js';

function safeCustomElement(tagName: string) {
// Prevents re-registering an element.
return customElements.get(tagName)
? (v: ReturnType<ReturnType<typeof customElement>>) => v
: customElement(tagName);
}

/**
* Widget element for giscus.
*/
@customElement('giscus-widget')
@safeCustomElement('giscus-widget')
export class GiscusWidget extends LitElement {
private GISCUS_SESSION_KEY = 'giscus-session';
private GISCUS_DEFAULT_HOST = 'https://giscus.app';
Expand Down

0 comments on commit 0e44e65

Please sign in to comment.