The key to incorperating someone else's code is trying to understand WHY they do certain things. Just doing a copy-and-paste of someones code is seldom the most omtimized solution in another environment.
Looking a the code, the writer tries to achieve two things:
1. Control the button visibility depending on the javascripts load finished
2. Loading the overall WebPage faster
This visibility issue can easily be done by using normal BANano code. Calling both methods in BANano_Ready can achieve the same thing. There is no need to use an onLoad in the script tag.
As for the speed, using defer/async
can be faster than not using it, but it is also something you have to do VERY careful about, knowing ALL the javascript and html used in your project. Using it wrongly can easily result in a broken project. In such a small project as this, and without using any external frameworks, it is something you can control.
I'm not going to explain what async/defer does (google it) and as I will show further this is not even needed in BANano so this could only cause confusion.
A speed analysis:
Without async/defer/onLoad, setting the JavaScript after the two <script src="http..."> tags and calling both the gapiLoaded and gisLoaded methods at the very end. So, doing it the 'normal way'.
With async/defer (as in the example):
You can notice the last two numbers, especially the
DOMContentLoaded is smaller (25ms vs 110ms) in the one without async/defer,
Load is almost the same (349ms vs 317ms).
Same project, in BANano with the build-in Dynamic JavaScript Loading (is automatic, nothing you have to do for this). Because of the way BANano constructs the loading of resources, the app code is always loaded AFTER the two scripts . This avoids any possible conflict a wrongly constructed async/defer could cause.
So, BANano uses an even smarter way to load resources faster: Dynamic JavaScript Loading. Even dispite using an extra UI framework like BANanoSkeleton,
Load is much faster (111ms vs 317ms) and
DOMContentLoaded (26ms vs 25ms) is on par with using async/defer.
Of course because BANanoSkeleton is highly optimized (each component is carefully manually optimized to be very lightweight) and written using as much Vanilla Javascript as possible. As BANano can strip out dead (unused) code (up to method level!), the size of resources that have to be interpreted by the browser is kept to an absolute minimum.
Our BANano PWAs using BANanoSkeleton can easily compete with their native counterparts and have no need for such contructions.
Alwaysbusy