diff --git a/app/assets/javascripts/discourse/app/initializers/opengraph-tag-updater.js b/app/assets/javascripts/discourse/app/initializers/opengraph-tag-updater.js index 66ab52a817f..1d8eba4ae40 100644 --- a/app/assets/javascripts/discourse/app/initializers/opengraph-tag-updater.js +++ b/app/assets/javascripts/discourse/app/initializers/opengraph-tag-updater.js @@ -6,15 +6,21 @@ export default { initialize(container) { // workaround for Safari on iOS 14.3 // seems it has started using opengraph tags when sharing - let appEvents = container.lookup("service:app-events"); - const ogTitle = document.querySelector("meta[property='og:title']"), - ogUrl = document.querySelector("meta[property='og:url']"); + this.appEvents = container.lookup("service:app-events"); + this.ogTitle = document.querySelector("meta[property='og:title']"); + this.ogUrl = document.querySelector("meta[property='og:url']"); - if (ogTitle && ogUrl) { - appEvents.on("page:changed", (data) => { - ogTitle.setAttribute("content", data.title); - ogUrl.setAttribute("content", getAbsoluteURL(data.url)); - }); + if (this.ogTitle && this.ogUrl) { + this.appEvents.on("page:changed", this, this.updateOgAttributes); } }, + + updateOgAttributes(data) { + this.ogTitle.setAttribute("content", data.title); + this.ogUrl.setAttribute("content", getAbsoluteURL(data.url)); + }, + + teardown() { + this.appEvents.off("page:changed", this, this.updateOgAttributes); + }, };