Why does nuxt not process netlify.toml file

The docs for nuxt state that a _headers file of _redirects file can be placed in the public dir, but I cannot use a netlify.toml file in the public dir. I have also tried placing the netlify.toml at the root and no luck. I don’t know why this is happening, it’s probably part of the build step coming from nitro or vite but I’m not sure.

When dealing with long redirects files or headers, it would be a lot nicer to use a toml file. After running nppm run build, a dist directory is generated and if I look at the _headers or _redirects I can see that they are not processed when a netlify.toml file is used.

@dbzx10299 See the main Build Configuration documentation:
https://docs.netlify.com/configure-builds/overview/#definitions

As I understand it, the netlify.toml loads from the Package directory, which defaults to the Base directory, which defaults to the root of your repository.

The Rule processing order is outlined here:
https://docs.netlify.com/routing/redirects/#rule-processing-order

@nathanmartin I did take a look at the docs. If I put a _redirects file in the public folder with the following

/foo   /bar   301!

and run npm run build I can look at the generated dist folder and look there in the _redirects file. It has the following contents

/foo  /bar  301!
/sitemap.xml	/.netlify/builders/server 200
/__nuxt_error	/.netlify/functions/server 200
/* /.netlify/functions/server 200

If however, I use a netlify.toml file in the root and add the following

[[redirects]]
  from = "/foo"
  to = "/bar"
  status = 301
  force = true

after running the npm run build command the dist folder contains the following content in its _redirects file

/sitemap.xml	/.netlify/builders/server 200
/__nuxt_error	/.netlify/functions/server 200
/* /.netlify/functions/server 200

It is not included here anymore. But when I look at the netlify deploy under the deploys tab, I see there in a netlify.toml at the root and it contains the code that I wrote. It just isn’t being applied. One other thing to note is that the redirect only works if I use a netlify preset for nitro, and if I use netlify_edge this will not work even if I use a _redirects file in the public folder

@dbzx10299 I’d imagine you cannot use the netlify.toml for your redirects due to the system that you’re working with.

If you factor in the Rule processing order that I linked to:

Rules in the _redirects file are always processed first, followed by rules in the Netlify configuration file.

Then consider that what you’re working with (nuxt) is clearly appending its own rules to the bottom of a _redirects file.

Nothing about Netlify’s systems moves rules between _redirects and netlify.toml, they’re two separate files that are processed in the order as above, first one, then the other.

Also:

The redirects engine will process the first matching rule it finds, reading from top to bottom.

Situation #1

When you’re putting your rule in the _redirects, it’s being output by nuxt as:

_redirects
----------
/foo  /bar  301!
/sitemap.xml	/.netlify/builders/server 200
/__nuxt_error	/.netlify/functions/server 200
/* /.netlify/functions/server 200

Accessing /foo will redirect to /bar, as the rule has high priority it is easily found.

Situation #2

When you have the same rule in the netlify.toml, factoring in processing you effectively end up with:

_redirects
----------
/sitemap.xml	/.netlify/builders/server 200
/__nuxt_error	/.netlify/functions/server 200
/* /.netlify/functions/server 200

netlify.toml
----------
/foo  /bar  301!

The rules automatically appended by nuxt now have a higher priority than the one in your netlify.toml

Accessing /foo would be caught by /* /.netlify/functions/server 200

Your /foo /bar 301! is never reached.

If you wanted to have all the rules in your netlify.toml you would need to ensure they were all in their appropriate place in the netlify.toml and nothing was producing the catch all rewrite in the _redirects.

That‘s a bit of a tough one, I see what you mean. I will have to dog into this deeper, either nitro is producing this or vite somehow

Do you know what might be causing the redirect not to work even though a _redirect file is used when using the nitro netlify_edge preset?

@dbzx10299 No, because I don’t even know what nitro is, I suppose you could try asking them?

Note: I don’t work for Netlify.