{"record":{"id":"af1d55b120e8e660","repo":"airbnb/lottie-web","slug":"cannot-access-dom-from-worker-thread","errorCode":null,"errorMessage":"Cannot access DOM from worker thread","messagePattern":"Cannot access DOM from worker thread","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"player/js/animation/AnimationManagerWorkerOverride.js","lineNumber":154,"sourceCode":"    }\n  }\n\n  function togglePause(animation) {\n    var i;\n    for (i = 0; i < len; i += 1) {\n      registeredAnimations[i].animation.togglePause(animation);\n    }\n  }\n\n  function destroy(animation) {\n    var i;\n    for (i = (len - 1); i >= 0; i -= 1) {\n      registeredAnimations[i].animation.destroy(animation);\n    }\n  }\n\n  function searchAnimations() {\n    throw new Error('Cannot access DOM from worker thread');\n  }\n\n  function resize() {\n    var i;\n    for (i = 0; i < len; i += 1) {\n      registeredAnimations[i].animation.resize();\n    }\n  }\n\n  function activate() {\n    if (!_isFrozen && playingAnimationsNum) {\n      if (_stopped) {\n        requestAnimationFrame(first);\n        _stopped = false;\n      }\n    }\n  }\n","sourceCodeStart":136,"sourceCodeEnd":172,"githubUrl":"https://github.com/airbnb/lottie-web/blob/bede03d25d232826e0c9dca1733d542d8a7754fb/player/js/animation/AnimationManagerWorkerOverride.js#L136-L172","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","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."],"exampleFix":"// before (runs in worker build)\nlottie.searchAnimations();\n// after\nif (typeof document !== 'undefined') {\n  lottie.searchAnimations();\n} else {\n  lottie.loadAnimation({ renderer: 'canvas', animationData });\n}","handlingStrategy":"validation","validationCode":"// searchAnimations is a DOM API; never call it in a worker.\nfunction autoDiscover() {\n  if (typeof document === 'undefined') {\n    throw new Error('searchAnimations requires the DOM; use loadAnimation in a worker.');\n  }\n  return lottie.searchAnimations();\n}","typeGuard":"function canSearchDom() {\n  return typeof document !== 'undefined' && typeof document.getElementsByTagName === 'function';\n}","tryCatchPattern":null,"preventionTips":["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."],"tags":["lottie","web-worker","dom","configuration"],"backgroundTag":null,"analyzedSha":"bede03d25d232826e0c9dca1733d542d8a7754fb","analyzedAt":"2026-08-13T05:40:22.197Z","schemaVersion":2},"datasetVersion":"2026-08-13T09:17:06.757Z"}