Railway/NIXPACKS automatically caches the following directories via Docker cache mounts:
- npm cache (
/root/.npm) - Cached between builds - Next.js build cache (
.next/cache) - Cached between builds - node_modules cache (
node_modules/.cache) - Cached between builds
You can see these in your build logs:
--mount=type=cache,id=.../root/npm,target=/root/.npm
--mount=type=cache,id=...next/cache,target=/app/.next/cache
--mount=type=cache,id=...node_modules/cache,target=/app/node_modules/.cache
- Faster, reproducible installs
- Uses
package-lock.jsonfor exact versions - Better cache utilization
- Ensures consistent dependency resolution
- Enables better npm cache hits
- Dependencies installed before Prisma generation
- Prisma generation before Next.js build
- Each step can leverage cache from previous builds
- Reduces Docker image size
- Faster Docker export/import phase
Railway uses Docker layer caching and build cache mounts:
-
Docker Layer Caching: Each step in the Dockerfile is cached as a layer
- If
package.jsonandpackage-lock.jsondon't change → npm install layer is reused - If source code doesn't change → build layer is reused
- If
-
Build Cache Mounts: Specific directories are cached between builds
- npm cache persists between builds
- Next.js cache persists between builds
- Only invalidated when dependencies change
Cache is automatically invalidated when:
- ✅
package.jsonorpackage-lock.jsonchanges → npm install runs fresh - ✅ Source code changes → Next.js build runs fresh
- ✅ Prisma schema changes → Prisma generate runs fresh
Cache is preserved when:
- ✅ Only source code changes (not dependencies)
- ✅ Only environment variables change
- ✅ Only configuration files change (that don't affect dependencies)
- npm install: ~40 seconds
- Prisma generate: ~3 seconds
- Next.js build: ~70 seconds
- Total: ~3-4 minutes
- npm install: ~5-10 seconds (if dependencies unchanged)
- Prisma generate: ~2 seconds (cached)
- Next.js build: ~30-40 seconds (with cache)
- Total: ~1-2 minutes (50-60% faster!)
- ✅ Removed redundant Prisma generation
- ✅ Removed
prisma db pushfrom start command - ✅ Using
npm cifor faster installs - ✅ Standalone output for smaller Docker images
- ✅ Metal build environment (faster Docker operations)
- Dockerfile optimization - Create custom Dockerfile with better layer caching
- Dependency optimization - Review and remove unused dependencies
- Build parallelization - Split build steps if possible
Check your Railway build logs for:
npm citime (should be faster on subsequent builds)- Next.js build time (should be faster with cache)
- Docker layer reuse messages
If builds seem slow:
- Check if
package-lock.jsonis committed - Verify
npm ciis being used (notnpm install) - Check build logs for cache mount usage
- Ensure Metal build environment is enabled
Your caching is already optimized! Railway automatically handles:
- ✅ npm cache
- ✅ Next.js build cache
- ✅ Docker layer caching
The optimizations we've made ensure:
- ✅ Faster dependency installation (
npm ci) - ✅ Better cache utilization (proper build order)
- ✅ Smaller Docker images (standalone output)
- ✅ Faster Docker operations (Metal build environment)
Expected improvement: Subsequent builds should be 50-60% faster than the first build!