# Preview mode (/docs/preview-mode)

Show drafts and pending changes on your own site before you publish them.

> **Not available yet**
>
> The API is not live and the `@tintage` packages are not published. The steps below do not work until they launch.

With preview mode, an editor presses Preview and sees the post on your site, in your design. Lists, related posts and navigation show drafts too. Your pages make the same calls as before: the client reads drafts for a visitor in preview and published content for everyone else.

## How it works

1. Preview in the editor opens your preview URL with a token and the path of the post.
2. Your preview route checks the token with the API, keeps it in an HttpOnly cookie on your domain and redirects to the path.
3. While the cookie is there, the client reads from `api.tintage.com` with `Authorization: Preview <token>`. Those answers are never cached or indexed.

A preview token reads one project and cannot write. It lasts one hour. It stops within a minute when the person who pressed Preview loses access to the project. Press Preview again for a new one. When a cookie has expired, your site shows published content.

## Add preview to your site

You need your project id, and a public key in `TINTAGE_PUBLIC_KEY`. Settings, Preview in the dashboard shows the same steps with your project id filled in, and a prompt you can give a coding agent.

### Next.js

For the App Router. The helpers use Next's draft mode.

```bash
bun add @tintage/sdk @tintage/next @tintage/react
```

`app/api/preview/route.ts`:

```ts
import { previewRoute } from "@tintage/next/cms";

export const GET = previewRoute({ project: "YOUR_PROJECT_ID" });
```

`app/blog/[slug]/page.tsx`:

```tsx
import { getClient, isPreview } from "@tintage/next/cms";
import { PostBody, PreviewBar } from "@tintage/react/cms";

const options = { project: "YOUR_PROJECT_ID", key: process.env.TINTAGE_PUBLIC_KEY! };

export default async function Page({ params }: { params: Promise<{ slug: string }> }) {
  const cms = await getClient(options);
  const post = await cms.posts.getBySlug((await params).slug);
  return (
    <article>
      <h1>{post.title}</h1>
      {post.body ? <PostBody document={post.body} /> : null}
      {(await isPreview(options)) ? <PreviewBar /> : null}
    </article>
  );
}
```

Your preview URL is your site's address followed by `/api/preview`.

### React Router

For framework mode. React Router 7 needs the v8_middleware future flag for the middleware.

```bash
bun add @tintage/sdk @tintage/react-router @tintage/react
```

`app/routes.ts`:

```ts
route("preview", "routes/preview.ts"),
```

`app/routes/preview.ts`:

```ts
import { previewLoader } from "@tintage/react-router/cms";

export const loader = previewLoader({ project: "YOUR_PROJECT_ID" });
```

`app/routes/post.tsx`:

```tsx
import { getClient, isPreview } from "@tintage/react-router/cms";
import { PostBody, PreviewBar } from "@tintage/react/cms";
import { useRevalidator } from "react-router";
import type { Route } from "./+types/post";

const options = { project: "YOUR_PROJECT_ID", key: process.env.TINTAGE_PUBLIC_KEY! };

export async function loader({ request, params }: Route.LoaderArgs) {
  const post = await getClient(request, options).posts.getBySlug(params.slug);
  return { post, preview: isPreview(request, options) };
}

export default function Post({ loaderData: { post, preview } }: Route.ComponentProps) {
  const revalidator = useRevalidator();
  return (
    <article>
      <h1>{post.title}</h1>
      {post.body ? <PostBody document={post.body} /> : null}
      {preview ? <PreviewBar previewPath="/preview" onRefresh={() => revalidator.revalidate()} /> : null}
    </article>
  );
}
```

`app/root.tsx`:

```tsx
import { previewMiddleware } from "@tintage/react-router/cms";

export const middleware = [previewMiddleware({ project: "YOUR_PROJECT_ID" })];
```

Your preview URL is your site's address followed by `/preview`.

### Astro

