Redirects messed up Netlify Functions

Hi there,
These are some of the contexts:

*Context: I am using Netlify Functions 2.0 with React (Vite) and customise some of my endpoints, for example /healthcheck. To avoid getting 404 when I refresh the SPA, I added the redirect into my netlify.toml.

[[redirects]]
from = "/*"
to = "/index.html"
status = 200

It seems to work fine with netlify dev but when I deployed, my functions would not run but instead it would return the index.html when called. Is there a correct way to fix it? Cause right now I am thinking of adding another redirect and add an extra ‘layer’ to the endpoint like api/healthcheck then for the redirect

[[redirects]]
from = "/api/*"
to = "/api/:splat" --> Please let me know if this is the correct syntax/format/way
status = 200

[[redirects]]
from = "/*"
to = "/index.html"
status = 200

Thank you for your help in advance

For anyone who is wondering, adding another redirect to point to the API works, I am just not sure if it is the ‘right’ way since I cannot find it documented anywhere yet

@bensua What you’ve done seems correct to me.

The often used SPA rule of “redirect everything that doesn’t exist to the /index.html” would absolutely trigger on any custom function routes, since they don’t exist as files.

With rules processing from top to bottom it’s fine to provide a more specific rule higher up that catches your custom API routes and prevents them falling into the SPA catch all.

1 Like