Site Framework

Contents

The Enonic Site Framework is the built-in way to build content-driven sites with Enonic apps, as an alternative to using a third-party front-end framework. This section covers how the site service handles a request, and how mappings, components, page contributions, and response processors take part in rendering it.

For a hands-on introduction, visit the Enonic Site Framework tutorial.

Introduction

A content-driven site gets its pages and URLs from the content: editors create and compose the pages, and XP renders them.

A site built with the Site Framework is rendered by the site service: the part of XP that responds at :8080/site/<project>/<branch>/<path-to-content>. The site service resolves the page for the requested content item, and renders its components with the server-side implementations in your app. It is purpose-built for delivering content-driven websites, and is closely linked to the CMS features of the platform. Combine it with vhosts for custom domains and paths.

Enonic works with any front-end framework. Next.js, for example, renders outside Enonic and fetches the content over HTTP. As alternatives to using a third-party front-end framework, two options run on the site service, inside XP:

  • The Site Framework uses the page rendering built into the site service, as described in this section. Each component is rendered by the implementation placed next to its descriptor.

  • React4XP also runs on the site service. It uses mappings to replace the built-in page and component rendering with rendering based on React. The rendering is detached from the component descriptors: a component registry binds each component, or content type, to a React view. This is the same model headless front-ends use. Visit the React4XP tutorial for details.

Visit the CMS documentation for details on content modeling and schemas. This section focuses on implementing site rendering via Enonic apps.

Usage

To activate the site service, you will need a site descriptor in your app.

src/main/resources/cms/site.yaml
kind: "Site"
mappings: (1)
  - controller: "/cms/mappings/security-txt.js"
    pattern: '/\.well-known/security\.txt'
processors: (2)
  - name: "tracker"
apis: (3)
  - "ws"
1 mappings — Optional. Binds HTTP functions and filters to URLs or content. Here, requests for /.well-known/security.txt on the site are handled by security-txt.ts. See Mappings.
2 processors — Optional. Functions that process the response after rendering. Here, the processor in cms/processors/tracker.ts. See Response processors.
3 apis — Optional. APIs to mount on the site. Here, the ws API of the same application. See Universal APIs.

All three are optional: a descriptor with only kind: "Site" is enough to activate the site service.

You will also need implementations of Mapping function or component functions to start rendering.

Site Request pipeline

The site pipeline is executed as a subset of the common pipeline:

Site rendering pipeline

This section describes the standard pipeline involved in processing a page request, e.g. :8080/site/<project>/<branch>/<path-to-page>.

Mappings::Use path or content-based site mappings. Each mapping will trigger a function, acting as a filter or a controller (returning a response directly). Filters will iteratively execute until a controller is reached, or the mappings are exhausted. If no mapping matches, the request continues to the default page rendering step.

Render page

Looks up the content at the path and resolves the page implementation and config (optionally via a page template) or returns a matching 404 if not found. 401 if protected, and 403 if user lacks permissions.

The content types base:shortcut is automatically handled with a 307 Temporary Redirect, and portal:fragment is only rendered when referenced from another page.

Implementation resolution:

  1. Implementation set directly on the content.

  2. Implementation set on a page template for the content. If the page template is unavailable (e.g. deleted), the page resolver treats it as if no page template was set.

  3. Implementation set on the first page template found in {site}/_templates that supports the content’s type.

    If no implementation is found, the response status is 404 Not Found.

    In inline rendering mode the status is 418 instead of 404, so Content Studio can switch to alternative rendering. In edit rendering mode the status is 200 instead of 404, to allow Content Studio to render the in-place component selector.

Component rendering:

Once the page component is rendered, the rendering iteratively continues until all nested components placeholders are resolved and rendered.

Response processors

For text/html responses, any response processors defined in the site descriptor are executed. Processors may manipulate the result, for instance by adding page contributions. Once all processors are done, contribution merging takes place: available pageContributions in the response object are merged into the html response body.

Response filters

Finally, any filters registered through a mapping iteratively execute their response processing step before the response is returned from the pipeline.

Two kinds of requests are routed past these steps:

Render component

A request to the component endpoint invokes the implementation of a single component directly. The result may include nested components, which are resolved and rendered iteratively (via component placeholders) until all placeholders are resolved and rendered.

Universal APIs

Sites support contextual APIs mounted in the same URL space. They are available on the /_/<app>:<api> path. APIs are mounted via the site descriptor.

The image and attachment APIs are implicitly mounted by the site service, and are available at //media:image and //media:attachment respectively. Use portal library functions to generate URLs for these, and they will be served with the appropriate caching and optimizations — including when an operator has moved media to a separate host or turned the implicit mounting off, in which case the generated URLs point elsewhere.

One site, multiple apps

XP is modular: an app is a unit that is installed, started, and stopped on its own, and a site is assembled from several of them. The apps are added to the site in Content Studio:

The Applications field of a site in Content Studio

The modularity applies to the content schemas, where each app contributes content types to the site under its own namespace. It applies in the same way to the front-end: every app with a site descriptor takes part in the request pipeline of the sites it is added to.

The schemas and the front-end don’t have to come from the same app. With React4XP, for instance, the schemas are typically placed in one app, and the front-end in another.

Rendering is normally the responsibility of one app: the app that provides the page, part, and layout components. The other apps contribute around the rendering, without rendering pages themselves:

Mappings

An app may serve specific URLs on the site, or handle specific content. The site service collects the mappings of every app on the site before rendering starts.

Filters

Filters reached through mappings, from every app on the site, form a single chain around the request. See HTTP filters.

Response processors

An app may process the rendered page, typically to add page contributions such as scripts and meta tags. See Response processors.

APIs

The site descriptor of an app mounts APIs on the site: its own, or those of another app. See Contextual APIs.

Site configuration

An app may declare a form in its cms.yaml descriptor. Content Studio shows the form for every site the app is added to, and the values are available through getSiteConfig().

Where several apps contribute at the same step, the order value decides the sequence, lowest first. For equal values, the position of the apps on the site decides.

As an example, the Sitemap.xml app consists of a single mapping for /sitemap.xml, and a site configuration form. Adding it to a site gives the site a sitemap, while another app keeps rendering the pages.

This lets you split functionality across apps, and reuse apps across sites. Visit Enonic Market for existing apps.

When an app is not available

An app stays on the site when it is stopped or uninstalled. Content Studio lists it by its name, as not available, and the configuration stored for it on the site is kept.

While the app is not available, it takes no part in the request pipeline. What happens to the site depends on the role of the app:

  • A contributing app - The site keeps working without the features of that app. A URL served by one of its mappings is handled as any other request, and normally ends in a 404 response.

  • The app that renders the site - Pages that use its components respond with 503. The other apps on the site keep working.

When the app is started again, it takes part as before, with the stored configuration.


Contents

Contents