Extension, Plugin

Archived
Forum
(read-only)

Minimee

ExpressionEngine 2

Back to this add-on's main page
View Other Add-ons From John D. Wells

     

Asynchronous Loading

Support Request

Todd
Todd

In my quest to speed up my websites, I’ve been actively making as much of Javascript asynchronous as I can. I’m not an expert on how to do this. I’m mainly been replacing tags with the new asynchronous tags that a lot of services I was using provide. So I’m a little unsure of how I would do this (or if it’s even possible to do) with the combine Javascript file that Minimee generates. So…

a) is this possible?

b) any pointers on how to do it?

Thanks for making such a great add-on!

-Todd

John D Wells
# 1
Developer
John D Wells

Hi Todd,

Good question.  First, Yes it is possible.  You may be aware that the way Minimee generates the cache script & link tags is based off of the format of the first such tag it encounters on the tag pair run.  So in the following example:

{exp:minimee:js}
script src="plugins.js" async="async" >< /script >
script src="script.js" >< /script >
{/exp:minimee:js} 

The rendered cache file would be based off the first tag, so:

script src="as876dfa98s7d6fa9s8df5a9s8dfa9asdf.js" async="async" >< /script 

So, that’s how you’d use it; question is should you, and if so when (and when not).

Personally, I tend to not run Minimee on any external scripts (Facebook, Google Analytics, Twitter etc); I take the view that these scripts are most likely already cached in people’s browsers; plus it ensures I always get their most up to date code (since Minimee cannot detect changes to external files).

And if you’re like me, those are most likely the only viable candidates for asynchronous loading; all other scripts I might need are usually reliant upon each other (e.g. jquery + cycle + bootstrap + plugins + scripts).

Plus, the benefit of using Minimee to both compress and combine files means that you’re making gains by reducing your HTTP requests.  So even if you’ve decoupled an HTTP request or two from the rest of the page load, you could argue you would have benefitted more if you’d had just 1 request total.

The exception might be for large scripts - the FB script is obnoxious at 55kb (jquery is 33kb!), so that is never something I want to bundle into the rest of my scripts.

If you’re using a lot of 3rd party services, you could consider combining all of these into a single Minimee’d async cache file, but then you’d need to periodically refresh the cached file to keep you scripts current (if you wanted to).  That at least would consolidate your async HTTP requests down to 1.  I’d consider that a win.

Well, I’m not sure if any of the above was what you were after - and none of the above is backed up by facts (get on the Cain Train), so follow at your own risk.

My final thought is that you’re probably doing pretty well if you’re at the point of considering async - but just in case, here’s a list of what I’d say are more important to do first:

- compress images (ImageOptim if you’re on a Mac)
- sprites
- scripts at bottom
- customise 3rd party frameworks/libraries to only include what’s used (e.g. modernizr, & twitter’s bootstrap)
- reduce number of web fonts
- limit your reliance on JS (controversial!)
- for scripts that are only needed on a single page, exclude from site-wide combination and embed inline (see {exp:minimee:embed})

And then of course there’s EE optimisation - but now I’m really getting off topic…

If you do have a go at harnessing async, I’d be appreciated to hear your findings!

Cheers,
John

 

Todd
# 2
Todd

This is an incredible response… really detailed and thorough and lots to think about. I might have more questions but I just wanted to clear up that I wasn’t planning on using Minimee on Facebook, etc. type stuff. Those things I’ve already replaced with the asynchronous version of those tags that area available with the services.

I was thinking of the

a) JQuery
b) JQuery Plug-Ins
c) The javascript/jquery that I’ve written that’s unique to my site.

That’s accounts for the biggest amounts that are not running asynchronous…

John D Wells
# 3
Developer
John D Wells

Hi Todd,

Glad you found that ramble worth a read…

FWIW I did try comparing YSlow’s grading between async and not, and at least this time, my grade didn’t change.  Interesting to see in fact that async isn’t even part of the YSlow evaluation…

... I suppose you could async Minimee’s cached JS file - provided you’ve included all of what you listed (including jQuery), so you do not have a script dependency issue.  And of course be sure to have it in the footer so that your DOM is complete.  I’m just not positive you’ll really gain much, since by then you’ll already be ahead of the curve….

Cheers,
John

Tamas Benke
# 4
Tamas Benke

Unfortunately async=“async” does no appear to show up in the cached tag :(

{exp:minimee:js}
< script type=“text/javascript” src=”/js/jquery.min.js” async=“async” >< / script >
< script type=“text/javascript” src=”/js/jquery.fancybox.js” >< / script >
< script type=“text/javascript” src=”/js/lime.off-nav.js” >< / script >
< script type=“text/javascript” src=”/js/jquery.placeholder.js” >< / script >
{/exp:minimee:js}

Rendered as:
< script type=“text/javascript” src=”//www.purplefog.com/cache/acbbe3da23d3d73299b2b228e4fd6b7ddbd92ba4.1424075787.js”>< / script >

Am I missing something?

Tamas Benke
# 5
Tamas Benke

Sorry it works, that was a caching issue on my end.