All Projects → beyonk-adventures → async-script-loader

beyonk-adventures / async-script-loader

Licence: other
Asynchronous script loading for SPAs

Programming Languages

javascript
184084 projects - #8 most used programming language

Projects that are alternatives of or similar to async-script-loader

spa-bus
🔥Tools for multilevel components to pass values in any SPA
Stars: ✭ 15 (+0%)
Mutual labels:  spa, event
AutoChange12306CDN
一个自动切换12306 CDN的代理,只需设置浏览器的代理为此软件监听端口,每次查询请求都会更换CDN,达到快速刷票的目的。
Stars: ✭ 35 (+133.33%)
Mutual labels:  cdn
webviewhs
🌐 A Haskell binding to the webview library created by Serge Zaitsev.
Stars: ✭ 109 (+626.67%)
Mutual labels:  spa
growth.dev
[EVENT] API & IPA
Stars: ✭ 46 (+206.67%)
Mutual labels:  event
mo360-ftk
MO360 Frontend Toolkit: A toolkit for single page applications (SPA) based on React and Typescript that allows to extract single features into microfrontends.
Stars: ✭ 54 (+260%)
Mutual labels:  spa
wechat-spa
🎉 微信端单页面应用(SPA)常见问题汇总及解决方案
Stars: ✭ 337 (+2146.67%)
Mutual labels:  spa
cm
Configuration management for all VOC systems
Stars: ✭ 17 (+13.33%)
Mutual labels:  cdn
azure-cdn-ips
List of Azure CDN IP Addresses
Stars: ✭ 14 (-6.67%)
Mutual labels:  cdn
ChRIS ui
UI for ChRIS
Stars: ✭ 20 (+33.33%)
Mutual labels:  spa
proffy
Plataforma de estudos online para conectar alunos e professores de forma rápida e fácil. @Rocketseat
Stars: ✭ 21 (+40%)
Mutual labels:  event
xRoute
一个小型的前端路由库✈️
Stars: ✭ 36 (+140%)
Mutual labels:  spa
event
The implementation of the pattern observer
Stars: ✭ 45 (+200%)
Mutual labels:  event
nocdn
Self-hosted alternative to CDNs.
Stars: ✭ 37 (+146.67%)
Mutual labels:  cdn
cdn-up-and-running
CDN Up and Running - an introduction about how modern CDNs works
Stars: ✭ 131 (+773.33%)
Mutual labels:  cdn
edliz
This 7th essential medicines list and standard treatment guidelines for the most common health conditions in Zimbabwe has been endorsed by the National Medicine & Therapeutics Policy Advisory Committee [NMTPAC]. It is the product of many years of combined efforts by hundreds of health workers at all levels of the health care system in Zimbabwe. …
Stars: ✭ 25 (+66.67%)
Mutual labels:  spa
super-simple-dockerized-spa
Illustrating how to wrap a single page application in docker.
Stars: ✭ 25 (+66.67%)
Mutual labels:  spa
pacman.store
Pacman Mirror via IPFS for ArchLinux, Endeavouros and Manjaro
Stars: ✭ 65 (+333.33%)
Mutual labels:  cdn
wordpress
Free PWA & SPA for Wordpress & Woocommerce
Stars: ✭ 103 (+586.67%)
Mutual labels:  spa
api-server
📡 API server for api.cdnjs.com - The #1 free and open source CDN built to make life easier for developers.
Stars: ✭ 48 (+220%)
Mutual labels:  cdn
cdn
Simple and fast property-based configuration library for JVM apps, similar to JSON5 standard, also with JSON & YAML-like targets 🧾
Stars: ✭ 33 (+120%)
Mutual labels:  cdn




Async Script Loader

js-standard-style CircleCI

Allows asynchronous loading of scripts and styles in a Single Page Application (or anything else, in fact):

  • Using a test to ensure that the code is only loaded once
  • Running a callback once the script is loaded
  • Running a callback if the script is already loaded
  • Not blocking the main thread

Reasoning

Having integrated a multitude of third-party SDKs from large, well known providers, I've come to the conclusion that not having a standard interface turns the whole thing into a minefield of callbacks, timers, random library-specific loader modules, and global objects on the window, resulting in XSS risks and all sort of other undesirable behaviour. This module aims to provide a standard way of loading third-party dependencies.

Usage

You pass a list of urls to the loader, along with a method for checking that your page is ready, and a callback to call when it is.

Urls can be scripts or stylesheets.

Script Tags

You can use the module like so, for a library loaded from example.com, which, when loaded, adds an attribute called PROVIDER to the global window object.

<script>
  import loader from '@beyonk/async-script-loader'

  const url = '//example.com/sdk/1.0.0/lib.js'

  function test () {
    return !!window.PROVIDER
  }

  function callback () {
    window.PROVIDER.someFunction()
  }

  loader([
    { type: 'script', url }
  ], test, callback)
</script>

You can pass options for script tags.

<script>
  loader([
    { type: 'script', url, options: { async: true, defer: true } } // these are the default options
  ], test, callback)
</script>

Style Tags

You can include any number of tags, including style tags.

When the last one has loaded, the callback will be called.

<script>
  import loader from '@beyonk/async-script-loader'

  loader([
    { type: 'script', url: '//example.com/sdk/1.0.0/lib.js' },
    { type: 'script', url: '//example.com/sdk/1.0.0/lib2.js' },
    { type: 'style', url: '//example.com/sdk/1.0.0/style.css' }
  ], () => {
    return !!window.PROVIDER
  }, () => {
    window.PROVIDER.someFunction()
  })
</script>

No more tears!

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].