Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions packages/liquid-forge/lib/inject-assets.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
*/

import { ThemeStudioScriptInjector } from '../services/rendering/theme-studio-script-injector';
import { logger } from './logger';

/**
* Utilidad para inyectar CSS y JS en el HTML renderizado
Expand All @@ -24,6 +25,13 @@ import { ThemeStudioScriptInjector } from '../services/rendering/theme-studio-sc
* @returns El HTML con los assets inyectados
*/
export function injectAssets(html: string, assetCollector: any, domain?: string): string {
logger.info('[injectAssets] Iniciando inyección de assets', {
htmlLength: html.length,
domain,
hasDomain: !!domain,
hasThemeStudioScript: html.includes('data-fasttify-theme-studio="true"'),
});

let finalHtml = html;
const css = assetCollector.getCombinedCss();
const js = assetCollector.getCombinedJs();
Expand All @@ -44,7 +52,20 @@ export function injectAssets(html: string, assetCollector: any, domain?: string)

// Inyectar script del ThemeStudio en todas las tiendas
if (domain) {
const beforeLength = finalHtml.length;
finalHtml = ThemeStudioScriptInjector.injectScript(finalHtml, domain);
const afterLength = finalHtml.length;
logger.info('[injectAssets] Script del ThemeStudio procesado', {
domain,
beforeLength,
afterLength,
difference: afterLength - beforeLength,
finalScriptCount: (finalHtml.match(/data-fasttify-theme-studio="true"/g) || []).length,
});
} else {
logger.info('[injectAssets] No se inyecta script del ThemeStudio - sin dominio', {
domain,
});
}

return finalHtml;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
*/

import { iframeSelectionScript } from '@fasttify/theme-studio';
import { logger } from '../../lib/logger';

/**
* Servicio para inyectar el script del ThemeStudio en el HTML renderizado
Expand All @@ -26,7 +27,16 @@ export class ThemeStudioScriptInjector {
* @returns El script completo como string
*/
static generateScript(domain: string | undefined): string {
return iframeSelectionScript(domain || null);
logger.info('[ThemeStudioScriptInjector] Generando script', {
domain,
hasDomain: !!domain,
});
const script = iframeSelectionScript(domain || null);
logger.info('[ThemeStudioScriptInjector] Script generado', {
scriptLength: script.length,
scriptPreview: script.substring(0, 200),
});
return script;
}

/**
Expand All @@ -36,21 +46,63 @@ export class ThemeStudioScriptInjector {
* @returns El HTML con el script inyectado
*/
static injectScript(html: string, domain: string | undefined): string {
if (html.includes('data-fasttify-theme-studio="true"')) {
const hasExistingScript = html.includes('data-fasttify-theme-studio="true"');
const scriptCount = (html.match(/data-fasttify-theme-studio="true"/g) || []).length;

logger.info('[ThemeStudioScriptInjector] Intentando inyectar script', {
domain,
htmlLength: html.length,
hasExistingScript,
scriptCount,
hasHead: html.includes('</head>'),
hasBody: html.includes('</body>'),
});

if (hasExistingScript) {
logger.warn('[ThemeStudioScriptInjector] Script ya existe, evitando duplicado', {
scriptCount,
domain,
});
return html;
}

const script = this.generateScript(domain);
const scriptTag = `<script data-fasttify-theme-studio="true">${script}</script>`;

let result: string;
if (html.includes('</head>')) {
return html.replace('</head>', `${scriptTag}</head>`);
result = html.replace('</head>', `${scriptTag}</head>`);
logger.info('[ThemeStudioScriptInjector] Script inyectado en </head>', {
domain,
resultLength: result.length,
scriptTagLength: scriptTag.length,
});
} else if (html.includes('</body>')) {
result = html.replace('</body>', `${scriptTag}</body>`);
logger.info('[ThemeStudioScriptInjector] Script inyectado en </body>', {
domain,
resultLength: result.length,
scriptTagLength: scriptTag.length,
});
} else {
result = html + scriptTag;
logger.info('[ThemeStudioScriptInjector] Script inyectado al final', {
domain,
resultLength: result.length,
scriptTagLength: scriptTag.length,
});
}

if (html.includes('</body>')) {
return html.replace('</body>', `${scriptTag}</body>`);
// Verificar que se inyectó correctamente
const finalScriptCount = (result.match(/data-fasttify-theme-studio="true"/g) || []).length;
if (finalScriptCount !== 1) {
logger.error('[ThemeStudioScriptInjector] Error: Script inyectado múltiples veces', {
domain,
finalScriptCount,
expectedCount: 1,
});
}

return html + scriptTag;
return result;
}
}
Loading