-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmain.mustache
More file actions
126 lines (85 loc) · 6.7 KB
/
main.mustache
File metadata and controls
126 lines (85 loc) · 6.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
# @contentstack/live-preview-utils
[](https://www.npmjs.com/package/@contentstack/live-preview-utils)
[](LICENSE)
The **Live Preview Utils** package runs on your website and establishes a communication channel between the page and Contentstack. This enables live content updates, edit controls, and Visual Editor UI within the preview surface.
This SDK is not a replacement for Contentstack delivery SDKs. Instead, it acts as a client-side bridge for preview and Visual Editor functionality.
## Where this SDK runs
The SDK works across multiple Contentstack preview and editing experiences:
- [**Live Preview**](https://www.contentstack.com/docs/content-managers/author-content/about-live-preview): Preview entries while your site loads inside the preview panel.
- [**Timeline**](https://www.contentstack.com/docs/content-managers/timeline/about-timeline): Preview how your site appears across future dates and scheduled updates using the same Live Preview setup.
- [**Visual Editor**](https://www.contentstack.com/docs/content-managers/visual-editor/about-visual-editor): Enable WYSIWYG editing with your site inside an iframe. Use [`mode: "builder"`](docs/live-preview-configs.md#mode) so “Start Editing” targets Visual Editor; the SDK still works when the same site is opened in Live Preview.
- [**Studio**](https://www.contentstack.com/docs/studio/about-studio): Use alongside Studio to structure pages from reusable components, bind CMS data, and preview changes in real time. It is designed to work with [Live Preview and Visual Editor](https://www.contentstack.com/docs/studio/live-preview-and-visual-editing-with-studio) on your connected site.
## Requirements
- **Browser:** Initialize only on the client (`window` must exist). Avoid calling `init` during server-side rendering.
- **SSR vs CSR:** Defaults assume SSR-friendly behavior. For **client-side rendering**, pass [`stackSdk`](docs/live-preview-configs.md#stacksdk) and set [`ssr: false`](docs/live-preview-configs.md#ssr) as described in the config reference.
## Installation
```bash
npm install @contentstack/live-preview-utils
```
### Load from a CDN (advanced)
Pin the version to match your app (update `{{packageVersion}}` when you upgrade):
```html
<script type="module" crossorigin="anonymous">
import ContentstackLivePreview from "https://esm.sh/@contentstack/live-preview-utils@{{packageVersion}}";
ContentstackLivePreview.init({
stackDetails: {
apiKey: "your-stack-api-key",
},
});
</script>
```
> [!TIP]
> If you initialize the SDK using this snippet, do not initialize it again in your app bundle on the same page.
## Quick start
Initialize the SDK to enable communication between your site and Contentstack:
```javascript
import ContentstackLivePreview from "@contentstack/live-preview-utils";
ContentstackLivePreview.init({
stackDetails: {
apiKey: "your-stack-api-key",
},
});
```
See the [`init` configuration properties](docs/live-preview-configs.md#initconfig-iconfig) in **[docs/live-preview-configs.md](docs/live-preview-configs.md)** for every option you can pass to `init`.
## Configuration
You can configure the SDK using the following options:
- [`enable`](docs/live-preview-configs.md#enable)
- [`ssr`](docs/live-preview-configs.md#ssr)
- [`mode`](docs/live-preview-configs.md#mode) (`preview` vs `builder`)
- [`editButton`](docs/live-preview-configs.md#editbutton)
- [`editInVisualBuilderButton`](docs/live-preview-configs.md#editinvisualbuilderbutton) (Start Editing outside Visual Editor)
- [`cleanCslpOnProduction`](docs/live-preview-configs.md#cleancslponproduction)
- [`stackDetails`](docs/live-preview-configs.md#stackdetails) ([`apiKey`](docs/live-preview-configs.md#apikey), [`environment`](docs/live-preview-configs.md#environment))
- [`clientUrlParams`](docs/live-preview-configs.md#clienturlparams) — [NA](docs/live-preview-configs.md#na-config) / [EU](docs/live-preview-configs.md#eu-config)
- [`stackSdk`](docs/live-preview-configs.md#stacksdk)
Full tables and examples: **[docs/live-preview-configs.md](docs/live-preview-configs.md)**.
### Methods and properties
- [`onLiveEdit`](docs/live-preview-configs.md#onliveeditcallback---void): Trigger actions on live edits
- [`onEntryChange`](docs/live-preview-configs.md#onentrychangecallback---void): Listen for entry updates
- [`hash`](docs/live-preview-configs.md#hash): Access preview state identifier
- [`config`](docs/live-preview-configs.md#config): Includes runtime context (for example Live Preview / Timeline preview, Visual Editor, or independent)
The [configs table of contents](docs/live-preview-configs.md#contentstack-live-preview-utils-sdk-configs) also lists `setConfigFromParams` and `getGatsbyDataFormat` for deeper workflows.
## Advanced: stripping the SDK at build time
Set `PURGE_PREVIEW_SDK` or `REACT_APP_PURGE_PREVIEW_SDK` to `"true"` during build to exclude preview code from production bundles.
**Note:** This reduces bundle size by replacing the SDK with a lightweight stub in production.
## Resources
- **Source:** [github.com/contentstack/live-preview-sdk](https://github.com/contentstack/live-preview-sdk)
- **Typed API (local):** `npm run docs`
## Documentation and learning
### Developers
- [Set up Live Preview for your website](https://www.contentstack.com/docs/developers/set-up-live-preview/set-up-live-preview-for-your-website)
- [How Live Preview works](https://www.contentstack.com/docs/developers/set-up-live-preview/how-live-preview-works)
- [Preview API](https://www.contentstack.com/docs/developers/set-up-timeline/preview-api)
- [Set up Timeline for your website](https://www.contentstack.com/docs/developers/set-up-timeline/set-up-timeline-for-your-website)
### Content managers
- [About Live Preview](https://www.contentstack.com/docs/content-managers/author-content/about-live-preview)
- [Preview content across a Timeline](https://www.contentstack.com/docs/content-managers/timeline/preview-content-across-a-timeline)
- [About Visual Editor](https://www.contentstack.com/docs/content-managers/visual-editor/about-visual-editor)
### Studio
- [Studio](https://www.contentstack.com/docs/studio)
- [Get started with Studio](https://www.contentstack.com/docs/studio/get-started-with-studio)
### Academy
- [Implementing Live Preview (course)](https://www.contentstack.com/academy/courses/implementing-live-preview)
- [Contentstack Live Preview under the hood](https://www.contentstack.com/academy/content/contentstack-live-preview-under-the-hood)
- [Understanding Timeline](https://www.contentstack.com/academy/content/understanding-timeline)
- [Understanding Visual Editor](https://www.contentstack.com/academy/content/understanding-visual-builder)