Capítulo 323 de 456
Guia de referência para rodar Next.js fora da Vercel: reverse proxy, otimização de imagens, cache/ISR, build cache multi-container, encryption key de Server Functions, deployment ID e version skew, e graceful shutdown.
next start; para static export precisa de loader customizado.next start; não suportado em static export (precisa de acesso à request).NEXT_PUBLIC_ expõe ao browser e é inlined no build.get, set, revalidateTag, resetRequestCache, configurada via cacheHandler em next.config.js.cacheMaxMemorySize: 0: desabilita cache em memória padrão (necessário ao usar cache handler distribuído).generateBuildId: gera um build ID consistente entre containers (ex. a partir do hash do git).NEXT_SERVER_ACTIONS_ENCRYPTION_KEY: chave AES (16/24/32 bytes, base64) que deve ser igual entre todas as instâncias para Server Functions funcionarem entre elas.deploymentId: habilita proteção de version skew; assets levam ?dpl=<id> e requests de navegação levam header x-deployment-id.NEXT_MANUAL_SIG_HANDLE: env var que permite tratar manualmente SIGTERM/SIGINT no shutdown (registrado via package.json, não .env).const cache = new Map()
module.exports = class CacheHandler {
constructor(options) { this.options = options }
async get(key) { return cache.get(key) }
async set(key, data, ctx) {
cache.set(key, { value: data, lastModified: Date.now(), tags: ctx.tags })
}
async revalidateTag(tags) {
tags = [tags].flat()
for (let [key, value] of cache) {
if (value.tags.some((tag) => tags.includes(tag))) cache.delete(key)
}
}
resetRequestCache() {}
}
module.exports = {
deploymentId: process.env.DEPLOYMENT_VERSION,
}
| Cache-Control | Quando aplica |
|---|---|
public, max-age=31536000, immutable | Assets com hash no nome (imagens estáticas locais) — não pode ser sobrescrito |
s-maxage: <revalidate>, stale-while-revalidate | Páginas com ISR (getStaticProps) |
private, no-cache, no-store, max-age=0, must-revalidate | Páginas dinamicamente renderizadas (App e Pages Router), incluindo Draft Mode |
NEXT_SERVER_ACTIONS_ENCRYPTION_KEY fixa: gera erro "Failed to find Server Action" porque cada build tem chave própria.generateBuildId consistente: quebra a suposição de "mesmo build sobe múltiplos containers".cacheMaxMemorySize: 0.deploymentId resolve version skew forçando hard navigation quando cliente e servidor divergem de versão.next start, não com next dev.