https://js.hsforms.net/forms/embed/v2.js (js.hsforms.net)
-
page speed insights is complaining about this javascript slowing down our site https://js.hsforms.net/forms/embed/v2.js (js.hsforms.net)

The page I need help with: [log in to see the link]
-
Hi @cp1132 Thanks for your message.
This script is responsible for loading HubSpot forms at your page. We already defer this particular script at our plugin.
Are you using the latest version of the plugin? Also I would make sure if this script is not being manually added into any page.
Let me know if this helps,
Best regardsClosing this due to inactivity, feel free to re-open if needed.
Best regards,
Version 10.1.6
is there a way to lazy load the hubspot script?
Similiar to what is done at https://plugins.svn.ww.wp.xz.cn/leadin/trunk/public/class-pagehooks.php
you can have your own plugin that adds async to the forms script, with the following code:
add_filter( 'script_loader_tag', array( $this, 'add_async_to_forms_script' ), 10, 2 ); public function add_async_to_forms_script( $tag, $handle ) { if ( 'leadin-forms-v2' === $handle ) { $tag = str_replace( 'src', 'async src', $tag ); } return $tag; }please note that this can affect forms that are loaded inside modals.
Can I add that to the functions.php?
It would work. but I would suggest having a folder created at plugins, eg. leadin-forms-async/leadin-forms-async.php
does it have to be a php file? I can only add an html file with wp file manager
https://ubigro.com/wp-content/uploads/2023/03/Screen-Shot-2023-03-29-at-3.33.45-PM.png
How do I know if it is working?
https://pagespeed.web.dev/analysis/https-ubigro-com/v5e0cimdl8?form_factor=desktop
-
This reply was modified 3 years, 2 months ago by
cp1132.
A html will not work on this case. You can verify by checking if the script has the async property when inspecting the source code of the page
best regardswhere do i look for that here? view-source:https://ubigro.com/
syntax error, unexpected ‘public’ (T_PUBLIC), expecting end of file
I also tried this code
<script> // override the hbspt functionality while hubspot is loading var hbspt = { //push form to hubcache queue forms:{create:function(c){hubspot.forms.push(c)}}, }; // cache hubspot forms here var hubcache = { forms:[], letsgo:function(){ for (var i in hubspot.forms){ //hubspot is now loaded hbspt.forms.create(hubcache.forms[i]); } } } </script>
and added defer to<script charset="utf-8" type="text/javascript" src="//js.hsforms.net/forms/v2.js" defer></script>but nothing is helping with page speed
https://pagespeed.web.dev/analysis/https-ubigro-com/aj5mru7dy0?form_factor=desktop
Our script already has the defer property and the logic to render forms after the script has been loaded – you are looking to add the async property instead
please note the code I sent was an example, you may need to adapt to work outside of a PHP class – which is the error you got
best regards
-
This reply was modified 3 years, 2 months ago by
harthur90. Reason: add additional information
The file is now a php file https://ubigro.com/wp-content/uploads/2023/04/Screen-Shot-2023-04-03-at-2.07.12-PM.png
Here is an example of my current form code on a page do I need to change anything?<script> // override the hbspt functionality while hubspot is loading var hbspt = { //push form to hubcache queue forms:{create:function(c){hubspot.forms.push(c)}}, }; // cache hubspot forms here var hubcache = { forms:[], letsgo:function(){ for (var i in hubspot.forms){ //hubspot is now loaded hbspt.forms.create(hubcache.forms[i]); } } } </script> <script charset="utf-8" type="text/javascript" src="//js.hsforms.net/forms/v2.js" defer></script> <script> hbspt.forms.create({ region: "na1", portalId: "8419634", formId: "d0de9b78-f8c6-4b96-b2fd-c2e78e483a5f" }); </script>You should change your php code to the following:
function add_async_to_forms_script( $tag, $handle ) { if ( 'leadin-forms-v2' === $handle ) { $tag = str_replace( 'src', 'async src', $tag ); } return $tag; }add_filter('script_loader_tag','add_async_to_forms_script')You can simplify you page code to the following:
<script> window.hsFormsOnReady = window.hsFormsOnReady || []; window.hsFormsOnReady.push(()=>{ hbspt.forms.create({ region: "na1", portalId: "8419634", formId: "d0de9b78-f8c6-4b96-b2fd-c2e78e483a5f", target: "#your-div-id", }) }); </script> -
This reply was modified 3 years, 2 months ago by
The topic ‘https://js.hsforms.net/forms/embed/v2.js (js.hsforms.net)’ is closed to new replies.