airbnb/lottie-web · error · Error
Cannot access DOM from worker thread
Error message
Cannot access DOM from worker thread
What it means
The worker override of the animation manager's searchAnimations() always throws. searchAnimations normally walks document for elements with data-anim-type attributes and auto-registers them, which is impossible in a Web Worker where document does not exist. The function is intentionally stubbed as an always-throw to fail fast on DOM-dependent API use.
Source
Thrown at player/js/animation/AnimationManagerWorkerOverride.js:154
}
}
function togglePause(animation) {
var i;
for (i = 0; i < len; i += 1) {
registeredAnimations[i].animation.togglePause(animation);
}
}
function destroy(animation) {
var i;
for (i = (len - 1); i >= 0; i -= 1) {
registeredAnimations[i].animation.destroy(animation);
}
}
function searchAnimations() {
throw new Error('Cannot access DOM from worker thread');
}
function resize() {
var i;
for (i = 0; i < len; i += 1) {
registeredAnimations[i].animation.resize();
}
}
function activate() {
if (!_isFrozen && playingAnimationsNum) {
if (_stopped) {
requestAnimationFrame(first);
_stopped = false;
}
}
}
View on GitHub (pinned to bede03d25d)
Solutions
- Do not call lottie.searchAnimations() in a worker; use lottie.loadAnimation({ renderer: 'canvas', animationData }) instead.
- Branch on environment: only call searchAnimations when typeof document !== 'undefined'.
- Keep DOM-based auto-discovery on the main thread and post animationData to the worker for rendering.
Example fix
// before (runs in worker build)
lottie.searchAnimations();
// after
if (typeof document !== 'undefined') {
lottie.searchAnimations();
} else {
lottie.loadAnimation({ renderer: 'canvas', animationData });
} Defensive patterns
Strategy: validation
Validate before calling
// searchAnimations is a DOM API; never call it in a worker.
function autoDiscover() {
if (typeof document === 'undefined') {
throw new Error('searchAnimations requires the DOM; use loadAnimation in a worker.');
}
return lottie.searchAnimations();
} Type guard
function canSearchDom() {
return typeof document !== 'undefined' && typeof document.getElementsByTagName === 'function';
} Prevention
- Gate every searchAnimations/registerAnimation call behind a document existence check.
- Restrict worker builds to the explicit loadAnimation API.
- Lint for searchAnimations usage inside worker-targeted modules.
When it happens
Trigger: Calling lottie.searchAnimations() (directly, or via the main module's auto-search/standalone path) while running inside the worker build. Also reachable through any shared code that calls the manager's searchAnimations.
Common situations: Including the worker build in code that also calls searchAnimations for auto-discovery; running a generic lottie integration module in both main thread and worker without branching; standalone build flow that triggers auto-search.
Related errors
- Cannot set data on wrapper for canvas worker renderer
- Only canvas renderer is supported when using worker.
- Canvas worker renderer cannot load animation from url
- Cannot load multiple segments in worker.
- The property has no keyframe at index {ind}
AI-assisted analysis of airbnb/lottie-web@bede03d25d (2026-08-13).
Data as JSON: /api/errors/af1d55b120e8e660.
Report an issue: GitHub.