diff --git a/lib/store-renderer/services/template-loader.ts b/lib/store-renderer/services/template-loader.ts index 40897dbd..fbc766f8 100644 --- a/lib/store-renderer/services/template-loader.ts +++ b/lib/store-renderer/services/template-loader.ts @@ -36,7 +36,7 @@ class TemplateLoader { } /** - * Carga una plantilla específica desde S3 + * Carga una plantilla específica desde S3 o CloudFront * @param storeId - ID de la tienda * @param templatePath - Ruta de la plantilla (ej: "layout/theme.liquid") * @returns Contenido de la plantilla @@ -49,28 +49,15 @@ class TemplateLoader { return cached.content } - if (!this.s3Client || !this.bucketName) { - throw new Error('S3 client or bucket not configured') - } - - // Construir la key de S3 - const s3Key = `templates/${storeId}/${templatePath}` + let content: string - // Cargar desde S3 - const command = new GetObjectCommand({ - Bucket: this.bucketName, - Key: s3Key, - }) - - const response = await this.s3Client.send(command) - - if (!response.Body) { - throw new Error(`Template not found: ${templatePath}`) + // En producción usar CloudFront, en desarrollo usar S3 directo + if (this.appEnv === 'production' && this.cloudFrontDomain) { + content = await this.loadTemplateFromCloudFront(storeId, templatePath) + } else { + content = await this.loadTemplateFromS3(storeId, templatePath) } - // Convertir stream a string usando AWS SDK v3 - const content = await response.Body!.transformToString() - // Guardar en caché this.setCachedTemplate(storeId, templatePath, content) @@ -264,6 +251,50 @@ class TemplateLoader { }) } + /** + * Carga una plantilla desde CloudFront (producción) + */ + private async loadTemplateFromCloudFront(storeId: string, templatePath: string): Promise { + const templateUrl = `https://${this.cloudFrontDomain}/templates/${storeId}/${templatePath}` + + const response = await fetch(templateUrl) + + if (!response.ok) { + throw new Error( + `Template not found: ${templatePath} (CloudFront returned ${response.status})` + ) + } + + return await response.text() + } + + /** + * Carga una plantilla desde S3 directamente (desarrollo) + */ + private async loadTemplateFromS3(storeId: string, templatePath: string): Promise { + if (!this.s3Client || !this.bucketName) { + throw new Error('S3 client or bucket not configured') + } + + // Construir la key de S3 + const s3Key = `templates/${storeId}/${templatePath}` + + // Cargar desde S3 + const command = new GetObjectCommand({ + Bucket: this.bucketName, + Key: s3Key, + }) + + const response = await this.s3Client.send(command) + + if (!response.Body) { + throw new Error(`Template not found: ${templatePath}`) + } + + // Convertir stream a string usando AWS SDK v3 + return await response.Body!.transformToString() + } + /** * Obtiene una plantilla del caché si existe y es válida */