I’m using this code to add a chat box in my next.js app page. When I navigate through my app and come back to the chat page the chat box dosn’t appear until I refresh.
I doest appear the first time after that it requirs refresh to appear.
Hello @MQasim2
The reason why the chat box doesn’t appear until you refresh the page is because the HubSpot script is not being loaded on the initial page load. This is because the useEffect hook is only triggered when the component mounts, and not when the page is loaded.
you need to move the code that loads the HubSpot script outside of the useEffect hook. You can do this by adding the following code to the top of the component.
const script = document.createElement("script");
script.src="//js-na1.hs-scripts.com/xxxxxx.js"; // Replace XXXXXX with your HubSpot script ID
script.id = "hs-script-loader";
script.async = true;
document.body.appendChild(script);
This will load the HubSpot script on the initial page load, and the chat box should appear without having to refresh the page.
Here is the updated code for the HubSpotChat component:
Hi, I used the method shared by @himanshurauthan. My concern is that I want to load different chatbots on route change with Next 13. I have tried every possible means to no avail. Below is a sample script: