Common API
Contents
Exports from @enonic/nextjs-adapter may be used in shared code unless their behavior requires a server-only Next.js API.
Usage
import {
ComponentRegistry,
getLocaleMapping,
I18n,
imageUrl,
imageUrlQuery,
validateData
} from '@enonic/nextjs-adapter';
Functions
getContentBranch
Returns the XP branch for a request context: master for direct Next.js and live requests, draft for editing contexts.
Parameters
getContentBranch() takes an optional Context object. Without it, the branch follows the RENDER_MODE environment variable, defaulting to master.
Returns
string — master or draft.
getLocaleMapping
Resolves the locale, project, and site mapping for a request. The Content Studio project header takes precedence over the route locale.
Parameters
getLocaleMapping() takes a single Context object.
Returns
LocaleMapping.
getLocaleMappingByLocale
Returns the mapping for locale. The optional useDefault boolean defaults to true and controls fallback to the default mapping.
Parameters
| Name | Type | Description |
|---|---|---|
|
|
|
Optional. Locale to find. |
|
|
|
Optional. Fall back to the default mapping. Default: |
Returns
LocaleMapping or undefined.
getLocaleMappingByProjectId
Returns the mapping for an XP project ID. Matching is case-insensitive; useDefault defaults to true.
Parameters
| Name | Type | Description |
|---|---|---|
|
|
|
Optional. XP project ID to find. |
|
|
|
Optional. Fall back to the default mapping. Default: |
Returns
LocaleMapping or undefined.
getRequestLocaleInfo
Returns {locale, locales, defaultLocale} for a request context. The project header is used in Content Studio; otherwise the route locale or default locale is used.
Parameters
getRequestLocaleInfo() takes a single Context object.
Returns
An object containing the selected locale, all configured locales, and defaultLocale.
localizeMappings
Prefixes the targets of URL mapping rules with a locale, for the /api/mappings response. Targets of the default mapping are returned unchanged, because default-locale URLs carry no prefix. Sources stay site-relative.
Parameters
| Name | Type | Description |
|---|---|---|
|
|
UrlMappingRule`[]` |
Rules to localize. |
|
|
|
Locale mapping of the project, normally from |
Returns
UrlMappingRule`[]`.
Example
const MAPPINGS: UrlMappingRule[] = [
{sources: ['/.*'], target: '/${siteRelativePath}'}
];
return NextResponse.json({mappings: localizeMappings(MAPPINGS, mapping)});
encryptParams
Encrypts a JSON-serializable object with AES-256-GCM, using the SHA-256 hash of secret as the key. The result is the xp blob format the Next.XP app sends. Requires Node.js crypto, so call it only from server code.
Parameters
| Name | Type | Description |
|---|---|---|
|
|
|
Values to encrypt. |
|
|
|
Shared secret, normally |
Returns
string — base64url-encoded IV, ciphertext, and authentication tag.
decryptParams
Decrypts an xp blob produced by the Next.XP app or encryptParams(). Successful decryption proves that the sender knows the shared secret. Requires Node.js crypto, so call it only from server code.
Parameters
| Name | Type | Description |
|---|---|---|
|
|
|
Value of the |
|
|
|
Shared secret, normally |
Returns
Record<string, any> with the decrypted values, such as xpProject, or null when the blob is invalid or was encrypted with another secret.
imageUrlQuery
Returns the Guillotine selection for an image URL: imageUrl(<args>) with every field of the ImageUrl type. Use it inside a … on media_Image selection.
Parameters
imageUrlQuery() takes a single args object with these properties:
| Name | Type | Description |
|---|---|---|
|
|
|
Image scale, for example |
|
|
|
Optional. JPEG quality. |
|
|
|
Optional. Background color for transparent images. |
|
|
|
Optional. Output format, for example |
|
|
|
Optional. Image filter. |
|
|
|
Optional. Extra query parameters added to the URL. |
Returns
string.
Example
`... on media_Image { ${imageUrlQuery({scale: 'width(500)'})} }`
mediaUrlQuery
Returns the Guillotine selection mediaUrl(<args>) with every field of the AttachmentUrl type.
Parameters
mediaUrlQuery() takes an optional MediaUrlArgs object.
Returns
string.
attachmentUrlQuery
Returns the Guillotine selection attachmentUrl(<args>) with every field of the AttachmentUrl type.
Parameters
attachmentUrlQuery() takes an optional MediaUrlArgs object.
Returns
string.
pageUrlQuery
Returns the Guillotine selection pageUrl(<args>) { url path queryString }.
Parameters
pageUrlQuery() takes an optional args object with these properties:
| Name | Type | Description |
|---|---|---|
|
|
|
Optional. Extra query parameters added to the URL. |
Returns
string.
imageUrl
Builds an absolute image URL from the result of imageUrlQuery(): the media base URL (ENONIC_MEDIA_CDN, default ENONIC_API) followed by the media part of path and the queryString. In client code without a NEXT_PUBLIC_ copy of these variables, the path is returned unchanged.
Parameters
data (ImageUrl, or any object with path and an optional queryString) — URL object from Guillotine. May be null or undefined.
Returns
string, or undefined when data is empty.
mediaUrl
Builds an absolute media URL from the result of mediaUrlQuery(), in the same way as imageUrl().
Parameters
data (AttachmentUrl, or any object with path and an optional queryString) — URL object from Guillotine. May be null or undefined.
Returns
string, or undefined when data is empty.
attachmentUrl
Builds an absolute attachment URL from the result of attachmentUrlQuery(), in the same way as imageUrl().
Parameters
data (AttachmentUrl, or any object with path and an optional queryString) — URL object from Guillotine. May be null or undefined.
Returns
string, or undefined when data is empty.
pageUrl
Builds a relative Next.js URL from the result of pageUrlQuery(). The site-relative path is prefixed with meta.locale unless it equals meta.defaultLocale.
Parameters
| Name | Type | Description |
|---|---|---|
|
|
URL object from Guillotine. Only |
|
|
|
|
Usually the page’s MetaData. Without |
Returns
string, or undefined when data is empty.
Example
<Link href={pageUrl(parent.pageUrl, meta)}>Back</Link>
<Link href={pageUrl({path: '/'}, meta)}>Home</Link>
richTextFragments
Returns the GraphQL fragment definitions spread by richTextQuery(), including the registered macros. fetchGuillotine() appends them automatically; use this function for queries sent through another client.
Returns
string.
withRichTextFragments
Appends the rich-text fragment definitions a query spreads but does not define.
Parameters
query (string) — GraphQL document.
Returns
string.
sanitizeGraphqlName
Converts text into a valid GraphQL name by replacing unsupported characters and prefixing names that begin with a number.
Parameters
text (string) — value to sanitize.
Returns
string.
validateData
Validates a FetchContentResult. Missing content invokes Next.js not-found handling; XP shortcuts invoke redirect handling.
Parameters
props (FetchContentResult) — result returned by fetchContent().
Returns
void. The function may invoke Next.js navigation handling.
Classes
ComponentRegistry
Static registry connecting XP descriptors to queries, processors, and React views.
addContentType
Registers a content-type mapping by application-qualified name.
addPage
Registers an XP page descriptor.
addLayout
Registers an XP layout descriptor.
addPart
Registers an XP part descriptor.
addMacro
Registers an XP macro descriptor.
addComponent
Registers a generic component kind such as the built-in part, layout, or text handling.
Each registration method accepts a name and a ComponentDefinitionParams object. Repeated registration merges properties for the same name.
setCommonQuery
Sets the query whose processed result is returned as common for every render.
getCommonQuery
Returns the registered shared query.
getByComponent
Resolves the definition for a normalized PageComponent from its type and descriptor.
getContentType
Returns a content-type definition by name, falling back to CATCH_ALL when registered.
getPage
Returns a page definition by descriptor, with catch-all fallback.
getLayout
Returns a layout definition by descriptor, with catch-all fallback.
getPart
Returns a part definition by descriptor, with catch-all fallback.
getMacro
Returns a macro definition by descriptor, with catch-all fallback.
getComponent
Returns a generic component-kind definition, with catch-all fallback.
getContentTypes
Returns all explicitly registered content-type definitions as name-definition pairs.
getPages
Returns all explicitly registered page definitions.
getLayouts
Returns all explicitly registered layout definitions.
getParts
Returns all explicitly registered part definitions.
getMacros
Returns all explicitly registered macro definitions.
getComponents
Returns all explicitly registered generic component definitions.
I18n
Static localization helper for server-rendered front-end phrases.
setLocale
Loads @phrases/<locale>.json and returns its dictionary.
localize
Returns the phrase for a key and substitutes positional {} or indexed {0} placeholders. A missing phrase is returned as <key>.
getLocale
Returns the active locale.
getDictionary
Returns the loaded phrase dictionary.
Type Definitions
Context
| Name | Type | Description |
|---|---|---|
|
|
|
Site-relative path requested from XP. |
|
|
|
Optional. Request headers. |
|
|
|
Optional. Active front-end locale. |
LocaleMapping
| Name | Type | Description |
|---|---|---|
|
|
|
Whether this is the fallback mapping. |
|
|
|
XP project ID. |
|
|
|
Site path inside the project. |
|
|
|
Front-end locale. |
ComponentDefinitionParams
| Name | Type | Description |
|---|---|---|
|
|
|
Optional. Guillotine query and optional variables function. |
|
|
|
Optional. Query selection for component configuration. |
|
|
|
Optional. Asynchronous data transformation. |
|
|
|
Optional. React renderer. |
GlobalVariables
Variables the adapter supplies to every registered query. A variables function must return them, optionally with additional variables.
| Name | Type | Description |
|---|---|---|
|
|
|
Full XP content path, including the site. |
|
|
|
Site path from the locale mapping. |
|
|
|
XP project from the locale mapping. |
|
|
|
|
UrlMappingRule
One rule of the /api/mappings response.
| Name | Type | Description |
|---|---|---|
|
|
|
Conditions a content item must meet. A |
|
|
|
Next.js URL template. |
|
|
|
Optional. Match when any source matches instead of all. Default: |
ImageUrl
Result of imageUrlQuery().
| Name | Type | Description |
|---|---|---|
|
|
|
Ready-made URL, with the base URL resolved by XP. |
|
|
|
Full media API path with a leading slash. |
|
|
|
URL-escaped query string with a |
|
|
|
Project context segment. |
|
|
|
Content ID. |
|
|
|
Media fingerprint. |
|
|
|
Processed scale segment. |
|
|
|
File name, with the requested format extension. |
AttachmentUrl
Result of mediaUrlQuery() and attachmentUrlQuery(). It has the url, path, queryString, context, id, fingerprint, and name fields of ImageUrl, plus:
| Name | Type | Description |
|---|---|---|
|
|
|
|
PageUrl
Result of pageUrlQuery().
| Name | Type | Description |
|---|---|---|
|
|
|
URL as XP resolves it. |
|
|
|
Site-relative content path. |
|
|
|
URL-escaped query string. |
MediaUrlArgs
| Name | Type | Description |
|---|---|---|
|
|
|
Optional. Serve the file as a download. |
|
|
|
Optional. Extra query parameters added to the URL. |
MetaData
Runtime information returned by fetchContent(): content identity and path, project, site, branch, XP base URL, locale, render mode, request type, renderability, and requested component.
Constants
| Export | Purpose |
|---|---|
|
|
Application key from the environment and descriptor-safe variants. |
|
|
Whether |
|
|
Registry key for a fallback definition. |
|
|
Direct, edit, preview, inline, live, and admin rendering modes. |
|
|
Normalized page-component kinds and fetch request kinds. |
|
|
Name of the query parameter carrying the encrypted blob from XP: |
|
|
Header names shared with the XP preview proxy. |
|
|
DOM attributes used by Content Studio editing. |
|
|
Guillotine selection required for processed rich-text references. |
Prefer these exports to duplicated string values in application code.