|
1 | | -# Contentstack Live Preview Utils SDK |
| 1 | +# @contentstack/live-preview-utils |
2 | 2 |
|
3 | | -Contentstack is a headless CMS with an API-first approach. It is a CMS that developers can use to build powerful cross-platform applications in their favorite languages. Build your application frontend, and Contentstack will take care of the rest. |
| 3 | +[](https://www.npmjs.com/package/@contentstack/live-preview-utils) |
| 4 | +[](LICENSE) |
4 | 5 |
|
5 | | -Contentstack provides the Live Preview Utils SDK to establish a communication channel between the various Contentstack SDKs and your website, transmitting live changes to the preview pane. [Read More](https://www.contentstack.com/docs/content-managers/live-preview/). |
| 6 | +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. |
6 | 7 |
|
7 | | -# Installation |
| 8 | +This SDK is not a replacement for Contentstack delivery SDKs. Instead, it acts as a client-side bridge for preview and Visual Editor functionality. |
8 | 9 |
|
9 | | -To install the package via npm, use the following command: |
| 10 | +## Where this SDK runs |
| 11 | + |
| 12 | +The SDK works across multiple Contentstack preview and editing experiences: |
| 13 | + |
| 14 | +- [**Live Preview**](https://www.contentstack.com/docs/content-managers/author-content/about-live-preview): Preview entries while your site loads inside the preview panel. |
| 15 | +- [**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. |
| 16 | +- [**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. |
| 17 | +- [**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. |
| 18 | + |
| 19 | +## Requirements |
| 20 | + |
| 21 | +- **Browser:** Initialize only on the client (`window` must exist). Avoid calling `init` during server-side rendering. |
| 22 | +- **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. |
| 23 | + |
| 24 | +## Installation |
10 | 25 |
|
11 | 26 | ```bash |
12 | 27 | npm install @contentstack/live-preview-utils |
13 | 28 | ``` |
14 | 29 |
|
15 | | -Alternatively, if you want to include the package directly in your website HTML code, use the following command: |
| 30 | +### Load from a CDN (advanced) |
| 31 | + |
| 32 | +Pin the version to match your app (update `{{packageVersion}}` when you upgrade): |
16 | 33 |
|
17 | 34 | ```html |
18 | | -<script type='module' crossorigin="anonymous"> |
19 | | - import ContentstackLivePreview from 'https://esm.sh/@contentstack/live-preview-utils@{{packageVersion}}'; |
20 | | -
|
21 | | - ContentstackLivePreview.init({ |
22 | | - stackDetails: { |
23 | | - apiKey: "your-stack-api-key", |
24 | | - }, |
25 | | - }); |
| 35 | +<script type="module" crossorigin="anonymous"> |
| 36 | + import ContentstackLivePreview from "https://esm.sh/@contentstack/live-preview-utils@{{packageVersion}}"; |
| 37 | +
|
| 38 | + ContentstackLivePreview.init({ |
| 39 | + stackDetails: { |
| 40 | + apiKey: "your-stack-api-key", |
| 41 | + }, |
| 42 | + }); |
26 | 43 | </script> |
27 | 44 | ``` |
28 | | -> [!NOTE] |
29 | | -> This step involves incorporating the package into your HTML code and initializing it, eliminating the need for re-initialization in the subsequent step. |
30 | 45 |
|
| 46 | +> [!TIP] |
| 47 | +> If you initialize the SDK using this snippet, do not initialize it again in your app bundle on the same page. |
31 | 48 |
|
32 | | -# Initializing the SDK |
| 49 | +## Quick start |
33 | 50 |
|
34 | | -### Live Preview Utils |
35 | | - |
36 | | -Since the Live Preview Utils SDK is responsible for communication, you need to only initialize it. |
37 | | -Use the following command to initialize the SDK: |
| 51 | +Initialize the SDK to enable communication between your site and Contentstack: |
38 | 52 |
|
39 | 53 | ```javascript |
40 | 54 | import ContentstackLivePreview from "@contentstack/live-preview-utils"; |
41 | 55 |
|
42 | 56 | ContentstackLivePreview.init({ |
43 | | - stackDetails: { |
44 | | - apiKey: "your-stack-api-key", |
45 | | - }, |
| 57 | + stackDetails: { |
| 58 | + apiKey: "your-stack-api-key", |
| 59 | + }, |
46 | 60 | }); |
47 | 61 | ``` |
48 | 62 |
|
| 63 | +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`. |
| 64 | + |
| 65 | +## Configuration |
| 66 | + |
| 67 | +You can configure the SDK using the following options: |
| 68 | + |
| 69 | +- [`enable`](docs/live-preview-configs.md#enable) |
| 70 | +- [`ssr`](docs/live-preview-configs.md#ssr) |
| 71 | +- [`mode`](docs/live-preview-configs.md#mode) (`preview` vs `builder`) |
| 72 | +- [`editButton`](docs/live-preview-configs.md#editbutton) |
| 73 | +- [`editInVisualBuilderButton`](docs/live-preview-configs.md#editinvisualbuilderbutton) (Start Editing outside Visual Editor) |
| 74 | +- [`cleanCslpOnProduction`](docs/live-preview-configs.md#cleancslponproduction) |
| 75 | +- [`stackDetails`](docs/live-preview-configs.md#stackdetails) ([`apiKey`](docs/live-preview-configs.md#apikey), [`environment`](docs/live-preview-configs.md#environment)) |
| 76 | +- [`clientUrlParams`](docs/live-preview-configs.md#clienturlparams) — [NA](docs/live-preview-configs.md#na-config) / [EU](docs/live-preview-configs.md#eu-config) |
| 77 | +- [`stackSdk`](docs/live-preview-configs.md#stacksdk) |
| 78 | + |
| 79 | +Full tables and examples: **[docs/live-preview-configs.md](docs/live-preview-configs.md)**. |
| 80 | + |
| 81 | +### Methods and properties |
| 82 | + |
| 83 | +- [`onLiveEdit`](docs/live-preview-configs.md#onliveeditcallback---void): Trigger actions on live edits |
| 84 | +- [`onEntryChange`](docs/live-preview-configs.md#onentrychangecallback---void): Listen for entry updates |
| 85 | +- [`hash`](docs/live-preview-configs.md#hash): Access preview state identifier |
| 86 | +- [`config`](docs/live-preview-configs.md#config): Includes runtime context (for example Live Preview / Timeline preview, Visual Editor, or independent) |
| 87 | + |
| 88 | +The [configs table of contents](docs/live-preview-configs.md#contentstack-live-preview-utils-sdk-configs) also lists `setConfigFromParams` and `getGatsbyDataFormat` for deeper workflows. |
| 89 | + |
| 90 | +## Advanced: stripping the SDK at build time |
| 91 | + |
| 92 | +Set `PURGE_PREVIEW_SDK` or `REACT_APP_PURGE_PREVIEW_SDK` to `"true"` during build to exclude preview code from production bundles. |
| 93 | + |
| 94 | +**Note:** This reduces bundle size by replacing the SDK with a lightweight stub in production. |
| 95 | + |
| 96 | +## Resources |
| 97 | + |
| 98 | +- **Source:** [github.com/contentstack/live-preview-sdk](https://github.com/contentstack/live-preview-sdk) |
| 99 | +- **Typed API (local):** `npm run docs` |
| 100 | + |
| 101 | +## Documentation and learning |
| 102 | + |
| 103 | +### Developers |
| 104 | + |
| 105 | +- [Set up Live Preview for your website](https://www.contentstack.com/docs/developers/set-up-live-preview/set-up-live-preview-for-your-website) |
| 106 | +- [How Live Preview works](https://www.contentstack.com/docs/developers/set-up-live-preview/how-live-preview-works) |
| 107 | +- [Preview API](https://www.contentstack.com/docs/developers/set-up-timeline/preview-api) |
| 108 | +- [Set up Timeline for your website](https://www.contentstack.com/docs/developers/set-up-timeline/set-up-timeline-for-your-website) |
49 | 109 |
|
50 | | -# License |
| 110 | +### Content managers |
51 | 111 |
|
52 | | -MIT License |
| 112 | +- [About Live Preview](https://www.contentstack.com/docs/content-managers/author-content/about-live-preview) |
| 113 | +- [Preview content across a Timeline](https://www.contentstack.com/docs/content-managers/timeline/preview-content-across-a-timeline) |
| 114 | +- [About Visual Editor](https://www.contentstack.com/docs/content-managers/visual-editor/about-visual-editor) |
53 | 115 |
|
54 | | -Copyright © 2021-{{currentYear}} [Contentstack](https://www.contentstack.com/). All Rights Reserved |
| 116 | +### Studio |
55 | 117 |
|
56 | | -Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: |
| 118 | +- [Studio](https://www.contentstack.com/docs/studio) |
| 119 | +- [Get started with Studio](https://www.contentstack.com/docs/studio/get-started-with-studio) |
57 | 120 |
|
58 | | -The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. |
| 121 | +### Academy |
59 | 122 |
|
60 | | -THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
| 123 | +- [Implementing Live Preview (course)](https://www.contentstack.com/academy/courses/implementing-live-preview) |
| 124 | +- [Contentstack Live Preview under the hood](https://www.contentstack.com/academy/content/contentstack-live-preview-under-the-hood) |
| 125 | +- [Understanding Timeline](https://www.contentstack.com/academy/content/understanding-timeline) |
| 126 | +- [Understanding Visual Editor](https://www.contentstack.com/academy/content/understanding-visual-builder) |
0 commit comments