External Backend Service, Redirect Help

Welcome to the forums @trevorwhealy

Excellent first post!

Correct, to make React Router work you need

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

in a netlify.toml or the following in a _redirects file

/*    /index.html    200

This rewrite is a must.

Rules are processed top to bottom and _redirects rules are processed first then netlify.toml rules. Better to choose one file for redirects only.

To proxy to another service the convention is to have a rewrite

[[redirects]]
  from = "/api/*"
  to = "https://api.somewhere.com/v1/:splat"
  status = 200

Rather than using

fetch("https://api.somewhere.com/v1/users", ...)
// or
fetch("/users", ...)

as previously, all calls go via the /api e.g

fetch("/api/users", ...)
// or
fetch("/api/user/123456", ...)

This rule (and any other rules) must go above the SPA catch-all rule otherwise everything gets routed back to the SPA.

[[redirects]]
  from = "/api/*"
  to = "https://api.somewhere.com/v1/:splat"
  status = 200

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

or if you wanted to use a _redirects

/api/*   https://api.somewhere.com/v1/:splat    200
/*    /index.html    200

If you have any questions or issues, don’t hesitate to reply.

1 Like