World is now on Opti ID! Learn more

John-Philip Johansson
Dec 19, 2017
  1535
(0 votes)

Taking more control of client-side rendering in OPE (Beta) (CMS UI 11.2.0)

This feature has changed in CMS UI 11.4.0! The event is no longer needed. Read the blog post for more info.

You can read more about how you enable Beta features in the documentation.

Earlier, in CMS UI 10.12.0, we introduced some features that let you take some control over the client-side rendering in On-Page Edit (OPE). One of those tools was the "beta/contentSaved" topic that you could subscribe to to know when to update the DOM. With CMS UI 11.2.0, we address the other side of that coin, which is letting us subscribe to know when you updated the DOM and that we need to remap the OPE overlays.

Here we’ll go through one new topic that you can use to further improve the OPE experience while using Angular, React, or any other JavaScript framework. You can read more in the Editing documentation.

The global JavaScript object (see "Telling OPE that you need new overlays"):

  • epi.publish("beta/domUpdated")

Knowing when to remap the OPE overlays

One common scenario is when you have an element with the property "data-epi-property-name" that will enable the OPE overlay, but since this element is not present when the OPE overlays are created, you don't see an OPE overlay for the element. This makes editing that property slightly challenging in OPE and once again you have to direct your editors to the All properties view. The Sticky View Mode can keep your editors from switching views all the time, but it would be nicer if OPE just works, right?

There are several situations where the OPE overlay doesn't match the DOM:

  • Element is conditionally rendered by the client-side framework
    • Example: A text that's only shown when the user reaches the last page in a pagination
  • Element is replaced by another similar element but using another "data-epi-property-name" value
    • Example: When loading the value was "fact1", and after user interaction the value is "fact2"
  • Element is rendered after the page has loaded
    • Example: Data is fetched through an API and only renders once it's done

We'll focus on the first two examples because they're easier to demo. For the user, it can look like this:

Image before.gif

Telling OPE that you need new overlays

We felt that using a message to communicate, like with "beta/contentSaved", was a nice solution so we decided to create a new topic: "beta/domUpdated". To tell the OPE view to remap all the overlays on the page, simply publish to that topic using the global "epi" object:

epi.publish("beta/domUpdated");

Note: You cannot subscribe to this topic from the view because the communication only goes from and to the iframe where the page view lives. A gadget or other code on the CMS UI side would be able to subscribe though.

Considerations when using an over-powered feature

With the new topic, it's possible to accidentally spam messages, prefering to publish one too many than one too few. This causes a few problems.

  1. UI becomes unresponsive as the spamming is ongoing
  2. Memory usage increases

To solve problem 1, we added a debounce period of 100 milliseconds where each new message resets the cool down period. This causes a small (100ms) lag before the OPE overlays are remapped.

To solve problem 2, we had to do several performance improvements, that also improve memory usage in other scenarios, such as changing pages (context change). All in all, we've made dozen small fixes, but as we say in Sweden: "Many small streams make a big river."

In one scenario we tried, we made a Higher-Order Component in React that would publish a message on the "beta/domUpdated" topic on componentDidMount and componentDidUpdate. The problem was that we created a new wrapped component in the render method, and that lead to an exponential growth of messages every time something rendered. The lesson is that it's very easy to accidentally publish too many messages. :) As the great American poet Ben Parker once said: "With great power, there must also come great responsibility". So try to consolidate your DOM changes to as few messages as you can.

A simple example in React

The message in "domUpdated" is simpler than the "contentSaved" because there is no message object involved. To demonstrate this we're just going to publish the message in our ThreeKeyFacts' componentDidUpdate method, but you should try to consolidate the messages so each component doesn't live its own life and spam "domUpdated" messages.

import React from "react";

export default class ThreeKeyFacts extends React.Component {

    constructor(props) { ... }

    render() { ... }

    next() { ... }

    previous() { ... }

    componentDidMount() {
        // Here we do some epi.subscribe("beta/contentSaved", ...)
    }

    componentDidUpdate() {
        if (window.epi && window.epi.publish) {
            // Publishing a message on the "beta/domUpdated" topic will tell episerver UI that the DOM has changed 
            // and that it needs to remap its overlays.
            window.epi.publish("beta/domUpdated");
        }
    }
}

By publishing a message on the "beta/domUpdated" topic in componentDidUpdate the overlays now match the elements being shown:

Image after.gif

 

Code on Github

Please contribute to these repos to help each other with how to work with OPE and your favourite JavaScript framework!

The example above is in our React sample repo: https://github.com/episerver/AlloyReact

Please contribute an example to the AngularJS repo: https://github.com/episerver/AlloyAngularJS

Dec 19, 2017

Comments

Please login to comment.
Latest blogs
Make Global Assets Site- and Language-Aware at Indexing Time

I had a support case the other day with a question around search on global assets on a multisite. This is the result of that investigation. This co...

dada | Jun 26, 2025

The remote server returned an error: (400) Bad Request – when configuring Azure Storage for an older Optimizely CMS site

How to fix a strange issue that occurred when I moved editor-uploaded files for some old Optimizely CMS 11 solutions to Azure Storage.

Tomas Hensrud Gulla | Jun 26, 2025 |

Enable Opal AI for your Optimizely products

Learn how to enable Opal AI, and meet your infinite workforce.

Tomas Hensrud Gulla | Jun 25, 2025 |

Deploying to Optimizely Frontend Hosting: A Practical Guide

Optimizely Frontend Hosting is a cloud-based solution for deploying headless frontend applications - currently supporting only Next.js projects. It...

Szymon Uryga | Jun 25, 2025

World on Opti ID

We're excited to announce that world.optimizely.com is now integrated with Opti ID! What does this mean for you? New Users:  You can now log in wit...

Patrick Lam | Jun 22, 2025

Avoid Scandinavian Letters in File Names in Optimizely CMS

Discover how Scandinavian letters in file names can break media in Optimizely CMS—and learn a simple code fix to automatically sanitize uploads for...

Henning Sjørbotten | Jun 19, 2025 |