# Live preview

> Show edits on the real page before they go live: the page overlay on any site, the draft render on Next.js.

Editors see their edits on the real page before anything goes live. Other visitors see only the deployed site.

## Use the page overlay

The overlay needs no setup and works on Next.js and Astro, in dev and production. While you type, it replaces the
deployed values on the page with your drafts: text that equals a field's value, images whose `src` contains the image
path, and the page `<title>`.

- To match inside one entry only, wrap it in `data-editlet-entry="<collection>/<slug>"`.
- For exact control, mark each element that shows a field: `data-editlet-field="<collection>/<slug>#<field>"`. Once
  a page has one field mark, the overlay patches only marked elements.
- With entry marks and the editor open, select words on the page and press **Edit**. The editor opens that entry.

The overlay does not patch Markdown bodies, computed values (dates, excerpts, links), or unmarked Astro images with
hashed `/_astro/…` URLs.

On the live site the page keeps showing the branch after you close the editor. Press **Exit** on the pill at the
bottom left to see the deployed page.

## Turn on the draft render (Next.js)

The draft render shows each save on the live site before any merge. The server renders the page from the branch
commit in Next draft mode. Astro has no draft render.

1. Turn it on. It needs a known repository and `git.baseBranch` in the schema.

   ```ts
   // next.config.ts
   export default withEditlet(nextConfig, { admin: { repo: 'acme/site', preview: true } });
   ```

2. Add the route. The editor calls it with the editor's GitHub token. The route checks push access, sets a signed
   cookie and does not store the token.

   ```ts
   // app/api/editlet/preview/route.ts
   export { POST, DELETE } from 'editlet/next/preview';
   ```

3. Set the server env variables:

   | Variable | Value |
   |---|---|
   | `CMS_PREVIEW_SECRET` | A random string of at least 32 characters: `openssl rand -base64 32` |
   | `CMS_GITHUB_TOKEN` | A fine-grained token with read-only **Contents** access to the repository |

4. Pass the preview ref to every content read, in pages, layouts and lists. Call `previewRef()` outside `'use cache'`.

   ```tsx
   import { editletField, previewRef } from 'editlet/next';
   import { getEntry } from '@/lib/cms';

   const ref = await previewRef(); // { branch, sha } for a verified editor, else undefined
   const post = await getEntry('post', slug, { ref });
   return <h1 {...editletField(post, 'title')}>{post.data.title}</h1>;
   ```

A read without `{ ref }` misses entries that exist only on the branch, and on Vercel fails with
`Cannot read schema: ENOENT`. Do not set `dynamicParams = false` on content routes: a branch can add a slug.

## Limits

- In a draft render, images that exist only on the branch are broken. Mark image fields with `editletField()` so the
  overlay shows them.
- Behind a proxy that terminates TLS, forward `Host` or `X-Forwarded-Host`, and `X-Forwarded-Proto`. Otherwise the
  route answers `403 Forbidden origin`.
