# How to connect a Vercel preview to the right backend PR

Gregale · Updated 2026-11-30

Pair a Next.js preview on Vercel with the same GitHub PR and commit on Gregale, and reject stale or missing FastAPI previews before writing test data.

A Vercel frontend preview doesn't know which backend belongs to its pull request. If you set its API URL to production, a reviewer can open a preview and still write to real data. Keep the frontend on Vercel, connect the backend on Gregale to the same GitHub repository, and let a server-side route match both the **PR number** and **full commit SHA** before the backend performs a write.

[Get the runnable example on GitHub](https://github.com/gregale-dev/vercel-backend-pr-preview).

This walkthrough is for a single GitHub repository with a Next.js frontend and FastAPI backend. [Download the runnable example](https://gregale.dev/examples/vercel-backend-pr-preview.zip). Its only write is one fixed row in a temporary, in-memory SQLite database. No real orders, credentials, or saved data are involved.

## 1. Set up two previews from the same repo

Use a disposable repository and test app. Commit the example files at the repository root. In Vercel, import that repository and set **Root Directory** to `frontend`. The Git-connected project uses the [Vercel system variables](https://vercel.com/docs/environment-variables/system-environment-variables) at runtime; enable their automatic exposure if you previously disabled it.

In Gregale, create a disposable app from the same GitHub repository, using the repository root for the FastAPI service. The repository's default branch is the parent app's deploy branch in this example. [Connect and bind the GitHub repository](https://gregale.dev/docs/deploy-from-github.md#deploy-from-github), with PR previews enabled. On Vercel, set `GREGALE_PREVIEW_PARENT_SLUG` to this app's slug under **Preview** only, then deploy a new preview. Don't add a production API URL to the demo.

Open a same-repository PR into the default branch. Use the deployment URL for the PR commit, not a branch alias that moves as you push. If Vercel built the branch before the PR existed, the PR number may be blank in that deployment. Push a new commit after opening the PR, and open the new deployment.

The Gregale preview inherits the parent app's ingress gates. For this credential-free example, if the **disposable synthetic-only parent** isn't already reachable without credentials, open its ingress using the [Gregale CLI](https://gregale.dev/docs/quickstart.md). Never run this against a real backend:

```bash
APP="your-disposable-demo-app-slug"
gregale app update "$APP" --visibility public --public-auth open --no-require-authn
```

Before running the demo, check that the `gregale-preview` GitHub check is green for the same full SHA. Copy the actual preview origin from the Gregale PR comment and verify it returns `{"status":"ok"}` at `/healthz` without credentials:

```bash
PREVIEW_URL="https://THE_PREVIEW_HOST_FROM_THE_PR_COMMENT"
curl -fsS "${PREVIEW_URL}/healthz"
```

Check that the preview origin matches the URL shape below rather than inferring it from the parent app's domain. [Gregale's preview docs](https://gregale.dev/docs/preview-environments.md) explain the URL, gates, quotas and lifecycle.

## 2. Resolve the backend on the server and reject mismatches

The browser posts to its own Next.js route at `/api/preview-check`. The route runs only when `VERCEL_ENV=preview` and both Git values are present and valid. It builds one URL from the server-configured app slug and the PR number:

```text
https://pr-<VERCEL_GIT_PULL_REQUEST_ID>-<GREGALE_PREVIEW_PARENT_SLUG>.gregale.dev/preview-check
```

The route sends `X-Expected-PR` and `X-Expected-Commit` from Vercel's deployment environment. It doesn't use a URL from the browser, doesn't follow redirects, and never retries against a production host. No browser CORS rule is needed for this route because the cross-host request is server to server.

The FastAPI handler checks `GREGALE_PREVIEW=1`, `GREGALE_PR_NUMBER` and the instance's [serving `FAAS_COMMIT_SHA`](https://gregale.dev/docs/trace-propagation.md#deployment-identity) **in the request that does the write**. If they don't match, it returns `409` before opening SQLite. A separate health check isn't enough: the stable backend PR URL could change revisions between a check and a later write. The frontend validates the receipt and shows a visible `503` if the backend is missing, stale, unreachable, or otherwise fails the check.

The pairing check is **not authentication**. For a real app, protect the preview frontend and require authorized access at the backend. Use a separate datastore with least-privilege test credentials and synthetic fixtures; keep production database credentials, payment secrets and webhook targets out of both preview environments. A separate URL does not isolate data, and [Gregale doesn't automatically provision a database or bucket per PR](https://gregale.dev/docs/preview-environments.md#internal-service-calls-from-a-preview).

## 3. Verify a match and a failure

1. Open the Vercel deployment URL for the PR commit and click **Run synthetic write**. The receipt must show the PR number, the full SHA from the GitHub PR, `synthetic-memory` and `preview-demo`.
2. Make a small edit to the backend source and push it on the same PR. Wait for both previews on the new SHA. The new Vercel deployment should return the new SHA. Reopen the _old_ immutable Vercel deployment URL and run the check again. Expect `503` and **Backend preview unavailable or on a different commit**, since Gregale's stable PR URL now serves the new commit.
3. For a missing-backend check, use a valid but unused slug in the Vercel test branch's Preview variable and create a new preview deployment. It should show the same `503`, without a fallback. Restore the branch variable after the test. Closing a PR isn't an instant missing-backend test because Gregale can keep it reachable during a grace period.

If frontend-only or ignored-path changes skip the Gregale rebuild, the exact-match recipe intentionally refuses to pair the two deployments. Include a backend source change in the demo's verification push. If you use separate repos, their SHAs will legitimately differ; use an explicit, reviewed mapping from the frontend deployment to the intended backend commit instead of comparing unrelated SHAs.

For a single dedicated staging URL rather than a backend per PR, see [Deploy FastAPI with Next.js on Vercel](https://gregale.dev/articles/deploy-fastapi-backend-nextjs-vercel).

## Questions

### How do I set a different backend URL for each Vercel preview?

In a GitHub-connected monorepo, a server-side Next.js route can build the Gregale preview URL from the deployment’s pull request number and a fixed backend app slug. Verify the serving commit before a write.

### Does a Vercel preview automatically get a separate backend database?

No. A separate frontend or backend URL does not isolate data. Use an independently configured test datastore and test credentials, or a disposable fixture that never touches an external database.

Canonical URL: https://gregale.dev/articles/connect-vercel-preview-to-backend-pr

