FIX: Safari iOS page title and url regression when sharing (#11699)

FIX: Safari iOS page title and url regression when sharing (#11699)

diff --git a/app/assets/javascripts/discourse/app/initializers/opengraph-tag-updater.js b/app/assets/javascripts/discourse/app/initializers/opengraph-tag-updater.js
new file mode 100644
index 0000000..66ab52a
--- /dev/null
+++ b/app/assets/javascripts/discourse/app/initializers/opengraph-tag-updater.js
@@ -0,0 +1,20 @@
+import { getAbsoluteURL } from "discourse-common/lib/get-url";
+
+export default {
+  name: "opengraph-tag-updater",
+
+  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']");
+
+    if (ogTitle && ogUrl) {
+      appEvents.on("page:changed", (data) => {
+        ogTitle.setAttribute("content", data.title);
+        ogUrl.setAttribute("content", getAbsoluteURL(data.url));
+      });
+    }
+  },
+};
diff --git a/app/assets/javascripts/discourse/tests/acceptance/opengraph-tag-updater-test.js b/app/assets/javascripts/discourse/tests/acceptance/opengraph-tag-updater-test.js
new file mode 100644
index 0000000..ab3a72e
--- /dev/null
+++ b/app/assets/javascripts/discourse/tests/acceptance/opengraph-tag-updater-test.js
@@ -0,0 +1,32 @@
+import { click, visit } from "@ember/test-helpers";
+import { acceptance } from "discourse/tests/helpers/qunit-helpers";
+import { test } from "qunit";
+
+acceptance("Opengraph Tag Updater", function (needs) {
+  needs.pretender((server, helper) => {
+    server.get("/about", () => {
+      return helper.response({});
+    });
+  });
+
+  test("updates OG title and URL", async function (assert) {
+    await visit("/");
+    await click("#toggle-hamburger-menu");
+    await click("a.about-link");
+
+    assert.equal(
+      document
+        .querySelector("meta[property='og:title']")
+        .getAttribute("content"),
+      document.title,
+      "it should update OG title"
+    );
+    assert.ok(
+      document
+        .querySelector("meta[property='og:url']")
+        .getAttribute("content")
+        .endsWith("/about"),
+      "it should update OG URL"
+    );
+  });
+});
diff --git a/app/views/qunit/index.html.erb b/app/views/qunit/index.html.erb
index eef411c..6109dec 100644
--- a/app/views/qunit/index.html.erb
+++ b/app/views/qunit/index.html.erb
@@ -6,6 +6,8 @@
     <%= javascript_include_tag "test_helper" %>
     <%= csrf_meta_tags %>
     <script src="<%= ExtraLocalesController.url('admin') %>"></script>
+    <meta property="og:title" content="">
+    <meta property="og:url" content="">
   </head>
   <body>
     <div id="qunit"></div>

GitHub sha: 26337408

This commit appears in #11699 which was approved by lis2. It was merged by pmusaraj.