ansango / wiki
 ·  2 min de lectura

Cache Control Header

Stale while revalidate, configurar Cache-Control headers óptimos en Cloudflare Pages

Headers HTTP que le dicen al navegador cuánto tiempo cachear cada archivo.

Implementación:

# Cloudflare Pages Custom Headers
# https://developers.cloudflare.com/pages/platform/headers/

# HTML pages - Always revalidate (para contenido nuevo)
/*.html
  Cache-Control: public, max-age=0, must-revalidate
  X-Content-Type-Options: nosniff
  X-Frame-Options: DENY
  Referrer-Policy: strict-origin-when-cross-origin

# Root HTML
/
  Cache-Control: public, max-age=0, must-revalidate
  X-Content-Type-Options: nosniff
  X-Frame-Options: DENY
  Referrer-Policy: strict-origin-when-cross-origin

# JavaScript - Cachear 1 año (tienen hash en el nombre)
/*.js
  Cache-Control: public, max-age=31536000, immutable
  X-Content-Type-Options: nosniff

# CSS - Cachear 1 año (tienen hash en el nombre)
/*.css
  Cache-Control: public, max-age=31536000, immutable
  X-Content-Type-Options: nosniff

# Assets folder (todo lo de Astro con hash)
/assets/*
  Cache-Control: public, max-age=31536000, immutable

# Fonts - Cachear 1 año
/fonts/*
  Cache-Control: public, max-age=31536000, immutable
  Access-Control-Allow-Origin: *

# Images - Cache 1 semana con stale-while-revalidate
/images/*
  Cache-Control: public, max-age=604800, stale-while-revalidate=86400

# Favicons y manifests - Cache 1 día
/*.png
  Cache-Control: public, max-age=86400
/*.ico
  Cache-Control: public, max-age=86400
/*.svg
  Cache-Control: public, max-age=86400
/*.webmanifest
  Cache-Control: public, max-age=86400
/*.xml
  Cache-Control: public, max-age=86400

# RSS Feed - Cache 1 hora
/rss.xml
  Cache-Control: public, max-age=3600
  Content-Type: application/xml; charset=utf-8

# Sitemap - Cache 1 día
/sitemap*.xml
  Cache-Control: public, max-age=86400
  Content-Type: application/xml; charset=utf-8

# robots.txt - Cache 1 día
/robots.txt
  Cache-Control: public, max-age=86400
  Content-Type: text/plain; charset=utf-8

# Security headers globales
/*
  X-Content-Type-Options: nosniff
  X-Frame-Options: DENY
  Referrer-Policy: strict-origin-when-cross-origin
  Permissions-Policy: geolocation=(), microphone=(), camera=()

Beneficio:

📋 Cómo funciona

Cloudflare Pages lee automáticamente el archivo _headers y aplica esos headers HTTP a las rutas especificadas.

🎯 Estrategia implementada

HTML (max-age=0):

Assets con hash (immutable, 1 año):

Imágenes (1 semana + stale-while-revalidate):

Security headers:

✅ Verificar después del deploy

# Ver headers aplicados
curl -I https://ansango.com/

# O en browser DevTools:
# Network tab → Click en recurso → Headers tab

Deberías ver:

cache-control: public, max-age=0, must-revalidate
x-frame-options: DENY
x-content-type-options: nosniff