feat: Implementa Cache Inteligente com SWR e Invalidação dinâmica de Assets

This commit is contained in:
João Pedro Toledo Goncalves 2026-02-04 20:19:47 -03:00
parent 5d1936e63e
commit 859d0851e5
3 changed files with 40 additions and 12 deletions

View File

@ -72,10 +72,10 @@ server {
# 2. Assets Estáticos (CACHE AGRESSIVO & MODERN)
location ~* \.(webp|avif|heic|apng|jpg|jpeg|gif|png|ico|svg|mjs|js|ts|wasm|json|woff2?|ttf|otf|eot|pdf|css|less|scss)$ {
expires $cache_asset_ttl;
add_header Cache-Control "public, immutable";
add_header Alt-Svc 'h3=":443"; ma=86400';
proxy_cache_valid 200 30d;
include snippets/cache_optimizer.conf;
add_header Cache-Control $cache_control_header;
proxy_cache_valid 200 $cache_asset_ttl;
proxy_pass http://ferreirareal_backend;
# Rate Limit Diferenciado

View File

@ -0,0 +1,29 @@
# --- Pathfinder Smart Cache Optimization ---
# 1. Stale-While-Revalidate (SWR) Global
# Serve conteúdo antigo enquanto atualiza em background (Ultra rápido)
proxy_cache_revalidate on;
proxy_cache_background_update on;
proxy_cache_use_stale error timeout updating http_500 http_502 http_503 http_504;
# 2. Configurações de Cache-Control por Tipo de Arquivo
# Nota: $cache_asset_ttl vem do security_maps.conf
add_header X-Cache-Status $upstream_cache_status;
# Trata HTML: Cachear mas sempre perguntar ao servidor se mudou (no-cache)
# Isso permite o 304 Not Modified, evitando download desnecessário mas garantindo atualização.
map $sent_http_content_type $cache_control_header {
default "no-cache, no-store, must-revalidate";
~*text/html "no-cache, must-revalidate";
~*application/json "no-cache, must-revalidate";
~*(image|audio|video|font) "public, max-age=31536000, immutable";
~*(text/css|javascript) $asset_cache_policy;
}
# 3. Política Diferenciada para JS/CSS
# Se tiver versão (?v= ou .v1.) -> Cache Eterno
# Se for nome limpo -> Cache curto com revalidação obrigatória
map $request_uri $asset_cache_policy {
~*(\?v=|\?id=|\.v[0-9]|\.[0-9a-f]{8,}) "public, max-age=31536000, immutable";
default "public, max-age=3600, must-revalidate";
}

View File

@ -114,15 +114,14 @@ map $security_score $heavy_limit_key {
}
# 3. Cache Asset TTL - Suporte Total 2026 (Modern Web)
# No proxy_cache usamos um tempo curto, o Cache-Control (Browser) é que decide o tempo longo.
map $request_uri $cache_asset_ttl {
# Imagens Modernas e Tradicionais
~*\.(webp|avif|heic|apng|jpg|jpeg|gif|png|ico|svg)$ 1y;
# Scripts Modernos (Modules) e Tradicionais
~*\.(mjs|js|ts|wasm|json)$ 1y;
# Imagens e Mídia
~*\.(webp|avif|heic|apng|jpg|jpeg|gif|png|ico|svg)$ 1d;
# Scripts e Estilos (Revalidação rápida no Proxy)
~*\.(mjs|js|ts|wasm|json|css|less|scss)$ 6h;
# Fontes e Documentos
~*\.(woff2?|ttf|otf|eot|pdf)$ 1y;
# Estilos e Outros
~*\.(css|less|scss)$ 1y;
# Padrão: Sem Cache de TTL Longo
~*\.(woff2?|ttf|otf|eot|pdf)$ 7d;
# Padrão: Sem Cache
default off;
}