Editor integration using metadata
A Next.js application can be integrated with Pages to enable dynamic, visual editing of page content. This relies on the Sitecore Headless Services HTTP rendering engine, Next.js API routes, and the Next.js Preview Mode.
The recommended method for editor integration involves the metadata provided by the layout service for placeholders, renderings, and fields. This metadata is used by Pages to identify layout elements and enable visual editing. Metadata-based editing was introduced in JSS 22.1, and all new JSS 22.1 apps use this method by default.
This type of integration is specifically for working with Pages and requires a Next.js app based on JSS 22.1 or later. If you're using JSS 22.0 or earlier, or if you also want to integrate with Experience Editor, you need to use the older chromes integration method.
If you don't need to integrate with Experience Editor, you can improve the performance of your app by making some configuration changes.
The following diagram illustrates the architecture of integrating a Next.js JSS app with Pages using the Metadata Mode architecture introduced in JSS 22.1. Of the two integration methods, this approach is significantly easier to set up and maintain, and it removes the need for ngrok tunnelling and CORS exemptions.
The diagram highlights APIs included with JSS for Next.js in teal. Elements with other colors are part of the Next.js JSS sample application.
With this method of editor integration, if you want to test components without affecting published sites or other users on the same environment, you can connect your local host directly to Pages.
API routes
The Next.js JSS sample application includes three Next.js API routes that facilitate the integration with Sitecore content and layout editors, as follows:
-
The Render API route defined in the file
<src>/pages/api/editing/render.ts
handlesGET
requests from XM Cloud Pages using logic encapsulated in the middlewareEditingRenderMiddleware
provided by JSS for Next.js. Use this route as the value for the JSS app configuration optionserverSideRenderingEngineEndpointUrl
. -
The Config API route defined in the file
<src>/pages/api/editing/config.ts
handlesGET
requests from XM Cloud Pages using logic encapsulated in the middlewareEditingConfigMiddleware
provided by JSS for Next.js. -
The Page route, which is the main Next.js optional catch-all route defined in
<src>/pages/[[...path]].tsx
, renders all Sitecore page routes.
JSS editing secret
The JSS editing secret is a token for securing the Sitecore editor endpoints exposed by the JSS Next.js apps through the Render API Route.
You must set the JSS editing secret both client-side and server-side.
The middleware EditingRenderMiddleware
automatically validates the token. If validation fails, it returns a response with the HTTP status code 401
.
Next.js preview mode
Preview mode is a built-in feature of Next.js meant for CMS use cases where you write draft content (such asin XM Cloud Pages, and you want to render pages with this content at request time, bypassing any static generation.
Integrating Next.js JSS apps with Sitecore editors relies on this feature.
The EditingRenderMiddleware
middleware enables Next.js preview mode, which creates specific cookies on the Render API route responses and passes them along to the subsequent page route request.
The page props factory, responsible for preparing all the data needed to render the page, checks whether the current route is in preview mode; if it is, the page route request is an editing request and can use the preview data to fetch editing data using the GraphQLEditingService
.
Understanding data editing in JSS Next.js
A GET
request made by XM Cloud Pages to the render API route contains the required query string parameters to render a given page route.
The page props factory uses those query string parameters to fetch editing data using the GraphQLEditingService
. The editing data contains the layout data and dictionary data necessary for rendering the page.
The page props factory uses the editing data to populate the page props
.
The following represents an example GET
request to the editing render endpoint when using metadata-based integration:
/api/editing/render?secret={EDITING_SECRET}&sc_site=nextjs-app&sc_itemid=54C8E9B5-0B2C-5363-8FA6-D32A3A302F51&sc_lang=en&route=/&mode=edit&sc_version=latest&sc_variant={VARIANT_ID}&sc_layoutKind=shared
sc_layoutKind
is an enum with a default value of final
, but it can be set to shared
instead if needed.
The sc_version
, sc_variant
, and sc_layoutKind
parameters are all optional.
APIs
You can find the relevant APIs for Sitecore editor integration in the @sitecore-jss/sitecore-jss-nextjs
NPM package in the editing
submodule.
To use any of these, import them into your code file. For example:
import { EditingRenderMiddleware } from '@sitecore-jss/sitecore-jss-nextjs/editing';
EditingRenderMiddleware
EditingRenderMiddleware
is a middleware handler for the editing Render API route. It does the following:
-
Validates the JSS editing secret.
-
Extracts the required query string parameters from the request URL.
-
Enables Next.js preview mode, passing the parameters to the preview data.
-
Redirects to the requested page route.
EditingConfigMiddleware
EditingConfigMiddleware
is a middleware handler for the editing Config API Route. It does the following:
-
Validates the JSS editing secret.
-
Provides a required configuration (application metadata) for XM Cloud Pages to determine feature compatibility and configuration.
This middleware supports the pagesEditMode
option, which allows you to specify the editing mode used by Pages. It is set to metadata
by default, but if you aren't ready to use metadata integration you can continue using chromes editing integration for Pages by manually setting pagesEditMode
to EditMode.Chromes
as follows:
import { EditMode } from '@sitecore-jss/sitecore-jss-nextjs';
const handler = new EditingConfigMiddleware({
...
pagesEditMode: EditMode.Chromes,
}).getHandler();
GraphQLEditingService
GraphQLEditingService
is the service responsible for fetching editing data from an editor such as XM Cloud Pages.
To use it, you would typically include the GraphQLEditingService
singleton in <src>/lib/graphql-editing-service.ts
.
Using an external editing host
If you're using an editing host provided by XM Cloud, the SITECORE_CONTEXT_ID
environment variable is initially set to the preview value of the Sitecore context ID.
The way to configure an external editing host depends on your use case:
-
If you're using a dedicated editing host, or if you're using an editing host for preview deployments only, ensure the
SITECORE_CONTEXT_ID
environment variable remains set to the preview value of the Sitecore context ID. -
If you're using the editing host for both preview and live deployments, you need to set the
SITECORE_CONTEXT_ID
environment variable to the value of the live Sitecore context ID, and you need to set theSITECORE_PREVIEW_CONTEXT_ID
environment variable to the preview value of the Sitecore context ID.SITECORE_PREVIEW_CONTEXT_ID
should be used byGraphQLEditingService
(<src>/lib/graphql-editing-service.ts
) to fetch the data for preview mode. To enable this, create a dedicated editingclientFactory
using<src>/lib/graphql-client-factory/create.ts
. -
If you're using Pages with a local container setup, you need to include
SITECORE_API_HOST
andSITECORE_API_KEY
environment variables.