I'm building a package that uses the new Umbraco.UI library. The package covers Umbraco 8, 9 and 10, so not all versions will include the UUI Javascript.
Rather than load the script twice on newer V10 installs that include the library by default, what's the best way to detect in javascript if its already loaded?
Wondering if anyone has a "best practice" for this as yet?
Detecting if the new UUI JS library is included
Hi,
I'm building a package that uses the new Umbraco.UI library. The package covers Umbraco 8, 9 and 10, so not all versions will include the UUI Javascript.
Rather than load the script twice on newer V10 installs that include the library by default, what's the best way to detect in javascript if its already loaded?
Wondering if anyone has a "best practice" for this as yet?
Here's my solution thus far. I have a separate JavaScript file that the package manifest references with the following code:
if (typeof uui === 'undefined') { var script = document.createElement('script'); script.src = "https://cdn.jsdelivr.net/npm/@umbraco-ui/uui@latest/dist/uui.min.js"; document.head.appendChild(script); }
This runs on load and if the uui object doesn't exist, it loads the latest from the CDN.
It seems to work well, but it does have the possibility of another package doing the same check and loading the script multiple times.
is working on a reply...