Introduction To Content SDK with Sitecore AI
Hello everyone, welcome to another blogpost. This blog will cover the new sdk package released by Sitecore for Sitecore AI and how it differs from traditional JSS SDK.
The Sitecore Content SDK connects SitecoreAI content to front-end JavaScript applications. It is the successor to Sitecore JavaScript Services (JSS). SitecoreAI is the new name for XM Cloud, announced at Sitecore Symposium 2025. This post covers what the SDK offers, what changed from JSS, and how its core pieces work in version 1.x and version 2.x, based on Sitecore's official documentation.
1. What is the Content SDK?
The SDK's APIs and services fetch SitecoreAI data and use it to build and deliver pages. Developers can:
- Build apps locally and create custom components.
- Push components back to SitecoreAI for marketers to use.
- Test changes by connecting a local host to the SitecoreAI Pages visual editor.
| Item | Details |
|---|---|
| Current version | 2.x (Next.js 16 minimum) |
| Framework | Next.js (Pages Router and App Router; App Router became generally available in 1.2) |
| Main package | @sitecore-content-sdk/nextjs |
| CLI package | @sitecore-content-sdk/cli (command: sitecore-tools) |
| Starter kit | Next.js starter template |
| License | Apache 2.0 (open source, github.com/Sitecore/content-sdk) |
2. Key Features
- Visual editing: integration with SitecoreAI Pages for editing and testing components.
- Personalization and A/B/n testing without custom code: page variants for audience categories (for example, time zone or language) and component variants tested across percentages of visitors.
- Multi-site support. By default, different sites can't have different default languages.
- GraphQL utilities for content, layout data, site information, and dictionaries.
- Built-in analytics, event tracking, and personalization.
- Next.js support: locale-based routing, SSR, and SSG.
3. Benefits
Official figures vs JSS-based applications
| Smaller bundles | Fewer files | Less code | Starter kit on disk |
|---|---|---|---|
| 49% | 81% | 39% | 8.15 MB → <1 MB |
Source: Sitecore's "Why upgrade" guide. The starter kit's footprint is about 89% smaller.
Other documented benefits
- Faster local development and component creation.
- Testing against live remote data before publishing.
- Quick app generation from a starter template.
- Faster initial loads and better Core Web Vitals from smaller bundles.
- Explicit component mapping and leaner configuration.
- Familiar Next.js patterns, so Next.js developers get up to speed faster.
4. What Changed: JSS vs Content SDK
| Area | JSS | Content SDK |
|---|---|---|
| Next.js package | @sitecore-jss/sitecore-jss-nextjs | @sitecore-content-sdk/nextjs |
| CLI package | @sitecore-jss/sitecore-jss-cli | @sitecore-content-sdk/cli |
| Dev tools package | @sitecore-jss/sitecore-jss-dev-tools | Removed |
| Data access | Multiple services and plugins | Single SitecoreClient class |
| Configuration | Spread across files | sitecore.config.ts (runtime) + sitecore.cli.config.ts (build/CLI) |
| Middleware | Separate middleware plugin files | One chain: defineMiddleware (1.x) / defineProxy (2.x) |
| Scaffolding | jss scaffold | sitecore-tools project component scaffold |
To migrate, Sitecore provides upgrade guides: JSS 22.0 → Content SDK 1.5.1 (1.x docs) and JSS 22.0 → Content SDK 2.1 (2.x docs). The core step is to replace every @sitecore-jss import with its Content SDK equivalent, then run a build or type-check to catch missing references.
5. Configuration: sitecore.config.ts
sitecore.config.ts in the app root is the central runtime configuration. Starter apps ship a lightweight version with mandatory settings only. The key properties are the same in 1.x and 2.x:
| Property | Environment variable |
|---|---|
api.edge.contextId | SITECORE_EDGE_CONTEXT_ID (server) |
api.edge.clientContextId | NEXT_PUBLIC_SITECORE_EDGE_CONTEXT_ID |
api.edge.edgeUrl | NEXT_PUBLIC_SITECORE_EDGE_URL (default https://edge-platform.sitecorecloud.io) |
api.local.apiKey / apiHost | NEXT_PUBLIC_SITECORE_API_KEY / NEXT_PUBLIC_SITECORE_API_HOST |
defaultSite | NEXT_PUBLIC_DEFAULT_SITE_NAME |
defaultLanguage | NEXT_PUBLIC_DEFAULT_LANGUAGE (default 'en') |
editingSecret | SITECORE_EDITING_SECRET |
Set exactly one of api.edge or api.local. Using separate server and client Context IDs gives data access without exposing sensitive credentials in the browser. Other settings include layout, dictionary, retries, redirects, multisite, personalize, and generateStaticPaths.
Retries and caching
- Retries are on by default for the layout, dictionary, and error-page services.
retries.countdefaults to3; set it to0to disable them. retries.retryStrategyaccepts aRetryStrategyfrom@sitecore-content-sdk/nextjs/client. The default,DefaultRetryStrategy, uses exponential back-off (factor 2) for HTTP 429, 502, 503, 504 and 520–524.- Dictionary caching (
dictionary.caching) is in-memory and on by default, with a 60-second timeout.
To read config values anywhere in the app:
import scConfig from 'sitecore.config';
6. Core Architecture: SitecoreClient
SitecoreClient is a generic, framework-agnostic class for working with SitecoreAI's headless APIs. It is initialized with a SitecoreClientInit object derived from your SitecoreConfig. Its custom property lets you inject your own service implementations.
Key methods (the same in 1.x and 2.x):
| Method | Purpose / returns |
|---|---|
getPage(path, pageOptions?, fetchOptions?) | Page for a route (layout data). Promise<Page | null> |
getPreview(previewData, fetchOptions?) | Preview / editing page. Promise<Page | null> |
getDictionary(routeOptions?, fetchOptions?) | Promise<DictionaryPhrases> |
getErrorPage(code, pageOptions?, fetchOptions?) | Error page. Promise<Page | null> |
getPagePaths(languages?, fetchOptions?) | Static paths for SSG. Promise<StaticPath[]> |
getComponentData(layoutData, context, moduleFactory) | Component-level props (Next.js only) |
getHeadLinks(layoutData, options?) | Style/theme <link> elements (enableStyles, enableThemes) |
getSiteMap(options, fetchOptions?) / getRobots(siteName, fetchOptions?) | Sitemap XML and robots.txt for SEO |
getDesignLibraryData(designLibData, fetchOptions?) | Design Library data. Promise<Page> |
getData<T>(query, variables?, fetchOptions?) | Raw GraphQL request using your configured retry, header, and fetch behavior. Promise<T> |
7. Components and the CLI
The CLI is configured in sitecore.cli.config.ts using defineCliConfig from @sitecore-content-sdk/nextjs/config-cli. It must be installed globally.
Scaffold a component
sitecore-tools project component scaffold ProductCard
sitecore-tools project component scaffold MyBYOCComponent --byoc
sitecore-tools project component scaffold MyComponent --templateName=myTemplate
- Creates
src/components/ProductCard.tsxfrom the default template for the framework. - Custom templates must be defined under
scaffold.templatesinsitecore.cli.config.ts. --configpoints to a different CLI config file.
Build command
sitecore-tools project build
Runs the functions listed under build.commands in order (by default generateMetadata() and generateSites()) to prepare build artifacts.
Component map
The component map at .sitecore/component-map.ts maps Sitecore rendering names to components. It exports componentMap, a Map whose string keys match the component names in Sitecore.
- Generated automatically by the
buildanddevscripts (sitecore-tools:generate-map). - Configured under
componentMapinsitecore.cli.config.ts:paths(defaultsrc/components),exclude,destination(default.sitecore),componentImports, andmapTemplate. - Manual registration is only needed for advanced cases such as Next.js
dynamic()imports. In that case, removesitecore-tools:generate-mapfrom the scripts. - App Router: two maps are generated:
component-map.ts(all components, server-side) andcomponent-map.client.ts(client components only), so server code stays out of browser bundles.
8. Middleware (1.x) and Proxy (2.x)
Request pre-processing such as multisite resolution, redirects, and personalization runs as a single chain. The API was renamed in 2.x.
| Content SDK 1.x | Content SDK 2.x | |
|---|---|---|
| File | middleware.ts | proxy.ts |
| Import | @sitecore-content-sdk/nextjs/middleware | @sitecore-content-sdk/nextjs/proxy |
| Base class | Middleware (or MiddlewareBase) | ProxyHandler (or ProxyBase) |
| Method to implement | handle(req, res) | handle(req, res) |
| Chain | defineMiddleware(...).exec(req, ev) | defineProxy(...).exec(req) |
Extend MiddlewareBase / ProxyBase when you want to inherit the default Sitecore methods. With ProxyBase, your constructor must accept a ProxyBaseConfig. Handlers are passed as separate arguments, not as an array.
Custom handler: 1.x
import { defineMiddleware, Middleware } from '@sitecore-content-sdk/nextjs/middleware';
import { NextRequest, NextResponse, NextFetchEvent } from 'next/server';
class LoggerMiddleware extends Middleware {
async handle(req: NextRequest, res: NextResponse): Promise<NextResponse> {
console.log(`[LOGGER] ${req.method} ${req.url}`);
return res;
}
}
const logger = new LoggerMiddleware();
export function middleware(req: NextRequest, ev: NextFetchEvent) {
return defineMiddleware(logger, multisite, redirects, personalize).exec(req, ev);
}
Custom handler: 2.x
import { ProxyHandler } from '@sitecore-content-sdk/nextjs/proxy';
import { NextRequest, NextResponse, NextFetchEvent } from 'next/server';
class LoggerProxy extends ProxyHandler {
async handle(req: NextRequest, res: NextResponse): Promise<NextResponse> {
console.log(`[LOGGER] ${req.method} ${req.url}`);
return res || NextResponse.next();
}
}
export function proxy(req: NextRequest, event: NextFetchEvent) {
const logger = new LoggerProxy();
return defineProxy(logger, multisite, redirects, personalize).exec(req);
}
multisite, redirects and personalize are the built-in handler instances already set up in the starter app's middleware.ts / proxy.ts.
Detecting editing mode
There is no official helper. As a workaround, check the Sitecore cookies sc_headless_mode and sitename#sc_mode. Next.js uses __next_preview_data (Preview Mode, Pages Router) and __prerender_bypass (Draft Mode, App Router).
9. Key Takeaways
- Officially 49% smaller bundles, 81% fewer files and 39% less code than JSS, with a starter kit under 1 MB.
- One
SitecoreClient(getPage,getDictionary,getData, and more) replaces JSS's separate services. - Configuration lives in two files:
sitecore.config.tsandsitecore.cli.config.ts. - Retries (3, exponential back-off) and dictionary caching (60 s) are on by default.
- The component map is generated automatically; App Router gets separate server and client maps.
- Middleware in 1.x (
defineMiddleware) became Proxy in 2.x (defineProxy,proxy.ts). - Content SDK 2.x requires Next.js 16.
References
- Content SDK for SitecoreAI (2.x)
- Why upgrade to Content SDK – Sitecore Developer Portal
- The SitecoreClient API (2.x) · (1.x)
- The Sitecore configuration file (2.x) · (1.x)
- CLI commands and configuration (2.x)
- Register a component in the component map (2.x)
- Manage component maps for client and server components
- Integrate custom proxy (2.x) · Integrate custom middleware (1.x)
- Upgrade JSS 22.0 to Content SDK 1.5.1 · to Content SDK 2.1
Comments
Post a Comment