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,
getUrl,
I18n,
validateData
} from '@enonic/nextjs-adapter';
Functions
getContentApiUrl
Returns the Guillotine URL for the context’s project and branch. Direct and live requests use master; editing contexts use draft.
Parameters
getContentApiUrl() takes a single context object with these properties:
| Name | Type | Description |
|---|---|---|
|
|
|
Site-relative content path. |
|
|
|
Optional. Request headers carrying Content Studio context. |
|
|
|
Optional. Route locale. |
Returns
string — absolute Guillotine project and branch URL.
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 the same single context object as getContentApiUrl().
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 the same single context object as getContentApiUrl().
Returns
An object containing the selected locale, all configured locales, and defaultLocale.
getUrl
Processes an internal content or media URL for the supplied MetaData. It removes the XP site prefix and applies the public locale or Content Studio base URL.
Parameters
| Name | Type | Description |
|---|---|---|
|
|
|
URL to process. |
|
|
Runtime context returned by |
Returns
string.
getAsset
Processes a resource URL without adding a locale segment. Use it for assets rather than navigable content links.
Parameters
The same url and meta parameters as getUrl().
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.
UrlProcessor
Low-level URL processor used by getUrl(), getAsset(), and rich-text views.
process
Processes a URL using MetaData. Optional flags distinguish server-side output and resource URLs.
processSrcSet
Processes every URL in an image srcset, preserving width and pixel-density descriptors.
isMediaLink
Tests whether a rich-text link reference points to media.
isContentImage
Tests whether a rich-text image reference resolves to content.
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. |
MetaData
Runtime information returned by fetchContent(): content identity and path, project, site, branch, API and preview base URLs, 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. |
|
|
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.