Post by txvaporguy » Fri Jun 07, 2019 9:17 am

I've run into a predicament where I've optimized things the best that I can for scripts that are loaded into header files via vqmod but it seems that scripts are still being injected using OC's built-in library functions (addScript function found in the ~/system/library/document.php file).

Code: Select all

public function addScript($script) {
	$this->scripts[md5($script)] = $script;
}
It would be highly useful to have the ability to load scripts with attribute tags such as async or defer in order to speed up page load times. Currently, we have something like ~10 scripts being added using the addScript function in module controller files and it'd be great to be able to add a defer tag to them to prevent them from blocking page render.

Has anyone run into this situation and come up with a solution or modification to the document.php file?

Any help is greatly appreciated!

Newbie

Posts

Joined
Wed Mar 16, 2016 9:52 am

Post by IP_CAM » Fri Jun 07, 2019 10:29 am

Certain .js Code as well as most-all .css Code could be combined into
a single File, to be called from the Theme Header or Footer File, then, every such
Combination-FILE can be addressed, to eventually function in ASYNC Mode. But it
sounds easier that it is in real, to hopefully find the ideal Combination, and make
everything work again as it worked before. :D
And every single new Add-On needs then to be checked for such link-lines, to avoid
any collision, due to calling the same JS-Code twice on a single page.

Code: Select all

</footer></div>
<script type="text/javascript" async src="catalog/view/theme/default/js/compressed.js"></script>
And if done well, GTMetrix might honor a Cat-Page Load Test accordingly:
https://gtmetrix.com/compare/WHq8UyVV/utkqMD09
Good Luck! ;)
Ernie
---
Image

My Github OC Site: https://github.com/IP-CAM
5'200 + FREE OC Extensions, on the World's largest private Github OC Repository Archive Site.


User avatar
Legendary Member

Posts

Joined
Tue Mar 04, 2014 1:37 am
Location - Switzerland

Post by thekrotek » Fri Jun 07, 2019 3:58 pm

Just insert yous JS scripts directly in the header. No need to use a library for that.

Professional OpenCart extensions, support and custom work.
Contact me via email or Skype by support@thekrotek.com


User avatar
Expert Member

Posts

Joined
Sun Jul 03, 2016 12:24 am


Post by txvaporguy » Fri Jun 07, 2019 7:56 pm

Perhaps my original post was unclear. I am familiar with how to combine JS as well as how to place the JS into the header/footer of the site. The issue is with individual modules and the way that they handle adding scripts to the header using the addScript function.

Rather than waste a TON of time trying to modify all individual module files to consolidate JS into a single file, it would be much easier (and updatable) to write a VQMod that modifies the controller file for individual modules that use addScript functions for inserting their JS into the header.

Case example:

Code: Select all

// JavaScript
    if ($popup_box['use_default_javascript']) {
      // Include core JavaScript files
      $this->document->addScript('catalog/view/javascript/popup_box/magnific_popup/0.9.9/dist/jquery.magnific-popup.min.js');
      
      if ($this->development_mode) {
        $this->document->addScript('catalog/view/javascript/popup_box/popup_box.js');
      } else {
        $this->document->addScript('catalog/view/javascript/popup_box/popup_box.min.js');
      }      
    }
In the above snippet from a pop-up box module's controller file, you can see that it loads scripts dynamically rather than just a single insert. I was hoping to find a way to modify the addScript to add a defer or async tag as such:

Code: Select all

$this->document->addScript('catalog/view/javascript/popup_box/magnific_popup/0.9.9/dist/jquery.magnific-popup.min.js', defer);
So that once it's inserted into the header, the original resulting html:

Code: Select all

<script type="text/javascript" src="catalog/view/javascript/popup_box/magnific_popup/0.9.9/dist/jquery.magnific_popup.min.js"></script>
Is changed to:

Code: Select all

<script defer type="text/javascript" src="catalog/view/javascript/popup_box/magnific_popup/0.9.9/dist/jquery.magnific_popup.min.js"></script>
I hope what I'm asking about is a little clearer now.

Thanks!

Newbie

Posts

Joined
Wed Mar 16, 2016 9:52 am

Post by thekrotek » Fri Jun 07, 2019 8:37 pm

There're no strict guidelines and every extension can store its JS files anywhere it wants. So your code is not really of any use.

Professional OpenCart extensions, support and custom work.
Contact me via email or Skype by support@thekrotek.com


User avatar
Expert Member

Posts

Joined
Sun Jul 03, 2016 12:24 am


Post by txvaporguy » Fri Jun 07, 2019 9:10 pm

thekrotek wrote:
Fri Jun 07, 2019 8:37 pm
There're no strict guidelines and every extension can store its JS files anywhere it wants. So your code is not really of any use.
Thanks but I guess that you still don't understand, none of the above posted code is "my code". I am simply asking if there's some way to add a modification to addScript function of the /system/library/document.php file to include defer or attribute tags to scripts that are added using the addScript function.

I understand that you're trying to help by responding, but if you've read what I posted, you'd understand what I'm trying to accomplish.

Newbie

Posts

Joined
Wed Mar 16, 2016 9:52 am

Post by thekrotek » Fri Jun 07, 2019 10:22 pm

And again, I repeat, that you can add these changes directly into the header. To all the scripts, which were added via library functions or directly. No need to expand the library functions themselves.

Anyway, the task is custom, implement it anyway you like.

Professional OpenCart extensions, support and custom work.
Contact me via email or Skype by support@thekrotek.com


User avatar
Expert Member

Posts

Joined
Sun Jul 03, 2016 12:24 am


Post by txvaporguy » Fri Jun 07, 2019 11:48 pm

thekrotek wrote:
Fri Jun 07, 2019 10:22 pm
And again, I repeat, that you can add these changes directly into the header. To all the scripts, which were added via library functions or directly. No need to expand the library functions themselves.

Anyway, the task is custom, implement it anyway you like.
Thanks, I'll try to find a way to accomplish this in the header. I guess I just figured writing additional functionality into the library functions would allow me to modify the offending render-blocking scripts easier.

When you say that you can add these changes directly into the header, would you mind explaining how you would accomplish this?

Thank you!

Newbie

Posts

Joined
Wed Mar 16, 2016 9:52 am

Post by IP_CAM » Sat Jun 08, 2019 8:07 am

When you say that you can add these changes directly into the header,
would you mind explaining how you would accomplish this?
Read, what I posted above, to find out, what he meant ... ::)

But whatever you decide on, just better do NOT expect more than possibly
1 percent Pageload Performance Improvement at GTMetrix.com, because
of 'postponing' Script Execution by use of async or similar commands.
And in many cases, something won't work anymore, like upgrading Top
Header Card Content on a Product / Category Page. You will at best
waste a lot of time , and end up with nothing, really improving something
considerably.
Just to have you warned, other OC Brakes matter more, when it comes to
peak up OC to it's best.

Good Luck! ;)
Ernie

My Github OC Site: https://github.com/IP-CAM
5'200 + FREE OC Extensions, on the World's largest private Github OC Repository Archive Site.


User avatar
Legendary Member

Posts

Joined
Tue Mar 04, 2014 1:37 am
Location - Switzerland
Who is online

Users browsing this forum: No registered users and 119 guests