Skip to content

Commit

Permalink
feat: add intercom with lazy loading
Browse files Browse the repository at this point in the history
  • Loading branch information
StephenFluin committed Oct 15, 2024
1 parent a670b52 commit b035965
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 2 deletions.
3 changes: 3 additions & 0 deletions astro.config.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@ export default defineConfig({
integrations: [react(), mdx(), sitemap()],
site: "https://docs.axelar.dev",
trailingSlash: "always",
devToolbar: {
enabled: false,
},
markdown: {
remarkPlugins: [
setDefaultLayout,
Expand Down
11 changes: 9 additions & 2 deletions src/layouts/MainLayout.astro
Original file line number Diff line number Diff line change
Expand Up @@ -73,8 +73,15 @@ const canonicalURL = new URL(Astro.url.pathname, 'https://docs.axelar.dev');
}
</script>

</script>
<script src="/src/scripts/index.ts"></script>
</script>
<script>
window.intercomSettings = {
api_base: "https://api-iam.intercom.io",
app_id: "rxsoqwkv",
};
</script>
<script src="/src/scripts/index.ts"></script>

</head>
<body>
<nav>
Expand Down
1 change: 1 addition & 0 deletions src/scripts/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,6 @@ import "./menu";
import "./dark";
import { showToast } from "./toast";
import "./collapsible-nav";
import "./intercom";

window["showToast"] = showToast;
43 changes: 43 additions & 0 deletions src/scripts/intercom.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
function loadIntercom() {
(function () {
const w: any = window;
const ic = w.Intercom;
if (typeof ic === "function") {
ic("reattach_activator");
ic("update", w.intercomSettings);
} else {
const d = document;
const i = function () {
i.c(arguments);
};
i.q = [];
i.c = function (args) {
i.q.push(args);
};
w.Intercom = i;
const s = d.createElement("script");
s.type = "text/javascript";
s.async = true;
s.src = "https://widget.intercom.io/widget/rxsoqwkv";
const x = d.getElementsByTagName("script")[0];
if (x && x.parentNode) {
x.parentNode.insertBefore(s, x);
console.log("intercom loading.");
}
}
})();
}

function checkAndLoadIntercom() {
if (document.readyState === "complete") {
// Wait 15 seconds after document ready and network idle
setTimeout(loadIntercom, 15000);
}
}

// Check for document ready and network idle
if (document.readyState === "complete") {
checkAndLoadIntercom();
} else {
window.addEventListener("load", checkAndLoadIntercom, false);
}

0 comments on commit b035965

Please sign in to comment.