next.js: All static assets (js/css/media) in Standalone mode become 404
Verify canary release
- I verified that the issue exists in the latest Next.js canary release
Provide environment information
Operating System:
Platform: win32
Arch: x64
Version: Windows 10 Enterprise
Binaries:
Node: 18.12.1
npm: N/A
Yarn: N/A
pnpm: N/A
Relevant packages:
next: 13.4.1-canary.1
eslint-config-next: 13.2.4
react: 18.2.0
react-dom: 18.2.0
Which area(s) of Next.js are affected? (leave empty if unsure)
Standalone mode (output: “standalone”)
Link to the code that reproduces this issue
asdf
To Reproduce
next.config.js
const { i18n } = require('./next-i18next.config')
/** @type {import('next').NextConfig} */
const nextConfig = {
// assetPrefix: ".", // https://github.com/vercel/next.js/issues/8158#issuecomment-687707467
output: 'standalone', // https://developers.redhat.com/articles/2022/11/23/how-deploy-nextjs-applications-red-hat-openshift#building_a_container_to_run_your_next_js_application
reactStrictMode: true,
productionBrowserSourceMaps: true,
i18n,
async redirects() {
return [
{
source: '/da-en/:slug*',
destination: '/en/:slug*',
permanent: true,
},
{
source: '/fi-en/:slug*',
destination: '/en/:slug*',
permanent: true,
},
{
source: '/fi-sv/:slug*',
destination: '/sv/:slug*',
permanent: true,
},
{
source: '/no-en/:slug*',
destination: '/en/:slug*',
permanent: true,
},
{
source: '/sv-en/:slug*',
destination: '/en/:slug*',
permanent: true,
},
]
},
}
module.exports = nextConfig
Run: npm run build
Run: node .next/standalone/server.js
Open http://localhost:3000
Describe the Bug
All js and css files become 404
p://localhost:3000/_next/static/css/36abb935a2bfb1bb.css net::ERR_ABORTED 404 (Not Found) p://localhost:3000/favicon.ico 404 (Not Found) p://localhost:3000/_next/static/chunks/main-764a718264343ae9.js net::ERR_ABORTED 404 (Not Found) etc
Expected Behavior
It can find and load all js/css files
Which browser are you using? (if relevant)
Latest Chrome
How are you deploying your application? (if relevant)
IISNode
About this issue
- Original URL
- State: closed
- Created 4 years ago
- Reactions: 13
- Comments: 32 (2 by maintainers)
I faced this issue today, I just found a workaround for this: copy .next/static to .next/standalone/.next/static
The script that works
go to standalone folder then when run: node server.js
Sharing the script I had to write to make this work (
nextversion 13, output directory is calleddist):This.
The bug
There is actually a bug in the documentation: https://nextjs.org/docs/app/api-reference/next-config-js/output
It should be noted here that
.nextdirectory is actually a default value ofdistDiranddistDiris used in other places during generation of directories.This is crucial when changing
distDiras one must take into account copying static files to a different directory in external scripts. It shouldn’t work in a way that forces a developer to remember to change his external scripts everytime he changes NextJS config, notablydistDir.Proposed solution
To mitigate this issue I propose to hardcode third level dist to .next, that is to always have:
Reasoning
I know that top level dist may be important when one wants to deploy to different providers, but third level dist in the standalone directory is just an internal directory for
server.js.Confusingly, it seems you need to write your own script copy these assets over to the build folder yourself. An opt-in that would allow the Next build to do this for you would be nice if a CDN wasn’t configured to serve these assets
I am on self-hosting, not using vercel service. I faced this issue today. Fortunately, it turned out that the _next/static/media folder was ignored by git. So, just excluded that in the .gitignore file and it’s working like charm.
I’m also facing this issue with v14.0.4
Copying the assets manually works, yeah.
I just don’t think everyone will want to host them on CDNs. Could we perhaps get a config option that controls whether or not these assets are copied to the standalone folder?
I am having the exact same issue
Ok i found it: https://github.com/vercel/next.js/blob/fb73c3a9d207105d8ffc70401799265e28ec0083/packages/next/src/server/lib/router-utils/filesystem.ts#L135
IF you specify a custom
distDirin thenext.config.jsfile, the static file must be placed there too.So in my case with a configuration like this:
The working paths must be this:
I lost 4 hours of my life for this.
Hi everyone—
The static files are
404ing because withoutput: 'standalone', we automatically create a standalone folder that copies only the necessary files for a production deployment including select files innode_modules.The minimal server does not copy the public or .next/static folders by default as these should ideally be handled by a CDN instead, although these folders can be copied to the standalone/public and standalone/.next/static folders manually, after which server.js file will serve these automatically.
To copy these and then start this local minimal server locally, you can do something like this:
I have also just created a PR to update our documentation with this → https://github.com/vercel/next.js/pull/72432.
I see that other folks have already figured out solutions for this. I will closing this PR since this is a non-issue!
Hi I have stumbled on this problem myself and my app
I am deploying in an Azure container and it works fine (everything loads), but if I try to access my app from a Terraform/ingress domain, I get all 404s
e.g if the domain is
mydomain.com/I get 404s formydomain.com/_nextI have tried the Dockerfiles here but no luck… Does anyone have any suggestions? I am not using a custom distDir.
EDIT: For anyone testing the solutions above and not getting anywhere and using Ingress/Kube to direct to their app through another domain: check if your requests are being made to
proxyUrl/_nextinstead ofdeploymentUrl/_next. If the former, there’s your culprit and you will need to setup a custom server that serves the static assets off your deployment urlI’m still having that issue: https://github.com/hendisantika/nextjs-mysql-product-crud
https://products.hendisantika.my.id/products
Did you find a way to get rid of this problem ?
感谢非常有用
Looking at the docs it seems that this is intended behavior:
“ This minimal server does not copy the public or .next/static folders by default as these should ideally be handled by a CDN instead, although these folders can be copied to the standalone/public and standalone/.next/static folders manually, after which server.js file will serve these automatically.”
https://nextjs.org/docs/pages/api-reference/next-config-js/output
same issue here