ScriptManager
The ScriptManager is a low-level utility for managing script resolution, downloading, and execution in React Native applications. It's particularly useful when working with code splitting, dynamic imports, and Module Federation.
It can be used to download, manage and execute external (either local or remote) JavaScript code.
Usage
API Reference
ScriptManager.shared
The globally shared instance of ScriptManager. You should always use this instead of creating new instances.
- Type:
ScriptManager
addResolver
Adds a new script locator resolver to handle script resolution.
- Type:
addResolver(resolver: ScriptLocatorResolver, options?: ResolverOptions): void - Parameters:
resolver: Async function that resolves script location dataoptions: Configuration options for the resolverpriority: Priority of the resolver (default: 2)key: Unique key to identify the resolver
removeResolver
Removes a previously added resolver.
- Type:
removeResolver(resolver: ScriptLocatorResolver | string): boolean - Parameters:
resolver: The resolver function or its unique key to remove
- Returns:
trueif resolver was found and removed,falseotherwise
removeAllResolvers
Removes all previously added resolvers.
- Type:
removeAllResolvers(): void
setStorage
Sets a storage backend for caching resolved script locator data.
- Type:
setStorage(storage: StorageApi): void - Parameters:
storage: Storage API implementation withgetItemandsetItemmethods
loadScript
Resolves, downloads, and executes a script.
- Type:
loadScript(scriptId: string, caller?: string, webpackContext?: any, referenceUrl?: string): Promise<void> - Parameters:
scriptId: Id of the script to loadcaller: Name of the calling script (optional)webpackContext: Webpack context (optional)referenceUrl: Reference URL for resolution (optional)
prefetchScript
Downloads a script without executing it.
- Type:
prefetchScript(scriptId: string, caller?: string, webpackContext?: any, referenceUrl?: string): Promise<void> - Parameters:
scriptId: Id of the script to prefetchcaller: Name of the calling script (optional)webpackContext: Webpack context (optional)referenceUrl: Reference URL for resolution (optional)
invalidateScripts
Clears cache and removes downloaded files for given scripts.
- Type:
invalidateScripts(scriptIds?: string[]): Promise<string[]> - Parameters:
scriptIds: Array of script ids to invalidate (optional)
- Returns: Promise resolving to array of invalidated script ids
Events
The ScriptManager extends EventEmitter and provides a comprehensive event system for monitoring and reacting to script loading lifecycle events. Events can be used to track script resolution, loading progress, and handle errors.
Available Events
The following events are emitted by ScriptManager:
Subscribing to Events
Hooks
The ScriptManager provides a hook system that allows developers to intercept and customize the script loading process at various stages. The hooks provide fine-grained control over script resolution and loading, enabling advanced use cases like custom caching strategies, script transformation, and error handling.


