Configurable rewrites

I’m deploying a Vite SPA to Vercel, and my backend is deployed elsewhere. Vercel’s rewrites feature is handy to allow proxying /api/* to my backend api url, but I don’t want to commit my backend address to source control.

I’m working around this issue by writing the vercel.json as part of my Github Action:

      - name: Write vercel.json
        run: |
          cat <<EOF > vercel.json
          {
            "rewrites": [
              {
                "source": "/api/:path*",
                "destination": "${{ secrets.BACKEND_API }}/:path*"
              },
              {
                "source": "/(.*)",
                "destination": "/index.html"
              }
            ]
          }
          EOF

It would be good if this was more natively supported by Vercel

1 Like

Hi @hpx7, welcome to the Vercel Community!

I see what you mean. But, there is no other way to dynamically generate a vercel.json.

I’m curious to know why you want to keep your backend API as a secret because the frontend users will always know what the backend API url is. So, the URL itself is a public information anyways.

Please let me know if I misunderstood.

1 Like

@anshumanb the rewrites feature allows me to hide the backend API url from frontend users.

Lets say my backend API was https://example.com. With rewrites, the frontend users will see a call to /api/foo, which will be proxied to https://example.com/foo via Vercel rewrites. Does that make sense?

1 Like

Hi @hpx7, thanks for explaining your use case. But as of today, we don’t have other way to support dynamic values in the vercel.json file. So, the solution you are using is the ideal one.