Skip to content

Conversation

@scottlovegrove
Copy link
Contributor

@scottlovegrove scottlovegrove commented Dec 3, 2025

Summary

Resolves browser compatibility issues with undici by replacing static imports with dynamic imports and environment detection.

  • Problem: Static undici import caused Cannot read properties of undefined (reading 'node') errors in browser environments
  • Solution: Dynamic import that only loads undici in Node.js environments using process.versions?.node detection
  • Impact: Enables browser usage while maintaining Node.js performance benefits

Have tested this with the Todoist Outlook add-in as this is where it was originally found.

Changes

  • Remove static import { Agent } from 'undici'
  • Add getHttpAgent() function with environment detection
  • Implement dynamic await import('undici') for Node.js only
  • Cache agent instance to prevent multiple imports
  • Update fetchWithRetry to use await getHttpAgent()

Browser Compatibility

Before: TypeError: Cannot read properties of undefined (reading 'node')
After: No errors, undici never loaded in browser environments

Testing

  • ✅ All 262 existing tests pass in Node.js
  • ✅ TypeScript compilation succeeds without errors
  • ✅ Vite build completes successfully in browser environment
  • ✅ No breaking changes for existing Node.js usage
  • ✅ Custom fetch adapters (like Obsidian) continue working unchanged

Backward Compatibility

Zero breaking changes - all existing code continues to work:

  • Node.js users still get undici Agent benefits (connection pooling, keepAlive management)
  • Custom fetch users have no overhead (agent never created)
  • Browser users can now use the SDK without errors

🤖 Generated with Claude Code

Replace static undici import with dynamic import and environment detection
to prevent 'process.versions.node' errors in browser environments.

- Remove static import of undici Agent
- Add dynamic import that only loads undici in Node.js environments
- Implement environment detection using process.versions?.node
- Cache agent instance to prevent multiple imports
- Maintain full backward compatibility for Node.js users
- Enable browser usage without undici import errors

Fixes browser bundling issues while preserving Node.js performance benefits.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>
@scottlovegrove scottlovegrove self-assigned this Dec 3, 2025
@scottlovegrove scottlovegrove changed the title Fix undici browser compatibility with dynamic imports fix: Fix undici browser compatibility with dynamic imports Dec 3, 2025
@scottlovegrove scottlovegrove merged commit 7cd8707 into main Dec 3, 2025
2 checks passed
@scottlovegrove scottlovegrove deleted the scottl/undici-fix branch December 3, 2025 17:09
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants