Skip to content

Commit 65d99a0

Browse files
committed
allow multiple + different tag types
1 parent 2c6f1fd commit 65d99a0

File tree

2 files changed

+56
-9
lines changed

2 files changed

+56
-9
lines changed

README.MD

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
[![js-standard-style](https://img.shields.io/badge/code%20style-standard-brightgreen.svg)](http://standardjs.com) [![CircleCI](https://circleci.com/gh/beyonk-adventures/async-script-loader.svg?style=shield)](https://circleci.com/gh/beyonk-adventures/async-script-loader)
88

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

1111
* Using a test to ensure that the code is only loaded once
1212
* Running a callback once the script is loaded
@@ -19,6 +19,12 @@ Having integrated a multitude of third-party SDKs from large, well known provide
1919

2020
### Usage
2121

22+
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.
23+
24+
Urls can be scripts or stylesheets.
25+
26+
### Script Tags
27+
2228
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.
2329

2430
```js
@@ -35,7 +41,31 @@ You can use the module like so, for a library loaded from example.com, which, wh
3541
window.PROVIDER.someFunction()
3642
}
3743

38-
loader(url, test, callback, { async: true, defer: true })
44+
loader([
45+
{ type: 'script', url }
46+
], test, callback)
47+
</script>
48+
```
49+
50+
#### Style Tags
51+
52+
You can include any number of tags, including style tags.
53+
54+
When the last one has loaded, the callback will be called.
55+
56+
```js
57+
<script>
58+
import loader from '@beyonk/async-script-loader'
59+
60+
loader([
61+
{ type: 'script', url: '//example.com/sdk/1.0.0/lib.js' },
62+
{ type: 'script', url: '//example.com/sdk/1.0.0/lib2.js' },
63+
{ type: 'style', url: '//example.com/sdk/1.0.0/style.css' }
64+
], () => {
65+
return !!window.PROVIDER
66+
}, () => {
67+
window.PROVIDER.someFunction()
68+
})
3969
</script>
4070
```
4171

index.js

Lines changed: 24 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,28 @@
1-
export default function (url, test, callback, options = { async: true, defer: true }) {
1+
export default function (urls, test, callback) {
2+
let remaining = urls.length
3+
4+
function maybeCallback () {
5+
remaining = --remaining
6+
if (remaining < 1) {
7+
callback()
8+
}
9+
}
10+
211
if (!test()) {
3-
const tag = document.createElement('script')
4-
tag.src = url
5-
tag.async = options.async
6-
tag.defer = options.defer
7-
tag.onload = callback
8-
document.body.appendChild(tag)
12+
urls.forEach(({ type, url, options = { async: true, defer: true }}) => {
13+
const isScript = type === 'script'
14+
const tag = document.createElement(isScript ? 'script': 'link')
15+
if (isScript) {
16+
tag.src = url
17+
tag.async = options.async
18+
tag.defer = options.defer
19+
} else {
20+
tag.rel = 'stylesheet'
21+
tag.href = url
22+
}
23+
tag.onload = maybeCallback
24+
document.body.appendChild(tag)
25+
})
926
} else {
1027
callback()
1128
}

0 commit comments

Comments
 (0)