Configuring Turbopack Cache for Next.js Projects
Turbopack’s caching layer operates as a deterministic state machine, persisting incremental compilation artifacts to disk or memory. In Next.js, cache configuration directly dictates build latency, memory footprint, and hot-reload fidelity. This guide covers disk versus memory allocation, Next.js integration points, cache lifecycle management, and performance tuning for incremental builds.
Understanding Turbopack’s Cache Architecture in Next.js
Turbopack stores compiled module graphs, transformed assets, and dependency metadata in .next/cache/turbopack. The directory maintains a strict mapping between source file hashes and cached transformation outputs. During incremental builds, the engine computes a content-addressable hash for each module. If the hash matches an existing cache entry, Turbopack bypasses transformation and injects the serialized AST directly.
Cache invalidation is tightly coupled to the module graph resolution pipeline. When a dependency changes, Turbopack propagates invalidation signals through the dependency tree, marking downstream nodes as stale while preserving unaffected subgraphs. For a deeper breakdown of how Turbopack tracks module dependencies and invalidates stale graphs, refer to our guide on Turbopack Incremental Compilation.
Core Configuration via next.config.mjs
Cache behavior is controlled through the experimental.turbopack namespace in next.config.mjs and supplemented by environment variables. The following configuration enforces disk-based caching with explicit directory routing and retention policies.
// next.config.mjs
export default {
experimental: {
turbopack: {
cache: {
dir: process.env.TURBOPACK_CACHE_DIR || '.next/cache/turbopack',
strategy: 'disk',
maxAge: 86400000 // 24 hours in milliseconds
}
}
}
};
Environment variables override runtime defaults:
TURBOPACK_CACHE_DIR: Absolute or relative path to the cache root.TURBOPACK_MEMORY_LIMIT: Maximum heap allocation for in-memory caching (defaults to 512MB).TURBOPACK_CACHE_STRATEGY:'disk','memory', or'hybrid'.
Diagnostic Trigger: Warning: Turbopack cache directory is not writable
Root Cause: Default cache paths frequently conflict with containerized environments, Docker volume mounts, or read-only filesystems. Unhandled fallbacks force volatile memory caching, eliminating persistence across restarts.
Resolution Steps:
- Define
TURBOPACK_CACHE_DIRin.env.localpointing to a persistent volume. - Set
experimental.turbopack.cache.strategyto'disk'innext.config.mjs. - Verify directory permissions with
chmod -R 755 <cache_dir>. - Run
next dev --turbopackand inspect.next/cache/turbopackfor.turbopackstate files.
Resolving TURBOPACK_CACHE_INVALID and Stale Build Artifacts
Cache corruption typically manifests after dependency upgrades, Node.js patch updates, or symlink topology changes. The engine detects hash divergence between the current module graph and persisted state, triggering explicit invalidation errors.
Diagnostic Triggers:
Error: TURBOPACK_CACHE_INVALID: Hash mismatch detectedError: Failed to read cache entry: ENOENT
Root Cause: Module graph hashes diverge from stored state when package-lock.json mutates, Node.js binary interfaces shift, or symlink resolution alters file paths. Turbopack rejects partial reads to prevent corrupted AST injection.
Resolution Steps:
- Stop the dev server to release file locks.
- Execute
rm -rf .next/cache/turbopack/*to clear stale state. - Run
next dev --turbopackto trigger a full cold rebuild. - Verify successful cache regeneration via console output:
[Turbopack] Cache initialized successfully. - Implement a pre-commit hook to invalidate cache on
package.jsonchanges.
# .husky/pre-commit
#!/bin/sh
if git diff --cached --name-only | grep -qE 'package\.json|package-lock\.json'; then
echo "Dependency manifest changed. Invalidating Turbopack cache..."
rm -rf .next/cache/turbopack/*
fi
Optimizing Cache Performance for CI/CD Pipelines
CI environments lack persistent local storage. Without explicit cache restoration, every pipeline execution performs a cold build, negating Turbopack’s incremental advantages and increasing build duration. Remote caching and deterministic key generation are required to maintain pipeline velocity.
Diagnostic Trigger: Error: Cache restore failed: checksum mismatch
Root Cause: Inconsistent cache keys or corrupted archive uploads cause checksum validation failures during restoration. The pipeline falls back to cold compilation.
Resolution Steps:
- Configure GitHub Actions
cacheaction targeting.next/cache/turbopack. - Use
package-lock.jsonhash for cache key generation to ensure dependency parity. - Add fallback
restore-keysfor partial cache hits during lockfile updates. - Validate cache hit rate via CI logs and adjust TTL if stale artifacts persist.
# .github/workflows/build.yml
- name: Cache Turbopack
uses: actions/cache@v3
with:
path: .next/cache/turbopack
key: ${{ runner.os }}-turbopack-${{ hashFiles('**/package-lock.json') }}
restore-keys: |
${{ runner.os }}-turbopack-
When aligning cache retention policies across multiple bundlers, consult the broader esbuild & Turbopack Workflows documentation for cross-toolchain compatibility patterns.
Troubleshooting Common Cache Misconfigurations
Cross-platform development introduces path normalization discrepancies, permission boundaries, and symlink resolution failures. Turbopack’s strict hashing algorithm rejects mismatched metadata, resulting in silent cache misses or explicit permission denials.
Diagnostic Triggers:
Error: EACCES: permission denied on cache dirError: Cache hash mismatch across environments
Root Cause: macOS/Linux/Windows filesystem differences alter path casing, line endings, or symlink targets. Turbopack computes different hashes for identical logical files, breaking cache portability.
Resolution Steps:
- Standardize cache paths using absolute paths or
path.resolve()in configuration. - Run
ls -la .next/cache/turbopackto verify ownership matches the executing user. - Disable symlink following in cache config if
ENOTDIRerrors occur during traversal. - Use
TURBOPACK_LOG=debugto trace cache read/write operations and isolate hash collisions.
# Enable verbose cache diagnostics
TURBOPACK_LOG=debug next dev --turbopack