This commit is contained in:
2025-09-03 14:35:18 -03:00
parent 6a508f468b
commit 57fe26afa9

View File

@@ -1,11 +1,10 @@
(function() { // frontend/public/bootstrap.js
// --- LÓGICA DE DETECCIÓN AUTOMÁTICA DE LA RUTA BASE ---
// document.currentScript.src nos da la URL completa del propio script bootstrap.js
const scriptUrl = new URL(document.currentScript.src);
// La ruta base es el origen (ej. 'https://elecciones2025.eldia.com')
const WIDGETS_HOST = scriptUrl.origin;
// --- FUNCIONES AUXILIARES (sin cambios) --- (function() {
// El dominio donde se alojan los widgets
const WIDGETS_HOST = 'https://elecciones2025.eldia.com';
// Función para cargar dinámicamente un script
function loadScript(src) { function loadScript(src) {
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
const script = document.createElement('script'); const script = document.createElement('script');
@@ -17,6 +16,7 @@
}); });
} }
// Función para cargar dinámicamente una hoja de estilos
function loadCSS(href) { function loadCSS(href) {
const link = document.createElement('link'); const link = document.createElement('link');
link.rel = 'stylesheet'; link.rel = 'stylesheet';
@@ -24,17 +24,17 @@
document.head.appendChild(link); document.head.appendChild(link);
} }
// --- LÓGICA PRINCIPAL --- // Función principal
async function initWidgets() { async function initWidgets() {
try { try {
// 1. Obtener el manifest.json desde la ruta base detectada // 1. Obtener el manifest.json para saber los nombres de archivo actuales
const response = await fetch(`${WIDGETS_HOST}/manifest.json`); const response = await fetch(`${WIDGETS_HOST}/manifest.json`);
if (!response.ok) { if (!response.ok) {
throw new Error('No se pudo cargar el manifest. (404 Not Found)'); throw new Error('No se pudo cargar el manifest de los widgets.');
} }
const manifest = await response.json(); const manifest = await response.json();
// 2. Encontrar el punto de entrada principal // 2. Encontrar el punto de entrada principal (nuestro main.tsx)
const entryKey = Object.keys(manifest).find(key => manifest[key].isEntry); const entryKey = Object.keys(manifest).find(key => manifest[key].isEntry);
if (!entryKey) { if (!entryKey) {
throw new Error('No se encontró el punto de entrada en el manifest.'); throw new Error('No se encontró el punto de entrada en el manifest.');
@@ -43,7 +43,7 @@
const entry = manifest[entryKey]; const entry = manifest[entryKey];
const jsUrl = `${WIDGETS_HOST}/${entry.file}`; const jsUrl = `${WIDGETS_HOST}/${entry.file}`;
// 3. Cargar el CSS // 3. Cargar el CSS si existe
if (entry.css && entry.css.length > 0) { if (entry.css && entry.css.length > 0) {
entry.css.forEach(cssFile => { entry.css.forEach(cssFile => {
const cssUrl = `${WIDGETS_HOST}/${cssFile}`; const cssUrl = `${WIDGETS_HOST}/${cssFile}`;
@@ -51,12 +51,20 @@
}); });
} }
// 4. Cargar el JS principal // 4. Cargar el JS principal y esperar a que esté listo
await loadScript(jsUrl); await loadScript(jsUrl);
// 5. Una vez cargado, llamar a la función de renderizado
if (window.EleccionesWidgets && typeof window.EleccionesWidgets.render === 'function') {
window.EleccionesWidgets.render();
}
} catch (error) { } catch (error) {
console.error('Error al inicializar los widgets de Elecciones:', error); console.error('Error al inicializar los widgets de elecciones:', error);
} }
} }
// Iniciar todo el proceso
initWidgets(); initWidgets();
})(); })();