Dynamic Library Loading
Dynamic library loading functionality for BetterForms - Load external JavaScript libraries, ESM modules, and CSS files from CDN dynamically within function actions.
Version: 3.3.1+
Feature: BF.libraryLoadOnce(), BF.libraryIsLoaded(), BF.libraryGetLoaded()
Overview
Load external JavaScript libraries, ESM modules, and CSS files from CDN dynamically within your function actions. Libraries are loaded once and cached automatically (idempotent), with built-in XSS protection and race condition prevention.
Why Use This?
Reduce initial page load - Only load libraries when needed
Self-contained components - Components can load their own dependencies
Version flexibility - Use different library versions per form
No rebuild required - Add new libraries without webpack rebuild
Quick Start
Load a UMD Library
// Load PapaParse from CDN
await BF.libraryLoadOnce('https://cdn.jsdelivr.net/npm/[email protected]/papaparse.min.js');
// Use it immediately
const result = Papa.parse('Name,Age\nJohn,30', { header: true });
console.log(result.data); // [{Name: "John", Age: "30"}]Load an ESM Module
Load CSS
API Reference
BF.libraryLoadOnce(url, options)
BF.libraryLoadOnce(url, options)Load a library from CDN. Returns immediately if already loaded (idempotent).
Parameters:
url
String
required
CDN URL to JavaScript, ESM module, or CSS file
options
Object
{}
Optional configuration
options.type
String
'script'
'script' or 'stylesheet'
options.moduleType
String
'classic'
'classic' for UMD or 'module' for ESM
options.timeout
Number
30000
Timeout in milliseconds
options.attributes
Object
{}
Additional HTML attributes (e.g., integrity, crossorigin)
Returns:
UMD/Script: Promise resolving to
{ url, type, element, cached }ESM Module: Promise resolving to the module object
Throws: Error if network failure, timeout, or invalid parameters
BF.libraryIsLoaded(url)
BF.libraryIsLoaded(url)Check if a library is already loaded.
Returns: Boolean
BF.libraryGetLoaded()
BF.libraryGetLoaded()Get array of all loaded library URLs.
Returns: Array of URL strings
Usage Examples
Example 1: Chart.js
Example 2: DayJS (ESM)
Example 3: Axios for API Calls
Example 4: Load with Security (SRI)
Example 5: Error Handling
Example 6: Multiple Libraries
Example 7: Dynamic Library Selection
ESM Module Scope
Important: ESM modules are scoped to the function action where they're loaded. Unlike UMD libraries (which are automatically global), ESM modules return an object that only exists in the current function's scope.
UMD vs ESM Behavior
UMD Libraries (automatically global):
ESM Modules (function-scoped):
Solution 1: Re-load in Each Action (Recommended)
The library is cached, so re-loading is < 1ms:
When to use:
β Most common case
β Clean, simple code
β Nearly instant (cached)
Solution 2: Bind to Window (If Needed Globally)
Manually store the module on window for global access:
When to use:
Used across many actions
Need consistent reference
Don't mind global namespace
Do You Even Need It Again?
In many cases, you won't need the module after initial setup:
Only need multi-action access if:
Calling library functions repeatedly
Building complex multi-step workflows
Library maintains state you need to access
Best Practices
β
DO:
Use version pinning: Specify exact versions in CDN URLs
Idempotent by design: No need to check before loading
Use error handling: Always catch potential failures
Prefer UMD builds: More compatible, simpler to use
β DON'T:
Don't load untrusted CDNs: Security risk
Don't load without error handling: Can break your form
Don't use latest/unstable versions: Use pinned versions
Don't load duplicate libraries: Check first with
BF.libraryIsLoaded()
Supported Library Formats
β
UMD (Universal Module Definition)
Most common format
Exposes global variable
Works everywhere
Examples: Chart.js, PapaParse, QRCode, Axios
β
ESM (ES Modules)
Modern format
Uses
import/exportReturns module object
Examples: DayJS, D3.js, Lit (web components)
β
CSS Stylesheets
Any CSS file
Animations, themes, frameworks
Examples: Animate.css, Font Awesome
Finding CDN URLs
Finding the correct CDN URL can be tricky. Here's how:
Step 1: Find the Package on npm
Go to
https://www.npmjs.com/package/package-nameCheck for version number and main file info
Step 2: Browse the CDN
jsDelivr (Recommended)
Browse:
https://cdn.jsdelivr.net/npm/package-name@version/Example:
https://cdn.jsdelivr.net/npm/[email protected]/Look in:
dist/,build/, or root folderESM shortcut: Add
/+esmto auto-find ESM entry
Common file patterns:
package.min.js- Minified UMD (most common)package.umd.js- UMD builddist/package.js- Main distribution filebuild/package.min.js- Built file
unpkg
Browse:
https://unpkg.com/package@version/?meta(add?meta)Example:
https://unpkg.com/[email protected]/?meta
cdnjs
Search:
https://cdnjs.com/libraries/package-nameExample:
https://cdnjs.com/libraries/Chart.js
Step 3: Verify the File Works
Test in browser console first:
Troubleshooting 404 Errors
Check version exists: Verify on npmjs.com
Try alternate paths:
package.min.jsdist/package.min.jsbuild/package.min.jsindex.js
Check package.json:
https://cdn.jsdelivr.net/npm/package@version/package.jsonLook for
"main","browser", or"module"fields
Real Examples
Chart.js:
Browse:
https://cdn.jsdelivr.net/npm/[email protected]/Find:
dist/chart.umd.jsURL:
https://cdn.jsdelivr.net/npm/[email protected]/dist/chart.umd.js
PapaParse:
Browse:
https://cdn.jsdelivr.net/npm/[email protected]/Find:
papaparse.min.js(at root)URL:
https://cdn.jsdelivr.net/npm/[email protected]/papaparse.min.js
DayJS ESM:
Use shortcut:
https://cdn.jsdelivr.net/npm/[email protected]/+esmDone! (jsDelivr finds ESM automatically)
Common Libraries
Chart.js (Charting)
PapaParse (CSV Parser)
QRCode (QR Generator)
Axios (HTTP Client)
DayJS (ESM - Date Library)
Sortable.js (Drag & Drop)
Troubleshooting
Library Not Available After Loading
Problem: Library loads but window.LibraryName is undefined
Solution: You might have loaded an ESM module as UMD
Timeout Error
Problem: Library takes too long to load
Solution: Increase timeout or check network
CORS Error
Problem: "blocked by CORS policy"
Solution: Use a reputable CDN that supports CORS (jsDelivr, unpkg, cdnjs)
Version Conflicts
Problem: Library already loaded but different version
Solution: Check what's loaded first
Need Help?
Check the BetterForms documentation
Visit the BetterForms community forum
Review usage examples above
Test in browser console first before using in function actions
Happy coding! π
Last updated