Extending context data returned by the Layout Service

Current version: 21.x

In addition to returning the name, fields, and placeholders/renderings of the requested item, the Layout Service output also contains a context property. You can use this property to provide data that is needed by many components/pages (such as a user's login state), and other information derived from the HTTP Context, such as the current site context and page mode. It is also useful for providing data for statically placed components on a page, such as those not managed through placeholders and Sitecore layouts, such as header and footer navigation.

The getLayoutServiceContext pipeline lets you add context data that is returned with particular Layout Service configurations and/or JSS apps. Context data appears under the context object in the Layout Service output.

By default, JSS includes the following properties in the context object:

  • pageEditing - provided by the Layout Service, a Boolean indicating whether the route is accessed with the Experience Editor.

  • pageState - like the pageEditing property, but a string with the values normal, preview , or edit.

  • site - provided by the Layout Service, an object containing the name of the current Sitecore site context.

You can enable/disable the out-of-the-box properties and add your own data to the context object by extending the getLayoutServiceContext pipeline provided by the Layout Service.

Context Extension performance considerations

When creating custom Context Extensions, you must consider their performance because they do not have the same caching options as JSON Renderings. If your custom extension is slow or performs many API calls or item reads, the slow performance is visible on every page load where the extension is activated.

If you want to share content between multiple pages, such as header and footer content, consider either using GraphQL and caching the data on the client-side or, in your delivery application, implementing your own data cache and eviction policy for the context extension.

Do you have some feedback for us?

If you have suggestions for improving this article,