@contentful/experiences-live-preview

中文文档
0.7.15发布于 2026/11/07MITTypeScriptES Module近 30 天下载 4858

Optional live preview for Contentful Experiences

原始使用文档
文档目录3 节
  1. Runtime parameters and Session updates
  2. API
  3. Usage

@contentful/experiences-live-preview

Optional live preview for Contentful Experiences.

Runtime parameters and Session updates

Direct by-ID fetchExperience accepts per-call parameters; see the runtime parameters guide. Preview Sessions retain their existing unprefixed context.parameters map (for example { productId: 'foo123' }), with values supported by the Session contract. The Contentful editor owns Session context changes. fetchPreviewSession loads the current hydrated snapshot; createLivePreviewClient consumes subsequent WebSocket snapshots. Replace the rendered plan from each update as usual. These APIs do not mutate Session parameters or encode parameter.* query keys.

API

type PreviewSessionOptions = {
  spaceId: string;
  environmentId: string;
  previewToken?: string;
  sessionId?: string;
  resourceResolution?: string;
  sessionHost?: string;
  debug?: boolean;
};

type PreviewSessionExperienceOptions = {
  spaceId: string;
  environmentId: string;
  sessionId: string;
  resourceResolution?: string;
};

type PreviewSessionClientOptions = {
  previewToken: string;
  host?: string;
};

type PreviewSessionResolveOptions = {
  config: ResolverConfig;
  metadata?: Record<string, unknown>;
  debug?: boolean;
};

fetchPreviewSession(
  previewSessionOptions: PreviewSessionExperienceOptions,
  clientOptions: PreviewSessionClientOptions,
  resolveOptions: PreviewSessionResolveOptions,
): Promise<PortableRenderPlan>;

PreviewSessionFetchError;
LivePreviewConnectionError;
NotFoundError;
PREVIEW_WEBSOCKET_HOST;

type LivePreviewResult = {
  data: ExperiencePayload | undefined;
  error: Error | undefined;
};

type LivePreviewClient = {
  getResult(): LivePreviewResult;
  subscribe(listener: () => void): () => void;
  subscribeStatus(listener: (status: LivePreviewStatus) => void): () => void;
};

createLivePreviewClient(
  previewSessionOptions: PreviewSessionOptions,
  initialPayload?: ExperiencePayload,
): LivePreviewClient;

sendPreviewStatus(status: LivePreviewStatus): void;

Use fetchPreviewSession for the initial render when the Contentful app provides a preview_session_id. It fetches the current Preview Session snapshot and resolves it into a PortableRenderPlan. Use createLivePreviewClient to receive subsequent WebSocket updates.

import { fetchPreviewSession } from '@contentful/experiences-live-preview';

const plan = await fetchPreviewSession(
  { spaceId, environmentId, sessionId },
  { previewToken },
  { config: experienceConfig }
);

The request sends previewToken as a Bearer token in the Authorization header. host selects a compatible Preview Session API host and defaults to the production host. Pass the same encoded resourceResolution value in the previewSessionOptions passed to fetchPreviewSession and createLivePreviewClient when the session references resources from other spaces. A missing session throws NotFoundError. Other request and response failures throw PreviewSessionFetchError.

Usage

createLivePreviewClient returns a data source for a Preview Session. It opens the socket when the first listener subscribes and publishes each valid next payload as received. If you pass initialPayload, getResult().data returns it until a valid update arrives. Without initialPayload, the data starts as undefined.

getResult().error is set when the live-preview connection fails. The last valid data remains available in getResult().data.

sessionId and previewToken are optional. The package opens a socket only when both values are provided. The caller supplies the session ID through PreviewSessionOptions.

sessionHost is an optional WebSocket URL for the Preview Session service. It defaults to the production Contentful Session service. The SDK uses the URL as supplied, appends the subscription route, and sends previewToken as the encoded access_token query parameter. When provided, resourceResolution is sent as the encoded resource_resolution query parameter.

When you use the package directly in a Contentful preview, connect the client's status subscription to sendPreviewStatus:

import { createLivePreviewClient, sendPreviewStatus } from '@contentful/experiences-live-preview';

const client = createLivePreviewClient(previewSessionOptions);
const unsubscribeStatus = client.subscribeStatus(sendPreviewStatus);
const unsubscribe = client.subscribe(() => {
  const experience = client.getResult().data;
  if (experience) updatePreview(experience);
});

This tells the Contentful app how to coordinate updates with the SDK. The subscription is required for this integration. Framework adapters set it up for you. Keep both unsubscribe functions and call them when the preview no longer uses the client.

A valid next message replaces the current data atomically. The client keeps the last valid data when it receives malformed messages, server errors, or transport interruptions. Unknown message types are ignored. The next payload contains an Experience. Adapters can build on this source and reuse its socket and message handling. Other frameworks can use the same framework-neutral client.

版本与发布记录

@contentful/experiences-live-preview 共发布 33 个版本,首次发布于 2026/09/23;过去 12 个月发布了 33 个版本。

版本发布日期距今
0.7.15latest2026/11/074 天前
0.7.142026/11/055 天前
0.7.132026/11/055 天前
0.7.122026/11/056 天前
0.7.112026/11/046 天前
0.7.102026/10/3111 天前

关于 @contentful/experiences-live-preview 的常见问题

@contentful/experiences-live-preview 是什么?

Optional live preview for Contentful Experiences

如何安装 @contentful/experiences-live-preview?

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

@contentful/experiences-live-preview 支持 TypeScript 吗?

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

@contentful/experiences-live-preview 支持 ESM 还是 CommonJS?

以 ES Module 形式发布,推荐使用 import 引入。在 CommonJS 项目中,Node.js 20.19+ / 22.12+ 可以直接 require() 不含顶层 await 的 ES Module,更早的版本需要改用动态 import()。

@contentful/experiences-live-preview 可以商用吗?

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

@contentful/experiences-live-preview 的下载量有多少?

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

@contentful/experiences-live-preview 还在维护吗?

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