Skip to content

Commit 873d091

Browse files
committed
adding readme
1 parent d275e1a commit 873d091

1 file changed

Lines changed: 159 additions & 1 deletion

File tree

README.md

Lines changed: 159 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,159 @@
1-
# ResourceLoader.js
1+
# ResourceLoader
2+
3+
ResourceLoader is a flexible JavaScript library that allows for dynamic loading of resources like JavaScript, CSS, images, JSON, and other file types. It supports features like error handling, retries, cache busting, and concurrency control, making it suitable for various applications.
4+
5+
## Features
6+
7+
- **Dynamic Resource Loading**: Load JS, CSS, images, JSON, fonts, and more dynamically.
8+
- **Concurrency Control**: Limit the number of concurrent loads to prevent overwhelming the browser.
9+
- **Retries and Error Handling**: Automatically retry failed resource loads, with customizable retry delays.
10+
- **Cache Busting**: Optionally append cache-busting query strings to prevent caching issues.
11+
- **Cross-Origin and Integrity Handling**: Support for crossorigin and integrity attributes for secure resource loading.
12+
- **Blob and File Loading**: Load and handle binary files like images, audio, and video as blobs.
13+
- **Callbacks for Success and Failure**: Handle success or failure for each resource with callbacks.
14+
15+
## Installation
16+
17+
You can directly download the `ResourceLoader.js` file or include it from your project.
18+
19+
### Direct Download
20+
21+
You can download the `ResourceLoader.js` file directly from this repository.
22+
23+
### Usage
24+
25+
Include the `ResourceLoader.js` file in your HTML or project:
26+
27+
```html
28+
<script src="path/to/ResourceLoader.js"></script>
29+
```
30+
31+
## Example Usage
32+
33+
### Basic JavaScript and CSS Loading
34+
35+
```javascript
36+
ResourceLoader.include(
37+
[
38+
'https://cdnjs.cloudflare.com/ajax/libs/jquery/3.6.0/jquery.min.js',
39+
'https://cdnjs.cloudflare.com/ajax/libs/normalize/8.0.1/normalize.min.css',
40+
],
41+
{
42+
logLevel: 'verbose',
43+
}
44+
)
45+
.then(() => {
46+
console.log('Resources loaded successfully.');
47+
})
48+
.catch((error) => {
49+
console.error('Error loading resources:', error);
50+
});
51+
```
52+
53+
### JSON Loading
54+
55+
```javascript
56+
ResourceLoader.include(['https://example.com/data.json'], {
57+
onSuccess: (data) => {
58+
console.log('JSON data loaded:', data);
59+
},
60+
onError: (error, url) => {
61+
console.error(`Error loading JSON from: ${url}`, error.message);
62+
},
63+
});
64+
```
65+
66+
### Image Loading
67+
68+
```javascript
69+
ResourceLoader.include(['https://example.com/image.jpg'], {
70+
onSuccess: (url) => {
71+
const img = new Image();
72+
img.src = url;
73+
document.body.appendChild(img);
74+
console.log('Image loaded successfully.');
75+
},
76+
onError: (error, url) => {
77+
console.error(`Error loading image from: ${url}`, error.message);
78+
},
79+
});
80+
```
81+
82+
### Audio Loading as Blob
83+
84+
```javascript
85+
ResourceLoader.include(['https://example.com/audio.mp3'], {
86+
onSuccess: (data) => {
87+
const blobUrl = URL.createObjectURL(data);
88+
const audioElement = document.createElement('audio');
89+
audioElement.controls = true;
90+
audioElement.src = blobUrl;
91+
document.body.appendChild(audioElement);
92+
},
93+
onError: (error, url) => {
94+
console.error(`Error loading audio from: ${url}`, error.message);
95+
},
96+
});
97+
```
98+
99+
### Cache Busting
100+
101+
```javascript
102+
ResourceLoader.include(['https://example.com/script.js'], {
103+
cacheBusting: true,
104+
});
105+
```
106+
107+
## API
108+
109+
### `include(urls, options)`
110+
111+
- **`urls`**: An array of URLs or a single URL to be loaded.
112+
- **`options`**:
113+
- `logLevel`: (default `'warn'`) Log level. Can be `'silent'`, `'warn'`, or `'verbose'`.
114+
- `onSuccess`: Callback when the resource(s) load successfully.
115+
- `onError`: Callback when the resource fails to load.
116+
- `retries`: Number of times to retry loading on failure.
117+
- `retryDelay`: Delay between retry attempts.
118+
- `deferScriptsUntilReady`: If true, defers script loading until DOM is ready.
119+
- `maxConcurrency`: Limit concurrent resource loads.
120+
- `cacheBusting`: Append a cache-busting query parameter to the resource URL.
121+
- `crossorigin`: Set cross-origin for JS/CSS resources.
122+
- `attributes`: Additional attributes to set on the element (e.g., integrity).
123+
124+
### `unloadResource(url)`
125+
126+
Unloads a resource from the page.
127+
128+
```javascript
129+
ResourceLoader.unloadResource('https://example.com/script.js');
130+
```
131+
132+
### `cancelResource(url)`
133+
134+
Cancels the loading of a resource.
135+
136+
```javascript
137+
ResourceLoader.cancelResource('https://example.com/script.js');
138+
```
139+
140+
### `cancelAll()`
141+
142+
Cancels all pending resource loads.
143+
144+
```javascript
145+
ResourceLoader.cancelAll();
146+
```
147+
148+
### `getResourceState(url)`
149+
150+
Gets the current state of a resource (`"loading"`, `"loaded"`, `"unloaded"`).
151+
152+
```javascript
153+
const state = ResourceLoader.getResourceState('https://example.com/script.js');
154+
console.log(state); // "loaded"
155+
```
156+
157+
## License
158+
159+
This project is licensed under the MIT License.

0 commit comments

Comments
 (0)