enonic-nextxp-integration
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseNext.js + Enonic XP Headless Integration (Next.XP)
Next.js + Enonic XP 无头集成(Next.XP)
Procedures
操作流程
Step 1: Identify the workspace integration surface
- Inspect the workspace for Next.js entry points (,
next.config.*withpackage.jsondependency), Enonic XP app markers (nextwithbuild.gradle), or existing Next.XP adapter references (com.enonic.xp).@enonic/nextjs-adapter - Execute to inventory Next.js projects,
node scripts/find-nextxp-targets.mjs .files with.envvariables, component mapping files (ENONIC_*), and Guillotine query files when a Node runtime is available._mappings.ts - If a Node runtime is unavailable, inspect ,
package.json, and.envmanually to identify the integration surface.src/components/_mappings.ts - If the workspace contains both an Enonic XP app and a Next.js frontend, confirm which side the task targets before proceeding.
- If the workspace is not a Next.js + Enonic XP project, stop and explain that this skill does not apply.
Step 2: Configure the Enonic adapter
- Read before writing or modifying configuration.
references/nextxp-reference.md - Verify or create the (or
.envfor local development) file in the Next.js project root with the required variables:.env.local- — shared secret for preview mode authentication.
ENONIC_API_TOKEN - — fully qualified Enonic application name (e.g.,
ENONIC_APP_NAME).com.example.myproject - — locale-to-project/site mapping (e.g.,
ENONIC_MAPPINGS).en:intro/hmdb - — base URL for the Guillotine API endpoint (e.g.,
ENONIC_API).http://127.0.0.1:8080/site
- Install the package if not already present. Use the version matching the Next.js version:
@enonic/nextjs-adapterfor Next.js 16+ ornpm install @enonic/nextjs-adapter@4for Next.js 14.x. Seenpm install @enonic/nextjs-adapter@3for version requirements.references/compatibility.md - Verify the Next.js project was scaffolded from the or contains the expected file structure:
nextxp-template,src/components/_mappings.ts, and API routes undersrc/app/[locale]/[[...contentPath]]/page.tsx.src/app/api/ - Read to confirm version requirements between
references/compatibility.md, Next.js, and Enonic XP.@enonic/nextjs-adapter
Step 3: Map content types to React components
- Read for the component registry API and mapping patterns.
references/nextxp-reference.md - Read for complete content type mapping examples including queries, views, and processors.
references/examples.md - For each Enonic content type that needs a custom rendering:
a. Create a Guillotine GraphQL query function in that fetches the fields specific to that content type using type introspection (
src/components/queries/). b. Create a React view component in... on AppName_ContentTypeNamethat acceptssrc/components/views/props and renders the fetched data. c. Register both inFetchContentResultusingsrc/components/_mappings.ts.ComponentRegistry.addContentType() - For page components (pages, parts, layouts):
a. Define the component XML in the Enonic app under ,
src/main/resources/site/pages/, orparts/. b. Create a corresponding React component inlayouts/,src/components/pages/, orparts/. c. Register usinglayouts/,ComponentRegistry.addPage(), orComponentRegistry.addPart().ComponentRegistry.addLayout() - Use and
APP_NAMEimports fromAPP_NAME_UNDERSCOREDto keep content type references dynamic.@enonic/nextjs-adapter
Step 4: Configure Guillotine data fetching
- Read for the Guillotine query structure and variable passing.
references/nextxp-reference.md - Write GraphQL queries that use as the primary variable for content retrieval via
$path:ID!.guillotine { get(key:$path) { ... } } - Use type introspection to access content-type-specific fields: .
... on AppName_ContentTypeName { data { ... } } - For parts and configurable components, export a query object with and
query(path, context, config)functions.variables(path, context, config) - Use processors (optional async functions) to post-process query results before passing to the view.
- Use for data shared across all page components. Remove the common query if not needed to optimize performance.
ComponentRegistry.setCommonQuery() - Use and
getUrl()helper functions from the adapter for URL handling that works in both standalone and Content Studio preview modes.getAsset()
Step 5: Enable Content Studio preview mode
- Read for full preview architecture details.
references/nextxp-reference.md - Install the app in Enonic XP:
Next.XP.XP Menu → Applications → Install → search "Next.XP" - Add the Next.XP app to the target site in Content Studio via the "Applications" combobox on the site edit view.
- Verify default preview configuration: URL and secret matching
http://127.0.0.1:3000.ENONIC_API_TOKEN - For production, configure the Next.XP app with the deployed Next.js URL and shared secret via app config:
nextjs.default.url = <Next.js app URL> nextjs.default.secret = <shared secret> - Preview mode automatically switches the Guillotine API to the branch, while the public Next.js frontend uses the
draft(published) branch.master - Use in all component links to ensure URLs resolve correctly across preview, inline, and edit modes in Content Studio.
getUrl() - If preview shows a blank page, read for diagnostic steps.
references/troubleshooting.md
Step 6: Deploy to production
- Read for the deployment checklist.
references/nextxp-reference.md - Deploy the Enonic app to Enonic Cloud using .
enonic cloud app install - Create an ingress in Enonic Cloud to expose the Guillotine API (target path , public path
/site)./api - Deploy the Next.js app to Vercel or another hosting platform.
- Set production environment variables: ,
ENONIC_API,ENONIC_APP_NAME,ENONIC_API_TOKEN.ENONIC_MAPPINGS - Configure the Next.XP app in Enonic Cloud to point to the production Next.js URL.
- Publish content in Content Studio to make it visible on the live frontend.
Step 7: Validate the integration
- Execute to confirm the adapter, mappings, and env configuration still resolve correctly.
node scripts/find-nextxp-targets.mjs . - Verify that standalone Next.js rendering works at with published content.
http://localhost:3000 - Verify Content Studio preview renders correctly for both draft and published content.
- Test content type mappings by visiting content URLs and confirming custom views render.
- Run the workspace build () to catch TypeScript or build errors.
npm run build
步骤1:确定工作区集成范围
- 检查工作区是否存在Next.js入口文件(、含
next.config.*依赖的next)、Enonic XP应用标记(含package.json的com.enonic.xp),或是已有的Next.XP适配器引用(build.gradle)。@enonic/nextjs-adapter - 当存在Node运行环境时,执行来清点Next.js项目、含
node scripts/find-nextxp-targets.mjs .变量的ENONIC_*文件、组件映射文件(.env)以及Guillotine查询文件。_mappings.ts - 若无Node运行环境,手动检查、
package.json和.env来确认集成范围。src/components/_mappings.ts - 如果工作区同时包含Enonic XP应用和Next.js前端,先确认当前任务的目标端再继续操作。
- 如果工作区不是Next.js + Enonic XP项目,停止操作并说明本指南不适用该场景。
步骤2:配置Enonic适配器
- 编写或修改配置前请先阅读。
references/nextxp-reference.md - 验证或在Next.js项目根目录创建(本地开发则用
.env)文件,填入必填变量:.env.local- — 预览模式身份校验的共享密钥。
ENONIC_API_TOKEN - — 全限定的Enonic应用名称(例如
ENONIC_APP_NAME)。com.example.myproject - — locale到项目/站点的映射(例如
ENONIC_MAPPINGS)。en:intro/hmdb - — Guillotine API端点的基础URL(例如
ENONIC_API)。http://127.0.0.1:8080/site
- 若尚未安装包则进行安装,使用与Next.js版本匹配的版本:Next.js 16+执行
@enonic/nextjs-adapter,Next.js 14.x执行npm install @enonic/nextjs-adapter@4。版本要求可参考npm install @enonic/nextjs-adapter@3。references/compatibility.md - 验证Next.js项目是基于搭建的,或是符合预期的文件结构:包含
nextxp-template、src/components/_mappings.ts,以及src/app/[locale]/[[...contentPath]]/page.tsx下的API路由。src/app/api/ - 阅读确认
references/compatibility.md、Next.js和Enonic XP之间的版本适配要求。@enonic/nextjs-adapter
步骤3:将内容类型映射到React组件
- 阅读了解组件注册API和映射规则。
references/nextxp-reference.md - 阅读查看完整的内容类型映射示例,包括查询、视图和处理器。
references/examples.md - 对于每个需要自定义渲染的Enonic内容类型:
a. 在下创建Guillotine GraphQL查询函数,通过类型自省(
src/components/queries/)拉取该内容类型的专属字段。 b. 在... on AppName_ContentTypeName下创建React视图组件,接收src/components/views/属性并渲染拉取到的数据。 c. 在FetchContentResult中通过src/components/_mappings.ts注册上述查询和组件。ComponentRegistry.addContentType() - 对于页面组件(页面、部件、布局):
a. 在Enonic应用的、
src/main/resources/site/pages/或parts/目录下定义组件XML。 b. 在layouts/、src/components/pages/或parts/目录下创建对应的React组件。 c. 通过layouts/、ComponentRegistry.addPage()或ComponentRegistry.addPart()进行注册。ComponentRegistry.addLayout() - 使用导出的
@enonic/nextjs-adapter和APP_NAME保持内容类型引用的动态性。APP_NAME_UNDERSCORED
步骤4:配置Guillotine数据拉取
- 阅读了解Guillotine查询结构和变量传递规则。
references/nextxp-reference.md - 编写GraphQL查询时,使用作为内容检索的主要变量,格式为
$path:ID!。guillotine { get(key:$path) { ... } } - 使用类型自省访问内容类型专属字段:。
... on AppName_ContentTypeName { data { ... } } - 对于部件和可配置组件,导出包含和
query(path, context, config)函数的查询对象。variables(path, context, config) - 可使用处理器(可选异步函数)在将查询结果传递给视图前进行后处理。
- 所有页面组件共享的数据可通过配置。若不需要通用查询可移除,以优化性能。
ComponentRegistry.setCommonQuery() - 使用适配器提供的和
getUrl()辅助函数处理URL,确保在独立运行和Content Studio预览模式下都能正常工作。getAsset()
步骤5:启用Content Studio预览模式
- 阅读了解完整的预览架构细节。
references/nextxp-reference.md - 在Enonic XP中安装应用:
Next.XP。XP菜单 → 应用 → 安装 → 搜索"Next.XP" - 在Content Studio的站点编辑视图中,通过「应用」下拉框将Next.XP应用添加到目标站点。
- 验证默认预览配置:URL为,密钥与
http://127.0.0.1:3000一致。ENONIC_API_TOKEN - 生产环境需要通过应用配置为Next.XP应用设置已部署的Next.js URL和共享密钥:
nextjs.default.url = <Next.js应用URL> nextjs.default.secret = <共享密钥> - 预览模式会自动将Guillotine API切换到分支,而公开的Next.js前端使用
draft(已发布)分支。master - 所有组件链接都使用生成,确保URL在Content Studio的预览、内嵌和编辑模式下都能正确解析。
getUrl() - 如果预览显示空白页,可阅读查看诊断步骤。
references/troubleshooting.md
步骤6:部署到生产环境
- 阅读查看部署检查清单。
references/nextxp-reference.md - 使用命令将Enonic应用部署到Enonic Cloud。
enonic cloud app install - 在Enonic Cloud中创建入口,对外暴露Guillotine API(目标路径,公开路径
/site)。/api - 将Next.js应用部署到Vercel或其他托管平台。
- 设置生产环境变量:、
ENONIC_API、ENONIC_APP_NAME、ENONIC_API_TOKEN。ENONIC_MAPPINGS - 配置Enonic Cloud中的Next.XP应用,使其指向生产环境的Next.js URL。
- 在Content Studio中发布内容,使其在正式前端站点可见。
步骤7:验证集成效果
- 执行确认适配器、映射和环境变量配置仍可正常解析。
node scripts/find-nextxp-targets.mjs . - 访问验证Next.js独立渲染可正常展示已发布内容。
http://localhost:3000 - 验证Content Studio预览可正常渲染草稿和已发布内容。
- 访问内容URL确认自定义视图正常渲染,校验内容类型映射是否生效。
- 执行工作区构建命令()排查TypeScript或构建错误。
npm run build
Error Handling
错误处理
- If Content Studio preview shows a blank page, read for preview proxy diagnostics, token mismatch detection, and CORS issues.
references/troubleshooting.md - If Guillotine queries return empty or unexpected data, verify the content type name matches the introspection pattern and test the query in GraphQL Playground at .
http://localhost:8080/site/<project> - If imports fail, check
@enonic/nextjs-adapterfor version requirements and confirm the package is installed.references/compatibility.md - If content renders in preview but not on the public site, verify the content is published (moved from to
draftbranch).master - If component mappings are not applied, verify registrations in use the correct fully-qualified content type or component name with
_mappings.ts.APP_NAME
- 如果Content Studio预览显示空白页,阅读排查预览代理、令牌不匹配、CORS相关问题。
references/troubleshooting.md - 如果Guillotine查询返回空数据或不符合预期,验证内容类型名称是否匹配自省规则,并在GraphQL Playground(地址为)中测试查询。
http://localhost:8080/site/<项目名> - 如果导入失败,查看
@enonic/nextjs-adapter的版本要求,确认包已正确安装。references/compatibility.md - 如果内容在预览中正常渲染但公开站点不显示,验证内容已发布(已从分支移动到
draft分支)。master - 如果组件映射未生效,验证中的注册项使用了正确的全限定内容类型/组件名称,且搭配了
_mappings.ts变量。APP_NAME