APIs & Integrations

ashadabbas15
Member

async javascript use in hubspot form

SOLVE

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.

2 Accepted solutions
piersg
Solution
Key Advisor

async javascript use in hubspot form

SOLVE

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
})

 

View solution in original post

stefen
Solution
Key Advisor | Partner
Key Advisor | Partner

async javascript use in hubspot form

SOLVE

@ashadabbas15 I would post this to the ideas board, it's something HubSpot would have to implement on their side.

Stefen Phelps, Community Champion, Kelp Web Developer

View solution in original post

7 Replies 7
SLovejoy
Participant

async javascript use in hubspot form

SOLVE

Are there any updates to this yet.

0 Upvotes
Jere
Contributor

async javascript use in hubspot form

SOLVE

Still in need of this! Any update?

0 Upvotes
SLovejoy
Participant

async javascript use in hubspot form

SOLVE

Any updates on this from anyone at Hubspot?

 

There still appears to be no way around the large .js scripts that are damaging site pagespeed performance.

stefen
Solution
Key Advisor | Partner
Key Advisor | Partner

async javascript use in hubspot form

SOLVE

@ashadabbas15 I would post this to the ideas board, it's something HubSpot would have to implement on their side.

Stefen Phelps, Community Champion, Kelp Web Developer
piersg
Solution
Key Advisor

async javascript use in hubspot form

SOLVE

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
})

 

Sjardo
Top Contributor | Elite Partner
Top Contributor | Elite Partner

async javascript use in hubspot form

SOLVE

Hi!

 

Fun thing is, if you do NOT use the standard header includes tag in your template, it's still loaded. 😅

Best, 

0 Upvotes
natsumimori
Community Manager
Community Manager

async javascript use in hubspot form

SOLVE

I will tag in subject matter experts here: @piersg and @stefen, are you familiar with this topic?

@ashadabbas15  Meanwhile, I wanted to share this resource: 3 Simple Steps to Create Lazy Loading Images In HubSpot 

0 Upvotes