Fixing 404 Errors for _next/static in Next.js Standalone Deployments
Fixing 404 Errors for _next/static in Next.js Standalone Deployments
This is Part 4 of the series: Self-Hosting Next.js in Kubernetes (Without Vercel).
If your Next.js app works locally but returns 404 errors for
/_next/static/* after deploying to Kubernetes or OpenShift,
this post is for you.
This is the most common issue with Next.js standalone deployments.
The Symptom
After deployment, you may see:
- Blank pages
- Missing CSS or JavaScript
- 404 errors in browser dev tools for
/_next/static/*
Yet the server itself is running fine.
Why This Happens
In standalone mode, Next.js:
- Bundles the server into
server.js - Includes only required Node.js dependencies
- Does NOT automatically include static assets
That means:
.next/standaloneexists.next/staticexists- But they are not wired together
On Vercel, this is handled for you. In Kubernetes, you must do it yourself.
How Standalone Mode Resolves Paths
When running in standalone mode, the server expects assets at:
.next/standalone/dist/apps/<app-name>/.next/static
But after build, static files are actually located at:
dist/apps/<app-name>/.next/static
If you don’t copy them, the server simply can’t find them.
The Fix: Copy Static Assets
After npm run build, copy the static assets manually:
cp -R dist/apps/example-web/.next/static \
dist/apps/example-web/.next/standalone/dist/apps/example-web/.next/static
This single command resolves most /_next/static 404 errors.
Don’t Forget the Public Folder
If you use images, fonts, or files from /public,
you must copy that folder as well:
cp -R dist/apps/example-web/public \
dist/apps/example-web/.next/standalone/apps/example-web/public
Without this, routes like:
/favicon.ico/images/*
will also return 404.
Common Mistakes
- Assuming standalone includes static files automatically
- Copying assets into the wrong directory
- Using
next startinstead ofserver.js - Mounting volumes that overwrite the standalone folder
How to Verify Inside the Container
Exec into the running pod and check:
ls .next/standalone/dist/apps/example-web/.next/static
If the directory exists and contains files, your issue is resolved.
Why This Problem Rarely Appears on Vercel
Vercel hides this complexity by:
- Managing file layout internally
- Serving static assets via CDN
- Abstracting runtime paths
When you self-host, you gain control — but you also take responsibility.
Series Progress
Self-Hosting Next.js in Kubernetes (Without Vercel)
- ✅ Part 1: Standalone Deployment
- ✅ Part 2: Minimal Docker Images
- ✅ Part 3: OpenShift Deployment
- ✅ Part 4: Fixing Static Asset 404s
- ⏭ Part 5: Runtime Config & Secrets
- ⏭ Part 6: Health Checks & Scaling
Final Thoughts
If your Next.js standalone app shows 404s for static assets, the problem is almost never Kubernetes itself.
It’s a file layout issue — and once you understand it, the fix is simple and repeatable.
In the next post, we’ll cover how to manage environment variables, ConfigMaps, and Secrets witho
❤️ Support This Blog
If this post helped you, you can support my writing with a small donation. Thank you for reading.
Comments
Post a Comment