Skip to content

Commit 51e8104

Browse files
committed
docs(core): CHECKOUT-4400 Update documentation to include additional methods
1 parent 47af3a4 commit 51e8104

File tree

1 file changed

+92
-1
lines changed

1 file changed

+92
-1
lines changed

README.md

Lines changed: 92 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,17 +14,108 @@ npm install --save @bigcommerce/script-loader
1414

1515
## Usage
1616

17+
### For scripts
18+
19+
To load a single script:
20+
1721
```js
1822
import { createScriptLoader } from '@bigcommerce/script-loader';
1923

2024
const loader = createScriptLoader();
2125

22-
loader.loadScript('https://code.jquery.com/jquery-3.2.1.min.js')
26+
loader.loadScript('https://cdn.foo.bar/main.js')
2327
.then(() => {
2428
console.log('Loaded!');
2529
});
2630
```
2731

32+
To load multiple scripts:
33+
34+
```js
35+
loader.loadScripts([
36+
'https://cdn.foo.bar/vendor.js',
37+
'https://cdn.foo.bar/main.js',
38+
]);
39+
```
40+
41+
This is different to calling `loadScript` multiple times because it can ensure that the scripts are downloaded in parallel but executed in the same sequence as the provided list of URLs.
42+
43+
To preload or prefetch a script:
44+
45+
```js
46+
loader.preloadScript('https://cdn.foo.bar/chunk.js');
47+
loader.preloadScript('https://cdn.foo.bar/another-chunk.js', { prefetch: true });
48+
```
49+
50+
A prefetched script is a low priority resource, therefore it will be downloaded in the background when the browser is idle. On the other hand, a script without `prefetch` option will be marked as a high priority resource and downloaded immediately.
51+
52+
Please note that the preloaded or prefetched script won't be executed. It will just be downloaded to the browser cache. To attach it to the document and execute it, you will need to call `loadScript` with the same URL.
53+
54+
To preload or prefetch multiple scripts:
55+
56+
```js
57+
loader.preloadScripts([
58+
'https://cdn.foo.bar/chunk.js',
59+
'https://cdn.foo.bar/another-chunk.js',
60+
]);
61+
62+
loader.preloadScripts([
63+
'https://cdn.foo.bar/chunk.js',
64+
'https://cdn.foo.bar/another-chunk.js',
65+
], { prefetch: true });
66+
```
67+
68+
### For stylesheets
69+
70+
To load a single stylesheet:
71+
72+
```js
73+
import { createStylesheetLoader } from '@bigcommerce/script-loader';
74+
75+
const loader = createStylesheetLoader();
76+
77+
loader.loadStylesheet('https://cdn.foo.bar/main.css')
78+
.then(() => {
79+
console.log('Loaded!');
80+
});
81+
```
82+
83+
To load multiple stylesheets:
84+
85+
```js
86+
loader.loadStylesheet([
87+
'https://cdn.foo.bar/vendor.css',
88+
'https://cdn.foo.bar/main.css',
89+
]);
90+
```
91+
92+
To prepend, instead of append, a stylesheet to the head of a document:
93+
94+
```js
95+
loader.loadStylesheet('https://cdn.foo.bar/main.css', { prepend: true });
96+
```
97+
98+
To preload or prefetch a stylesheet:
99+
100+
```js
101+
loader.preloadStylesheet('https://cdn.foo.bar/chunk.css');
102+
loader.preloadStylesheet('https://cdn.foo.bar/another-chunk.css', { prefetch: true });
103+
```
104+
105+
Similar to a preloaded script, a preloaded or prefetched stylesheet won't take an effect until it is attached to the document. To do it, you will need to call `loadStylesheet` with the same URL.
106+
107+
```js
108+
loader.preloadStylesheets([
109+
'https://cdn.foo.bar/chunk.css',
110+
'https://cdn.foo.bar/another-chunk.css',
111+
]);
112+
113+
loader.preloadStylesheets([
114+
'https://cdn.foo.bar/chunk.css',
115+
'https://cdn.foo.bar/another-chunk.css',
116+
], { prefetch: true });
117+
```
118+
28119
## Contribution
29120

30121
To release:

0 commit comments

Comments
 (0)