@contentstack/live-preview-utils

中文文档
4.6.1发布于 2026/11/06MITTypeScriptESM + CJS构建来源已验证近 30 天下载 14.7万

Contentstack provides the Live Preview SDK to establish a communication channel between the various Contentstack SDKs and your website, transmitting live changes to the preview pane.

原始使用文档
文档目录14 节
  1. Where this SDK runs
  2. Requirements
  3. Installation
  4. Load from a CDN (advanced)
  5. Quick start
  6. Configuration
  7. Methods and properties
  8. Advanced: stripping the SDK at build time
  9. Resources
  10. Documentation and learning
  11. Developers
  12. Content managers
  13. Studio
  14. Academy

@contentstack/live-preview-utils

npm License: MIT

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: Preview entries while your site loads inside the preview panel.
  • Timeline: Preview how your site appears across future dates and scheduled updates using the same Live Preview setup.
  • Visual Editor: Enable WYSIWYG editing with your site inside an iframe. Use mode: "builder" so “Start Editing” targets Visual Editor; the SDK still works when the same site is opened in Live Preview.
  • 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 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 and set ssr: false as described in the config reference.

Installation

npm install @contentstack/live-preview-utils

Load from a CDN (advanced)

Pin the version to match your app (update 4.6.1 when you upgrade):

<script type="module" crossorigin="anonymous">
  import ContentstackLivePreview from "https://esm.sh/@contentstack/[email protected]";

  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:

import ContentstackLivePreview from "@contentstack/live-preview-utils";

ContentstackLivePreview.init({
  stackDetails: {
    apiKey: "your-stack-api-key",
  },
});

See the init configuration properties in docs/live-preview-configs.md for every option you can pass to init.

Configuration

You can configure the SDK using the following options:

Full tables and examples: docs/live-preview-configs.md.

Methods and properties

  • onLiveEdit: Trigger actions on live edits
  • onEntryChange: Listen for entry updates
  • hash: Access preview state identifier
  • setPageContext: Tell Visual Builder which entry the current page renders so “Start Editing” targets the right entry (needed for custom-URL pages)
  • config: Includes runtime context (for example Live Preview / Timeline preview, Visual Editor, or independent)

The configs table of contents 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

Documentation and learning

Developers

Content managers

Studio

Academy

版本与发布记录

@contentstack/live-preview-utils 共发布 57 个版本,首次发布于 2021/10/20;过去 12 个月发布了 17 个版本。最新版本附带 npm provenance 构建来源证明,可追溯到公开的 CI 构建记录。

版本发布日期距今
4.6.1latest2026/11/064 天前
4.6.02026/11/046 天前
4.5.32026/09/291 个月前
4.5.22026/09/161 个月前
4.5.12026/09/151 个月前
4.5.02026/08/112 个月前

其他发布通道:alpha → 3.2.0-alpha.1。

关于 @contentstack/live-preview-utils 的常见问题

@contentstack/live-preview-utils 是什么?

Contentstack provides the Live Preview SDK to establish a communication channel between the various Contentstack SDKs and your website, transmitting live changes to the preview pane.

如何安装 @contentstack/live-preview-utils?

在项目目录运行 npm install @contentstack/live-preview-utils,或使用 pnpm add / yarn add / bun add。

@contentstack/live-preview-utils 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

@contentstack/live-preview-utils 支持 ESM 还是 CommonJS?

同时提供 ES Module 与 CommonJS 入口,import 和 require() 都可以使用,打包工具会自动选择合适的版本。

@contentstack/live-preview-utils 可以商用吗?

@contentstack/live-preview-utils 使用 MIT 许可。宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。以上为概括,不构成法律意见,请以许可原文为准。

@contentstack/live-preview-utils 的下载量有多少?

根据 npm 官方下载统计,@contentstack/live-preview-utils 在 2026/10/11 – 2026/11/09 这 30 天内被下载 147,279 次,日均约 4,909 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@contentstack/live-preview-utils 还在维护吗?

最新版本 4.6.1 发布于 2026/11/06(4 天前);至今共发布 57 个版本,其中过去 12 个月发布了 17 个;首次发布于 2021/10/20。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。