-
-
Notifications
You must be signed in to change notification settings - Fork 0
Description
Problem:
When StudySession component was moved from platform-ui to common-ui, we encountered CORS errors due to environment variables not being properly initialized. Investigation revealed that the ENVIRONMENT_VARS.ts module was being loaded twice in different contexts - once with proper environment values and once without. This is a module duplication issue caused by how different packages in our monorepo access the same module in different contexts (Vue SFC vs. regular TS files).
Solution Implemented:
- Added a global singleton approach using
window.__SKUILDER_ENV__to ensure all instances of the module use the same environment values - Implemented a blocking retry mechanism to handle race conditions during initialization
- Added fallback default values to ensure the application works even if environment variables fail to load
Remaining TODOs for Future Robustness:
-
Non-blocking initialization:
- Replace the current blocking retry with a non-blocking mechanism
- Add an event system to notify components when environment variables are properly loaded
- Add
isReadyflag andonReadycallback registration
-
Build system improvements:
- Configure Vite/webpack to treat
commonpackage as an external dependency to prevent duplication - Set up proper environment variable sharing between packages in the monorepo
- Add a plugin to ensure environment variables are consistently available to all packages
- Configure Vite/webpack to treat
-
Architecture improvements:
- Create a proper dependency injection system for configuration values
- Implement a service locator pattern for database services that depend on environment variables
- Refactor database access to be more resilient to configuration changes
-
Testing:
- Add tests specifically for environment variable initialization
- Test behavior under various race conditions
- Document the proper usage pattern for modules that depend on environment variables
This solution provides a reliable way to handle environment variables in our monorepo, but a more comprehensive approach will be needed as the application grows.