Netlify.toml redirects overwritten by nextjs

Hi,
I have this nextjs site preview-testing.netlify.app that I’m deploying as a nextjs preview. I generate the netlify.toml previous to the build with a script, using Environment variables to point my redirects to different environments (QA, UAT) . That works fine as you can see on the build log.


However, when the site is deployed the netlify.toml file is overwritten by nextjs and all the custom redirects are missing and the images are not found as you can see here Guide Dogs | The Guide Dogs for the Blind Association (65c50a0fbc8a370008cd0281–preview-testing.netlify.app).

Can you provide a solution for this issue?
Thanks

netlify.toml is supposed to remain static during a build. Dynamically building it is not supported. The Next.js Runtime takes into account any redirects configured for your site (as long as it’s a static netlify.toml - it can’t do so for a dynamic netlify.toml) and only adds its own redirects after your redirects (so they have a lower priority).

This is to say, this would have worked fine in a typical setup where the netlify.toml doesn’t change during the build. For your case, you might be looking for: Custom headers | Netlify Docs (that doc is for headers, but the same should be possible for redirects as well).

Thanks for your answer, sorry if I don’t understand what you mean.
The “npm run preview” command executes a script that generates the netlify.toml file with the redirects we need before the build starts, so the file should be already there when nextjs runs. Actually, if I upload a generated netlify.toml file, nextjs only adds its own stuff and behaves as you said, but obviously only works for the environment it was created for.

The file needs to exist before you start the entire Netlify build process, not just Next.js. So if your process is something like:

  • start netlify build
  • npm run preview
  • generate netlify.toml
  • next build

This is expected to not work.

The only way this would work:

  • npm run preview
  • generate netlify.toml
  • start netlify build
  • next build