After deployment, the application shows errors:
Failed to execute 'attachShadow' on 'Element': Shadow root cannot be created on a host which already hosts a shadow tree.
This error typically occurs when:
- Browser extensions (password managers, ad blockers) inject their own shadow DOM elements
- Deployment platform (AWS Amplify) injects monitoring/analytics scripts that conflict
- Multiple attempts to attach shadow roots to the same element
Added a script that intercepts attachShadow calls and prevents duplicate shadow root creation.
If the issue persists after deployment:
-
Check Browser Extensions
- Test in incognito/private mode with all extensions disabled
- Common problematic extensions: LastPass, 1Password, Grammarly, ad blockers
-
AWS Amplify Specific
- Check if Amplify is injecting any monitoring scripts
- In Amplify Console, go to App settings > Environment variables
- Add:
AMPLIFY_MONOREPO_APP_ROOT=.(if not already set)
-
Add CSP Headers (if needed) Create a
customHttp.ymlin your project root:customHeaders: - pattern: "**/*" headers: - key: "Content-Security-Policy" value: "script-src 'self' 'unsafe-inline' 'unsafe-eval';"
-
Alternative Fix - Vite Config If the issue is related to build optimization, you can try adding to
vite.config.ts:build: { commonjsOptions: { transformMixedEsModules: true } }
- Build locally:
yarn build - Test production build:
yarn preview - Check browser console for any shadow DOM warnings
- Test in different browsers and incognito mode
- Regularly test production builds locally
- Monitor for browser extension conflicts
- Keep deployment platform configurations documented