Which netlify config files are safe to copy/overwrite in npm run build?

Hi there,
We’re deploying the same codebase to more than one netlify site.
They each rely on a different toml file to configure their build. (The biggest difference is a bunch of redirects, depending on the site.)

Given that they all receive the same code, the build command starts by copying the relevant toml to the root folder.
This works locally under netlify dev, but when running as a remote deploy on netlify, it does not. I suspect the build is using whatever toml was in the root when the build is triggered. (And ignoring the one that is copied to the root at build time)

Is there a better way to do this?
Many thanks,
George

Hi @George_A

The netlify.toml is one of the first files read as it can contain build-time variables (such as NODE_VERSION, NPM_VER) and branch/context specific information, so copying a different version of this file during build doesn’t work.

Another option available is to have different branches in the repository that have the different configurations required.

If you would prefer not maintaining multiple branches, you could have a custom build script (e.g. my-build-script.sh) that reads the URL environment variable and copy a _redirects file as part of the build to the output directory (redirects are processed post-build)

if [[ $URL eq 'some_url' ]]
  then
  npm run build && cp redirects/[site]/_redirects OUTPUT_DIRECTORY
elif [[....]]
  then
  # another version
else
  # ...
fi;

(This is not tested as is per se.)

1 Like

Ahh, thanks @coelmay that makes sense.
Re the build-script.sh, does it have to be a script or could we use node script?
Many thanks.

Certainly, you can use a node script, PHP, or any other supported language.

1 Like