@contentful/optimization-web-preview-panel

中文文档
2.0.6发布于 2026/11/06MITTypeScriptESM + CJS近 30 天下载 2987

Preview panel UI for Contentful Optimization Web integrations

原始使用文档
文档目录8 节
  1. Optimization Web Preview Panel
  2. Getting started
  3. When to use this package
  4. Pre-fetched entries
  5. Common configuration
  6. Production bundle control
  7. Content security policy support
  8. Related

Contentful Logo

Contentful Personalization & Analytics

Optimization Web Preview Panel

This package implements the first-party browser preview panel for the Optimization Web SDK. It loads into the DOM as a Lit-based Web Component micro-frontend and talks to the Web SDK through the preview bridge exposed by the Optimization Web SDK runtime.

[!IMPORTANT]

Upgrading from v1? Follow the v1-to-v2 migration guide and align this package with the upgraded Web SDK packages.

Table of Contents

Getting started

Install using an NPM-compatible package manager, pnpm for example:

pnpm add @contentful/optimization-web-preview-panel

Import the attach function; both CJS and ESM module systems are supported, ESM preferred:

import attachOptimizationPreviewPanel from '@contentful/optimization-web-preview-panel'

Attach the preview panel with an existing, unmodified Contentful Delivery API client. By default, the attach function uses the browser singleton created by the Optimization Web SDK:

attachOptimizationPreviewPanel({
  contentful: contentfulClient,
})

When your application already fetches preview content, pass entries instead:

attachOptimizationPreviewPanel({
  entries: {
    audiences: audienceEntries,
    experiences: experienceEntries,
  },
})

The attach function appends the panel to the DOM and adds the toggle button that opens it. It is safe to call more than once; repeated calls reuse the in-flight or completed panel attachment. The panel search uses fuzzy matching for audience and optimization names and highlights matched text in the result list.

[!IMPORTANT]

Importing this package has no side effects. It does not attach the panel, define custom elements, inject styles, touch storage, or mutate globals until attachOptimizationPreviewPanel(...) is called.

When to use this package

Use @contentful/optimization-web-preview-panel when a Web SDK or React Web SDK integration needs the browser preview panel attached to an existing Contentful SDK client. Application code can pass an explicit Optimization Web SDK instance, but the common browser flow uses window.contentfulOptimization.

Pre-fetched entries

Use entries when the app cannot give the browser preview panel a direct Contentful client or when it already owns the query layer. This covers GraphQL query adapters, Hydrogen loaders, SSR data handoff, tag-filtered Contentful queries, and proxy APIs.

The entries option has this shape:

attachOptimizationPreviewPanel({
  entries: {
    audiences,
    experiences,
  },
})

Each value can be either a Contentful-style entry collection or a readonly array of Contentful entries. The preview panel normalizes arrays into entry collections, keeps only nt_audience items from audiences, keeps only nt_experience items from experiences, and preserves experiences.includes.Entry so linked variant names can be displayed.

If both entries and contentful are provided, entries wins and the preview panel does not fetch through the Contentful client.

Common paths:

  • Contentful client - Pass contentful first when the browser can use an existing Contentful Delivery API client.
  • GraphQL - Map GraphQL nodes to Contentful entry objects, then pass the arrays as entries.audiences and entries.experiences.
  • Hydrogen loaders and SSR - Load audiences and experiences in the loader or server route, serialize them to the browser, then attach with entries.
  • Tag-filtered queries - Pass the filtered Contentful collections directly. The panel still filters by content type before building definitions.
  • Proxy APIs - Return { audiences, experiences } from the proxy and pass that object as entries.

Common configuration

OptionRequired?DefaultDescription
contentfulConditionalN/AExisting Contentful client used to read preview content
entriesConditionalN/APre-fetched audience and experience entries
optimizationNowindow.contentfulOptimizationExisting Optimization Web SDK instance
nonceNoundefinedCSP nonce applied to Lit styles when strict CSP is enabled

Provide either contentful or entries.

For the complete attach function signature, use the generated Preview Panel reference.

Production bundle control

The preview panel is intended for development and staging builds. Consumers own that build policy and can wrap the attachment call in an environment-backed conditional:

import attachOptimizationPreviewPanel from '@contentful/optimization-web-preview-panel'

if (import.meta.env.PUBLIC_OPTIMIZATION_ENABLE_PREVIEW_PANEL === 'true') {
  void attachOptimizationPreviewPanel({ contentful: contentfulClient })
}

When the consumer bundler replaces that condition with a build-time false value and performs dead-code elimination, the side-effect-free preview panel import graph can be removed from the production output.

Content security policy support

In strict CSP environments, pass a nonce directly:

attachOptimizationPreviewPanel({ contentful: contentfulClient, nonce })

Alternatively, set window.litNonce before attaching the panel:

window.litNonce = nonce
attachOptimizationPreviewPanel({ contentful: contentfulClient })

版本与发布记录

@contentful/optimization-web-preview-panel 共发布 25 个版本,首次发布于 2026/02/10;过去 12 个月发布了 25 个版本。

版本发布日期距今
2.0.6latest2026/11/064 天前
2.0.52026/11/046 天前
2.0.42026/10/2220 天前
2.0.32026/10/1922 天前
2.0.22026/10/1625 天前
2.0.12026/09/231 个月前

其他发布通道:alpha → 0.1.0-alpha6。

关于 @contentful/optimization-web-preview-panel 的常见问题

@contentful/optimization-web-preview-panel 是什么?

Preview panel UI for Contentful Optimization Web integrations

如何安装 @contentful/optimization-web-preview-panel?

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

@contentful/optimization-web-preview-panel 支持 TypeScript 吗?

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

@contentful/optimization-web-preview-panel 支持 ESM 还是 CommonJS?

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

@contentful/optimization-web-preview-panel 可以商用吗?

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

@contentful/optimization-web-preview-panel 的下载量有多少?

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

@contentful/optimization-web-preview-panel 还在维护吗?

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