Skip to content

Supported File Types

ResourceLoader.js automatically detects resource types from file extensions and uses the appropriate loading mechanism for each.

CategoryExtensionsLoading MechanismonSuccess receivesPromise value
Scriptsjs<script> elementURL stringURL string
Stylesheetscss<link rel="stylesheet"> elementURL stringURL string
Datajsonfetch() + response.json()Parsed objectParsed object
Imagesjpg, jpeg, png, gif, svg, webp<img> elementURL stringURL string
Fontswoff, woff2FontFace APIundefinedundefined
Audiomp3, ogg, wavfetch() + response.blob()BlobBlob
Videomp4, avi, webmfetch() + response.blob()BlobBlob
Binary / PDFpdf, zip, binfetch() + response.blob()BlobBlob

Element-Based Loading (Scripts, CSS, Images)

Section titled “Element-Based Loading (Scripts, CSS, Images)”

For js, css, and image types, ResourceLoader.js creates a DOM element and waits for its load event:

  • Scripts: Creates <script src="url"> and appends it to <head> (or <body> with appendToBody: true)
  • Stylesheets: Creates <link rel="stylesheet" href="url"> and appends it to <head>
  • Images: Creates <img src="url"> — the image is pre-fetched and cached by the browser but not displayed; you display it using the URL in onSuccess

The success callback and Promise value receive the original URL string.

Fetch-Based Loading (JSON, Audio, Video, Binary)

Section titled “Fetch-Based Loading (JSON, Audio, Video, Binary)”

For json, audio, video, and binary types, ResourceLoader.js uses the fetch() API:

  • JSON: fetch(url).then(r => r.json()) — returns the parsed JavaScript object
  • Blob types: fetch(url).then(r => r.blob()) — returns a Blob object

These loads respect the timeout option via AbortController and can be cancelled with cancelResource().

For woff and woff2 files, ResourceLoader.js uses the FontFace constructor:

const font = new FontFace('customFont', `url(${url})`);
await font.load();
document.fonts.add(font);

The font is registered in document.fonts and available via font-family: "customFont" in CSS.


The type is determined by the file extension (the part after the last ., before any ? or #):

https://example.com/app.js → js
https://example.com/styles.css → css
https://example.com/data.json → json
https://example.com/photo.jpg → jpg
https://example.com/file.js?v=1 → js (query string ignored)
https://example.com/file.js#hash → js (fragment ignored)

URL Pattern Inference (Extensionless URLs)

Section titled “URL Pattern Inference (Extensionless URLs)”

When a URL has no recognisable file extension, ResourceLoader.js applies heuristics:

URL contains…Inferred type
/jsonjson
jsonplaceholderjson
/apijson
/imagespng
picsumpng
/imgpng

Examples:

// All inferred as JSON:
ResourceLoader.include([
'https://jsonplaceholder.typicode.com/todos/1',
'https://api.example.com/v2/users',
'https://example.com/data/json/config',
]);
// Inferred as image:
ResourceLoader.include([
'https://picsum.photos/200/300',
]);

After loading, scripts are executed automatically by the browser. CSS is applied immediately. No further code is needed.

ResourceLoader.include(['https://api.example.com/config.json'], {
onSuccess: (config) => {
// config is already a parsed object
document.title = config.appTitle;
},
});
ResourceLoader.include(['https://example.com/hero.jpg'], {
onSuccess: (url) => {
const img = document.createElement('img');
img.src = url;
img.alt = 'Hero image';
document.getElementById('hero').appendChild(img);
},
});
await ResourceLoader.include(['https://example.com/fonts/MyFont.woff2']);
// Apply the font — the family name is always "customFont"
document.body.style.fontFamily = '"customFont", sans-serif';
ResourceLoader.include(['https://example.com/track.mp3'], {
onSuccess: (blob) => {
const url = URL.createObjectURL(blob);
const audio = new Audio(url);
audio.play();
audio.addEventListener('ended', () => URL.revokeObjectURL(url));
},
});

PDFs and Binary — trigger a download or embed

Section titled “PDFs and Binary — trigger a download or embed”
ResourceLoader.include(['https://example.com/report.pdf'], {
onSuccess: (blob) => {
const url = URL.createObjectURL(blob);
const link = document.createElement('a');
link.href = url;
link.download = 'report.pdf';
link.click();
URL.revokeObjectURL(url);
},
});