Hi everyone,
I’ve been analyzing the performance of my website hosted on HubSpot CMS, and I noticed a significant number of extra scripts being loaded on my pages by HubSpot. These scripts are impacting the page speed, especially on mobile devices, as flagged by tools like Google PageSpeed Insights.
Upon inspecting the network requests, I found the following scripts being loaded (screenshot attached for reference):
- [ bundle.production.js, bundles/project.js, bundles/visitor.js, bundles/app.js ]
While I understand that some of these scripts might be essential for HubSpot functionalities, I’m unsure about the purpose of each one. Specifically:
- What are these scripts used for?
- Are all of them necessary for every page, or can some be conditionally loaded or removed entirely?
- How can I control which scripts are included in the page?
I’m particularly concerned because these scripts:
- Add extra network requests, increasing load time.
- Sometimes block the main thread, affecting Core Web Vitals metrics like Largest Contentful Paint (LCP) and Time to Interactive (TTI).
Questions:
- Is there a way to identify which scripts are critical and which are not?
- Can unnecessary scripts be disabled for specific pages or site-wide?
- What best practices should I follow to optimize my site performance while keeping essential HubSpot functionalities intact?
Any guidance on managing these extra scripts or optimizing my page speed in HubSpot would be greatly appreciated.
Attachments:
Thank you!
Best regards,
Abhishek Kumar
Hi @AKumar2718 and welcome, we are delighted to have you here!
Thanks for asking the Community!
For information, here are some resources that might be of interest to you:
- Track visitors in HubSpot
- Install the HubSpot tracking code
I’d like to share the solution from @sandeep4467 on this post “Google PageSpeed score is being negatively affected by HubSpot network requests” that might help you!
Also, I’d love to put you in touch with some of our Top Experts: Hi @zach_threadint and @Teun do you have other suggestions to help @AKumar2718, please?
Thanks so much and have a lovely day!
Best,
Bérangère
Hi @AKumar2718,
The four scripts listed (bundle.production.js, bundles/project.js, bundles/visitor.js, bundles/app.js) are HubSpot’s default scripts, which are automatically added.
As far as I know, they’re loaded asynchronously, which doesn’t affect the page speed. Would you mind sharing your pagespeed results URL here?
Hey Everyone,
I wanted to share the best solution I’ve found for resolving this issue. The root cause is the HubSpot main portal script, which loads multiple scripts initially, causing performance concerns. My approach involves removing the portal script from the initial load and instead deferring its loading. Here’s the code I’ve used—it might help you if you’re facing a similar problem:
Updated base.html:
{% set custom_footer_includes = standard_footer_includes|split(‘\n’) %}
{% for line in custom_footer_includes %}
{% unless line is string_containing ‘{portal-id}.js’ %}
{{line}}
{% endunless %}
{% endfor %}
JavaScript Code to Load the Portal Script Later:
document.addEventListener(“DOMContentLoaded”, function () {
let e = document.querySelector(‘script[src=“/_hcms/forms/v2.js”]’);
if (e) {
e.setAttribute(“defer”, “defer”);
} else {
console.error(“Script not found.”);
}
});
window.addEventListener(“load”, () => {
setTimeout(() => {
let e = document.createElement(“script”);
e.src=“/hs/scriptloader/{portal-id}.js”;
e.async = true;
document.body.appendChild(e);
}, 11000); // Load after 11 seconds
});
Request to HubSpot Team
If anyone from HubSpot is reading this, could you please confirm whether deferring the portal script loading as described above will impact HubSpot tracking or analytics functionality? Your input would be incredibly helpful to ensure this solution does not cause unintended consequences.
I hope this solution helps others facing similar issues. Looking forward to your feedback!
Best regards,
Abhishek kumar
Thanks for sharing this resource, @IMakkar! Could you please provide additional, specific context as to how this integration would help with @AKumar2718’s questions above?
Best,
Kennedy