deploymentproxycreate-react-appnetlifynetlify-cli

netlify redirect is not working with my create-react-app


I have built a real estate site using Create-react-app. One of the apis is giving me Cors errors in production. Following the documentation from netlify I set up a netlify.toml file in the root of my project. and in it I use the following rules

``

[[redirects]]
from = "/api/*"
to = "https://completecriminalchecks.com/:splat"
status = 200
force = true

my orginal request looks like this:

const response = await fetch(
    `/api/json/?apikey=6s4122z013xlvtphdfsdfxxe19&search=radius&miles=2&center=${theZip}`,
    {
      method: "GET",
      headers: {
        "Access-Control-Allow-Origin": "*"
      }
    }

I am expecting this to proxy my request to https://completecriminalchecks.com/ + /api/json/?apikey=6s4122z013xlvtfsdfxxe19&search=radius&miles=2&center=${theZip}

but when I check my network dev tools the request is made to

Request URL: https://jessehaven.netlify.app/api/json/?apikey=6s4122z013vfvffxlvtphrnuge19&search=radius&miles=2&center=18702

why isnt it proxing what i have in the to rule?


Solution

  • I suppose your api domain is https://completecriminalchecks.com/api/. If this is the case, please add /api after your url like below and it should work.

    Also, I hope you are applying two spaces before each config line after [[redirects]] line in netlify.toml file

    [[redirects]]
      from = "/api/*"
      to = "https://completecriminalchecks.com/api/:splat"
      status = 200
      force = true
    

    ============I am using below netlify.toml conf and its working for my SPA======

    # api redirect.
    [[redirects]]
      from = "/api/*"
      to = "http://X.X.X.X/api/:splat"
      status = 200
    # SPA router support.
    [[redirects]]
      from = "/*"
      to = "/index.html"
      status = 200