Next.js static assets incorrectly linked

I’m having some trouble fixing a next.js deploy issue and hope you might be able to help. My build and deploy are both completing successfully. The resultant deploy is not able to correctly resolve static asset routes. So requests to static assets are returning 404 errors.

For instance, static assets are requested at this path:
/_next/static/chunks/1591-d4ea8c29823ee9e5.js

But these assets are hosted at the following path:
/static/chunks/1591-d4ea8c29823ee9e5.js

How can I resolve this?

netlify.toml:

[build]
  command = "next build"
  publish = ".next"

Versions:

  • Node.js v18.20.5
  • Next.js version 13.5.8
  • Netlify Next.js Plugin version 5.9.2
  • Netlify CLI version 17.38.1 darwin-arm64 node-v18.20.5

Related issues:

Are you deploying using the command netlify deploy --build? If not, then use that.

1 Like

That worked. Thanks!

Just so I can understand: how is this different?

Also, I’d suggest adding this to the docs (Get started with Netlify CLI | Netlify Docs)? (I just lost about 4 hours thinking there was an error elsewhere in my build configuration…)

Above command builds the output and deploys it in the same command context. While I personally advise that command for all sites, it’s specifically a standard requirement at this point for Next.js sites.

For Next.js to work on Netlify, the Next.js Runtime (@netlify/plugin-nextjs) runs some steps during the build process. When builds run on Netlify, it doesn’t make any difference as to what the plugin does internally because the output is directly deployed to the CDN. However locally, things change a bit. The plugin also has to restore any filesystem changes + the Netlify config changes it made during the build process so that your setup is not disturbed. Thus, if you run:

netlify build && netlify deploy

the changes made during the build command are lost by the time the deploy starts as they’re 2 distinct commands. However, when you run the command I mentioned, the deploy then uses all the changes made by the plugin as it’s the same command context and the changes are not yet restored.

If you absolutely want to run the two commands separately, I believe it’s still possible using:

netlify build && netlify deploy --dir="./.netlify/static/"