I want the hubspot forms to use async method or can we perform lazyload on hubspot javascript file , so that we can reduce the load time
help me ! how we can do that
files I am talking here are like forms/v2.js, leadflow.js.
these file always load when we open a website not depending on whether user submit a hubspot form or not.
I’m not sure this is possible sadly. Normally you would use the async or defer attribute on script tag, however, because these scripts are part of the standard_header_includes so you obviously can’t do that.
You could add defer to the script tags as their added to the DOM using MutationObserver, but I’m not sure that’s a good idea/may slow your page down.
const observer = new MutationObserver(mutations => {
mutations.forEach(({ addedNodes }) => {
addedNodes.forEach(node => {
// For each added script tag
if(node.nodeType === 1 && node.tagName === 'SCRIPT') {
// Adds defer attribute to the script tag in Safari, Chrome, Edge & IE
node.setAttribute('defer', 'defer');
}
})
})
})
// Starts the monitoring
observer.observe(document.documentElement, {
childList: true,
subtree: true
})