Capítulo 204 de 456

cacheMaxMemorySize

Core Idea

cacheMaxMemorySize sets the byte size of the in-memory cache each Next.js server instance keeps (default 50 MB), covering both the prerendered-pages/route-handler/image cache and the built-in use cache handler.

Key Concepts

  • cacheMaxMemorySize: number (bytes) in next.config.ts, default 50 * 1024 * 1024 (50 MB).
  • Two caches sized: (1) server cache for prerendered pages, route handler responses, optimized images; (2) built-in handler behind 'use cache'.
  • 0 = disable both: mimics serverless behavior where entries rarely survive between requests.
  • Custom cacheHandler: set this to 0 when adopting a custom incrementalCacheHandlerPath so reads go to your store, not a per-instance copy.
  • Custom cacheHandlers: if registered, that handler manages its own memory and this option no longer applies to it.

Code Examples

import type { NextConfig } from 'next'

const nextConfig: NextConfig = {
  cacheMaxMemorySize: 50 * 1024 * 1024, // 50 MB, the default
}

export default nextConfig
  • O que demonstra: valor default explícito para o tamanho do cache em memória.

Anti-patterns

  • Deixar cacheMaxMemorySize > 0 com custom cacheHandler externo: cria uma cópia por instância em vez de ler do store compartilhado; zere quando usar handler externo.

Key Takeaways

  1. next dev mantém seu próprio cache em memória independente desta opção; produção respeita o valor configurado exatamente.
  2. Zerar a opção é uma forma de simular ambiente serverless (sem persistência entre requests).
  3. Se um cacheHandlers custom estiver registrado, ele gerencia sua própria memória, cacheMaxMemorySize deixa de valer pra ele.

Connects To

  • cacheHandlers: handler custom que assume a gestão de memória em vez desta opção.
  • use cache: diretiva que consome o cache em memória padrão dimensionado aqui.