Preview needs pages rendered on demand: add a server adapter and set prerender = false on the pages that show posts. A fully static build cannot preview.

```bash
bun add @tintage/sdk @tintage/astro
```

`src/pages/api/preview.ts`:

```ts
import { previewRoute } from "@tintage/astro/cms";

export const prerender = false;
export const GET = previewRoute({ project: "YOUR_PROJECT_ID" });
```

`src/pages/blog/[slug].astro`:

```astro
---
import { getClient, isPreview } from "@tintage/astro/cms";

export const prerender = false;
const options = { project: "YOUR_PROJECT_ID", key: import.meta.env.TINTAGE_PUBLIC_KEY };
const post = await getClient(Astro, options).posts.getBySlug(Astro.params.slug!, { format: "html" });
const preview = isPreview(Astro, options);
---

<article>
  <h1>{post.title}</h1>
  <Fragment set:html={post.body} />
  {preview && <a href={"/api/preview?exit=1&path=" + encodeURIComponent(Astro.url.pathname)}>Exit preview</a>}
</article>
```

`src/middleware.ts`:

```ts
import { previewMiddleware } from "@tintage/astro/cms";

export const onRequest = previewMiddleware({ project: "YOUR_PROJECT_ID" });
```

Your preview URL is your site's address followed by `/api/preview`.

### Any other framework

For any server that takes a web Request and returns a Response.

```bash
bun add @tintage/sdk
```

`GET /api/preview`:

```ts
import { handlePreviewRequest } from "@tintage/sdk/cms";

export async function GET(request: Request) {
  return (await handlePreviewRequest(request, { project: "YOUR_PROJECT_ID" })).response;
}
```

`Where you read posts`:

```ts
import { createRequestClient, requestPreviewToken, withPreviewHeaders } from "@tintage/sdk/cms";

const options = { project: "YOUR_PROJECT_ID", key: process.env.TINTAGE_PUBLIC_KEY! };

export async function page(request: Request, slug: string) {
  const post = await createRequestClient(request, options).posts.getBySlug(slug, { format: "html" });
  const exit = requestPreviewToken(request, options.project) ? '<a href="/api/preview?exit=1">Exit preview</a>' : "";
  const response = new Response(post.body + exit, { headers: { "content-type": "text/html" } });
  return withPreviewHeaders(response, request, options.project);
}
```

Your preview URL is your site's address followed by `/api/preview`.

## Save the preview URL

In Settings, Preview, enter the preview URL and save. A staging URL is optional and adds Preview on staging to the editor. Owners and developers can set them.

Then press Test preview. It calls your route with a token that reads no content and tells you what is wrong:

| Message about | What to check |
| --- | --- |
| The address cannot be reached | The spelling, and that the site is deployed and public |
| A sign-in before the address | Deployment protection or a firewall in front of the route |
| No preview route | The route file, and that you deployed after adding it |
| A static file | Server rendering for the route (on Astro, `prerender = false` and an adapter) |
| Another project | The project id your site is configured with |
| An old SDK version | Update the `@tintage` packages and deploy again |
| No preview cookie | A proxy or CDN that removes `Set-Cookie` headers |

## The preview bar

`<PreviewBar />` from `@tintage/react/cms` shows "Previewing drafts" with Refresh and Exit preview. Render it only while `isPreview` is true. It takes `previewPath` (your preview route), `title`, `onRefresh`, `className` and `style`. Without React, link to your preview route with `?exit=1&path=` and the path to return to.

Preview does not update by itself while someone types. Press Refresh after the editor saves.

## Caching

Do not let a CDN or framework cache pages that carry the `tintage_preview` cookie. The middleware of each helper marks them `Cache-Control: private, no-store` and `X-Robots-Tag: noindex`. On Next.js, draft mode already renders the page for that visitor alone.

## Share links

To show a draft to someone outside your team, use Share in the editor. That link shows our rendering of one post and does not need preview mode on your site.
