Initial commit
This commit is contained in:
87
js/env-loader.js
Normal file
87
js/env-loader.js
Normal file
@@ -0,0 +1,87 @@
|
||||
// env-loader.js
|
||||
// This module is responsible for loading environment variables for the PWA
|
||||
|
||||
// Store environment variables in a global object
|
||||
window.ENV_CONFIG = {
|
||||
// Default values that will be overridden when config.env.js loads
|
||||
PUBLIC_VAPID_KEY: 'your_public_vapid_key_here',
|
||||
BACKEND_URL: 'https://your-push-server.example.com'
|
||||
};
|
||||
|
||||
// Function to load environment variables from config.env.js
|
||||
async function loadEnvVariables() {
|
||||
try {
|
||||
// Try to fetch the config.env.js file which will be generated at build time or deployment
|
||||
const response = await fetch('/config.env.js');
|
||||
|
||||
if (!response.ok) {
|
||||
console.warn('Could not load config.env.js file. Using default values.');
|
||||
return;
|
||||
}
|
||||
|
||||
const configText = await response.text();
|
||||
|
||||
// Extract the configuration object from the JavaScript file
|
||||
// The file should be in format: window.ENV_CONFIG = { key: "value" };
|
||||
try {
|
||||
// Create a safe way to evaluate the config file
|
||||
const configScript = document.createElement('script');
|
||||
configScript.textContent = configText;
|
||||
document.head.appendChild(configScript);
|
||||
document.head.removeChild(configScript);
|
||||
|
||||
console.log('Environment variables loaded successfully from config.env.js');
|
||||
|
||||
// Dispatch an event to notify that environment variables have been loaded
|
||||
window.dispatchEvent(new CustomEvent('env-config-loaded', {
|
||||
detail: { config: window.ENV_CONFIG }
|
||||
}));
|
||||
} catch (parseError) {
|
||||
console.error('Error parsing config.env.js:', parseError);
|
||||
}
|
||||
} catch (error) {
|
||||
console.error('Error loading environment variables:', error);
|
||||
}
|
||||
}
|
||||
|
||||
// Export function to initialize environment variables
|
||||
export async function initEnv() {
|
||||
await loadEnvVariables();
|
||||
return window.ENV_CONFIG;
|
||||
}
|
||||
|
||||
// Start loading environment variables immediately
|
||||
initEnv();
|
||||
|
||||
// Export access functions for environment variables
|
||||
export function getEnv(key, defaultValue = '') {
|
||||
return window.ENV_CONFIG[key] || defaultValue;
|
||||
}
|
||||
|
||||
// Export a function to wait for environment variables to be loaded
|
||||
export function waitForEnv() {
|
||||
return new Promise((resolve) => {
|
||||
// If we already have non-default values, resolve immediately
|
||||
if (window.ENV_CONFIG.BACKEND_URL !== 'https://your-push-server.example.com') {
|
||||
resolve(window.ENV_CONFIG);
|
||||
return;
|
||||
}
|
||||
|
||||
// Otherwise, wait for the env-config-loaded event
|
||||
window.addEventListener('env-config-loaded', (event) => {
|
||||
resolve(event.detail.config);
|
||||
}, { once: true });
|
||||
|
||||
// Set a timeout to resolve with current values if loading takes too long
|
||||
setTimeout(() => {
|
||||
console.warn('Environment loading timed out, using current values');
|
||||
resolve(window.ENV_CONFIG);
|
||||
}, 3000);
|
||||
});
|
||||
}
|
||||
|
||||
export default {
|
||||
initEnv,
|
||||
getEnv,
|
||||
waitForEnv
|
||||
};
|
||||
Reference in New Issue
Block a user