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

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.