# Previews

The Preview button in Velora's editor opens a draft on your real site, so editors see it exactly as readers will. It sends the editor to your **Preview URL** ([Connect](https://developers.velora.build/connect.md)) with a signed link:

```
https://your-site.com/api/preview?article=<article id>&exp=<unix seconds>&sig=<hex>
```

- `article` is the article's id, a UUID.
- `exp` is when the link stops working, in Unix seconds. Links last 24 hours.
- `sig` is the hex HMAC-SHA256 of the string `preview:<article id>:<exp>`, keyed with the **webhook secret**. It has no `sha256=` prefix.

No Preview button appears until the Preview URL is set.

## Check the link

Before showing anything:

1. Check `article` is a UUID and `exp` is digits.
2. Check `exp` has not passed.
3. Compute `preview:<article>:<exp>` with the webhook secret and compare it with `sig` in constant time.
4. Answer `401` if any check fails.

```ts
import { createHmac, timingSafeEqual } from "node:crypto";

export function validPreview(article: string, exp: string, sig: string, secret: string): boolean {
  if (!/^\d{1,12}$/.test(exp) || Number(exp) < Date.now() / 1000) return false;
  const expected = Buffer.from(createHmac("sha256", secret).update(`preview:${article}:${exp}`).digest("hex"));
  const given = Buffer.from(sig.toLowerCase());
  return expected.length === given.length && timingSafeEqual(expected, given);
}
```

## Read the draft

Pass the same `sig` and `exp` on to the preview route, with the read key:

```bash
curl -H "Authorization: Bearer $VELORA_READ_KEY" \
  "https://delivery.velora.build/$SITE_ID/preview/$ARTICLE_ID?sig=$SIG&exp=$EXP"
```

Velora checks the signature again and answers the draft as an `ArticleDetail`, the same shape as a published article, with an empty `related` list. An expired or wrong link answers 404. Nothing about it is written or cached anywhere.

## Show it

- Render it with your article page, so the preview is faithful.
- Never cache it, and keep it out of search engines (`noindex`).
- Show a clear banner, so nobody mistakes a draft for the published page.
- Give the preview its own route, kept for your front end (Velora keeps `/preview` by default; see [Routing](https://developers.velora.build/routing.md#addresses-kept-for-your-front-end)). If your framework has a draft mode, the preview handler can turn it on and redirect to that route, carrying `sig` and `exp` with it.
