Skip to content
Merged
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
71 changes: 51 additions & 20 deletions lib/store-renderer/services/template-loader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)

Expand Down Expand Up @@ -264,6 +251,50 @@ class TemplateLoader {
})
}

/**
* Carga una plantilla desde CloudFront (producción)
*/
private async loadTemplateFromCloudFront(storeId: string, templatePath: string): Promise<string> {
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<string> {
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
*/
Expand Down