Utility module for loading external scripts with deduplication and cancellation support. Provides a centralized way to load external JavaScript libraries while avoiding duplicate requests.
- Version
- 1.0.0
- License
- MIT
- Source
- scriptLoader.js, line 1
Members
private, constant, inner callbackRegistry :Map<string, Set<{resolve: function(), reject: function()}>>
Module-local registry for managing callbacks per script URL. Cross-bundle deduplication is handled via DOM dataset attributes instead of global state.
- Type
- Map<string, Set<{resolve: function(), reject: function()}>>
- Source
- scriptLoader.js, line 19
Methods
private, inner attachScriptListeners(url, script, getResult)
Attaches load/error event listeners to a script element. Handles success/error callbacks for all pending requests.
| Name | Type | Description |
|---|---|---|
url |
string |
Script URL for callback registry lookup. |
script |
HTMLScriptElement |
Script element to attach listeners to. |
getResult |
function |
Callback that retrieves the global result object. |
- Source
- scriptLoader.js, line 65
private, inner attachToLoadingScript(url, script, getResult) → CancelablePromise
Attaches to an existing script that is currently loading (data-loaded="0").
| Name | Type | Description |
|---|---|---|
url |
string |
URL of the script being loaded. |
script |
HTMLScriptElement |
Script element to attach listeners to. |
getResult |
function |
Callback that retrieves the global result object. |
- Returns
-
Cancelable promise resolving on script load completion.
- Type CancelablePromise
- Source
- scriptLoader.js, line 95
private, inner createNewScript(url, options, getResult) → CancelablePromise
Creates and injects a new script element.
| Name | Type | Description |
|---|---|---|
url |
string |
URL of the script to create and load. |
options |
Object |
Configuration for script attributes (async, defer, etc.). |
getResult |
function |
Callback that retrieves the global result object. |
- Returns
-
Cancelable promise resolving on script load completion.
- Type CancelablePromise
- Source
- scriptLoader.js, line 121
static request(url, optionsopt) → CancelablePromise
Requests loading of an external script. Returns a cancelable promise. Multiple requests for the same URL will share the same underlying script element.
| Name | Type | Attributes | Description | ||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
url |
string |
The URL of the script to load. |
|||||||||||||||||||||||||||||||||||||||||
options |
Object | optional |
Optional configuration for script element.
|
- Returns
-
A cancelable promise that resolves when the script loads.
- Type CancelablePromise
- Source
- scriptLoader.js, line 183
Examples
import scriptLoader from '/lib/util/scriptLoader.js';
const dashjs = await scriptLoader.request('https://cdn.dashjs.org/latest/dash.all.min.js', {
global: 'dashjs'
});
// dashjs is now window.dashjs
// With cancellation
const promise = scriptLoader.request(url);
// Later, if no longer needed:
promise.cancel().catch(() => {}); // Suppress AbortError
// With custom availability check
const Hls = await scriptLoader.request(url, {
global: 'Hls',
checkAvailable: () => window.Hls?.isSupported?.()
});
// With CORS and integrity
scriptLoader.request(url, {
crossOrigin: 'anonymous',
integrity: 'sha384-...'
});