---
title: "Live preview plugin"
description: "Preview unsaved CMS or editor changes directly in the browser on the real site."
canonical_url: "https://content.comark.dev/plugins/built-in/live-preview"
---
# Live preview plugin

> Preview unsaved CMS or editor changes directly in the browser on the real site.

The `livePreview` plugin lets a CMS or editor preview unsaved changes directly in the browser. It runs on a browser Content instance loaded from a [snapshot](https://content.comark.dev/sources/snapshot) with `comark-content/runtime`, rendering edits with the site's own pages and components.

An **edit** is an unsaved change to a document: updated content, a new document, or a removal. While an edit is active, `get()`, `list()`, `stat()`, `navigation()`, [`query()`](https://content.comark.dev/plugins/built-in/sql-query), and [`search()`](https://content.comark.dev/plugins/built-in/full-text-search) all reflect it. Discarding an edit restores the **base** document.

## When to use it

Use `livePreview` on an instance dedicated to a single editor, such as a browser instance. Every reader of an instance sees its edits, so don't install it on a shared server instance. For server-side previews, create a separate instance per editor with [`withRef()`](https://content.comark.dev/reference/content/with-ref) and call [`clean()`](https://content.comark.dev/reference/content/clean) when finished.

Edits are stored in memory only. Your CMS or editor is responsible for persisting them and re-applying them on page load.

## Usage

Add the plugin, then modify documents through `content.livePreview`:

```ts [content.ts]
import { comarkContent } from 'comark-content/runtime'
import snapshot from 'comark-content/sources/snapshot'
import livePreview from 'comark-content/plugins/live-preview'

export const content = comarkContent('docs', {
  source: snapshot('/api/content/snapshot/docs.json'),
  plugins: [livePreview()],
})

content.livePreview.onChange(() => refreshPage())

const intro = await content.get('/guide/intro')
await content.livePreview.write({ ...intro!, data: { ...intro!.data, title: 'Introduction' } })
await content.get('/guide/intro') // serves the edit

await content.livePreview.remove('/guide/old-page')
await content.livePreview.discard('/guide/intro') // restores the base
```

Each edit is a parsed [`ContentFile`](https://content.comark.dev/reference/types/content#contentfiletdata-tmeta), produced by a visual editor or parsed with Comark. Because the plugin does not parse raw text, it works on `comark-content/runtime` instances without parsers.

## How edits stay consistent

The plugin applies edits through [`update()`](https://content.comark.dev/reference/content/update) and [`remove()`](https://content.comark.dev/reference/content/remove), so every read method and index plugin picks them up automatically. Before the first edit to a document, the plugin saves its base copy for `discard()`.

When the underlying content changes while edits are active, the plugin reconciles them:

- **Base updated** (from HMR, a webhook, or [`refresh()`](https://content.comark.dev/reference/content/refresh)). The plugin records the new base and re-applies the edit on top.
- **Base matches the edit** (for example after saving). The edit is cleared and the instance serves the saved base.
- **Base removed.** An edit modifying that document becomes a new document; an edit removing it is cleared.

All three cases notify [`onChange`](#contentlivepreviewonchangelistener) listeners.

Operations run sequentially in call order. Listeners run after the change is visible to reads, so your UI can re-render immediately.

::note
Calling `update()` and `remove()` on a [`content.withRef()`](https://content.comark.dev/reference/content/with-ref) instance also previews changes in isolation, but it does not follow base updates. HMR updates don't reach the ref instance, and `refresh()` overwrites pending changes.
::

## Caches

Edits are stored in the instance cache, which must persist entries and stay private to the editing session:

- With `cache: false`, the plugin throws at creation.
- With a cache `ttl`, expired edits fall back to the base document.
- With a shared cache `driver`, every reader sharing that cache sees the edits.

The default in-memory cache works out of the box in the browser.

## Hubs

On a [hub](https://content.comark.dev/advanced/hub), `hub.livePreview` exposes the same methods. `write`, `remove`, and `discard` route to the instance named by the key (`blog/hello.md`) or serving the public path (`/blog/hello`). `list()` and `onChange()` span all instances that use the plugin.

```ts [hub.ts]
const hub = contentHub([docs, blog]) // both use livePreview()

await hub.livePreview.write(editedPost) // routed by editedPost.meta.key
await hub.livePreview.remove('/docs/guide/setup') // routed by public path
hub.livePreview.list() // active edits across all instances
```

`hub.livePreview` exists only when at least one instance uses the plugin. Calling a method for an instance without it throws.

## API

The plugin adds the `content.livePreview` namespace.

### `livePreview()`{lang="ts"}

Returns a [`ContentPlugin`](https://content.comark.dev/reference/types/plugins#contentplugin) that adds `content.livePreview` (and `hub.livePreview` on a [hub](#hubs)). Takes no options. Throws if the instance has `cache: false`.

### `content.livePreview.write(file)`

Applies a parsed [`ContentFile`](https://content.comark.dev/reference/types/content#contentfiletdata-tmeta) as an edit without re-parsing. `file.meta.key` identifies the document (for example `docs/guide/intro.md`). If the key does not exist yet, it adds a new document.

**Returns:** `Promise<void>`

### `content.livePreview.remove(key)`

Removes a document by key or public path. Removing a document created by an edit clears that edit.

**Returns:** `Promise<void>`

### `content.livePreview.discard(key)`

Restores the base document, or removes a document created by an edit. Accepts a key or public path, including the path of a removed document. Does nothing if the key has no active edit.

**Returns:** `Promise<void>`

### `content.livePreview.list()`

Returns all active edits on the instance.

**Returns:** `PreviewEdit[]`

### `content.livePreview.onChange(listener)`

Calls `listener` after every change, including base updates under an active edit.

**Returns:** a function that removes the listener.

The listener receives a `PreviewChange`:

```ts
interface PreviewChange {
  key: string // for example 'docs/guide/intro.md'
  edit?: PreviewEdit // undefined when no active edit remains for the key
}
```

### `PreviewEdit`

```ts
interface PreviewEdit {
  key: string // document key, for example 'docs/guide/intro.md'
  file: ContentFile | null // edited document, or null for a removal
  base: ContentFile | null // base document, or null for a new document
}
```


## Sitemap

See the full [sitemap](https://content.comark.dev/sitemap.md) for all pages.
