Skip to content

Module: lib/util/scriptLoader

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
Author
Frank Kudermann - alphanull
License
MIT
Source
scriptLoader.js, line 1

Members

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.

Parameters:
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").

Parameters:
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.

Parameters:
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.

Parameters:
Name Type Attributes Description
url string

The URL of the script to load.

options Object optional

Optional configuration for script element.

Parameters:
Name Type Attributes Default Description
global string optional

Global variable name (e.g. 'dashjs'). If set, promise resolves with window[global].

checkAvailable function optional

Custom availability checker. Defaults to () => window[global] != null.

async boolean optional true

Whether to load the script asynchronously.

defer boolean optional false

Whether to defer script execution.

crossOrigin string optional

CORS setting ('anonymous' or 'use-credentials').

integrity string optional

Subresource integrity hash.

referrerPolicy string optional

Referrer policy for the script request.

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-...'
});