Duplicate

See original ticket which has been reopened.

Proper mechanism for easily detecting XPM mode in DXA - server side (2)

This is a copy of the following idea which was closed as 'not considering' because a solution was thought to already exist but it turns out that it doesn't. Because of that, I have created this copy to "reopen" the idea.

The original text is below:

Often times when rendering in views there is a need to know what context is the application in (XPM or not). For example, this can be to:

  • render additional html tags to be editable for elements which only have 1 visual representation on the page, typical for embedded schemas, like link text and link url
  • omitting or including different js/css files to make the content more XPM compatible
  • etc.

In my understanding there is no out of the box solution for this, only some hacks. For example like appending query strings, but this specific approach is done at a client side, so too late for the optional fields example. In other words, I would need to render all the fields and then remove them from the DOM if not in XPM context, which is crazy.

"WebRequestContext.IsPreview" should do this, but as the note on the property says, this is always true for staging.

Screenshot of Tridion Sites Ideas showing a note on 'WebRequestContext.IsPreview' property indicating it is always true for staging, making it unreliable for XPM context detection.

The proposed solution was to use IsSessionPreview in DXA 2.2, but that is not applicable, see the following SE question.

To summarize, we still need a reliable way of detecting XPM, server-side.