monacousa-portal/plugins/04.config-cache-init.client.ts

80 lines
2.6 KiB
TypeScript

/**
* Config Cache Initialization Plugin
* Ensures config cache is properly initialized and prevents reload loops
* Specifically designed to fix Safari iOS issues
*/
export default defineNuxtPlugin({
name: 'config-cache-init',
enforce: 'pre', // Run before other plugins
async setup() {
console.log('[config-cache-init] Initializing config cache plugin');
// Only run on client side
if (typeof window === 'undefined') {
return;
}
// Initialize a flag to prevent multiple initializations
if ((window as any).__configCacheInitialized) {
console.log('[config-cache-init] Config cache already initialized');
return;
}
// Mark as initialized
(window as any).__configCacheInitialized = true;
// Initialize the config cache structure if not already present
if (!(window as any).__configCache) {
(window as any).__configCache = {
recaptcha: null,
registration: null,
recaptchaLoading: false,
registrationLoading: false,
recaptchaError: null,
registrationError: null
};
console.log('[config-cache-init] Config cache structure initialized');
}
// Initialize call history for circuit breaker
if (!(window as any).__configCallHistory) {
(window as any).__configCallHistory = {};
console.log('[config-cache-init] Call history initialized');
}
// Add a global error handler to catch and prevent reload loops
const originalError = window.onerror;
window.onerror = function(msg, url, lineNo, columnNo, error) {
// Check for common reload loop patterns
if (typeof msg === 'string' && (
msg.includes('Maximum call stack') ||
msg.includes('too much recursion') ||
msg.includes('RangeError')
)) {
console.error('[config-cache-init] Potential reload loop detected:', msg);
// Prevent default error handling which might cause reload
return true;
}
// Call original error handler if it exists
if (originalError) {
return originalError(msg, url, lineNo, columnNo, error);
}
return false;
};
// Add unhandled rejection handler
window.addEventListener('unhandledrejection', (event) => {
if (event.reason?.message?.includes('config') ||
event.reason?.message?.includes('reload')) {
console.error('[config-cache-init] Unhandled config-related rejection:', event.reason);
// Prevent default which might cause reload
event.preventDefault();
}
});
console.log('[config-cache-init] Config cache plugin initialized successfully');
}
});