Live Chat App slows page down

This is for anyone that requested I show our team’s workaround for helping page speed with the bloated Live Chat script.
First set the Hubspot chat setting to not immediately load live chat. This single setting alone prevents the “vistor.js” file from loading which appears to be the main contributing file to the slow down. Only allow the live chat to load immediately if you know the user to be in a chat session or if the live chat auto launch hash url is used. (#hs-chat-open)

//Prevent chat from loading immediately load only if chat is not in progress or #hs-chat-open is not in the url
if (sessionStorage.getItem("chatInProgress") !== "true" && window.location.hash !== "#hs-chat-open") {
 window.hsConversationsSettings = { loadImmediately: false };
}

Now that the chat is blocked. We have to artifically create a chat button that will launch live chat only when clicked by the user. The rest of this code will also set a session storage for knowing if the user’s conversation is open, which will allow live chat to load itself on page load. There is also observer mutation events to detect when the live chat window is opened, so you can hide the fake hubspot chat button.

if (window.HubSpotConversations) {
 hubspotChatReady();
 } else {
 window.hsConversationsOnReady = [hubspotChatReady];
 }
 function hubspotChatReady() {
 //Proceed to immediately load Live Chat while conversation is persisting
 window.HubSpotConversations.addEventListener("conversationStarted", function () {
 sessionStorage.setItem("chatInProgress", "true");
 });
 window.HubSpotConversations.addEventListener("conversationClosed", function () {
 sessionStorage.removeItem("chatInProgress");
 });
 //Fake Hubspot Live chat button to launch real live chat only when user clicks
 //Ignore if Live Chat is already loaded
 if (window.HubSpotConversations.widget.status().loaded === false) {
 var $liveChat = document.getElementById("activate-chat");
 $liveChat.style.display = "block";
 $liveChat.addEventListener("click", function (event) {
 event.preventDefault();
 $liveChat.classList.add("loading");
 //Chat window is open when the iframe is loaded in the dom (Hubspot doesn't give a callback for the chat window opening)
 var observer = new MutationObserver(function (mutationList, observer) {
 mutationList.forEach(function (m) {
 m.addedNodes.forEach(function (n) {
 if (n.id === "hubspot-messages-iframe-container") {
 n.querySelector('iframe').addEventListener('load', function () {
 $liveChat.classList.remove("loading");
 window.HubSpotConversations.widget.open();
 observer.disconnect();
 });
 }
 });
 });
 });
 observer.observe(document.body, { childList: true, attributes: false, subtree: false });
 window.HubSpotConversations.widget.load({ widgetOpen: true });
 })
 }
 }

I hope this helps some people. This is not a perfect workaround. Some of the shortfalls would be if you use Hubspot’s “hide launcher” setting. Where maybe you want to turn Live Chat on or off from the portal. This workaround bakes in a fake button that doesn’t know if Hubspot chat should or shouldn’t be showing. In our situation we leave Live Chat open 24/7 and just use away messages.
We are still waiting for Hubspot to take some concern with page speed. But until then we’ll do what we can to work around it.