[SalesForce] Detect community builder context

We have a number of package components for lightning communities and would like to improve their experience in the community builder. Many of these components depend on a certain context (url parameters for example) and when we are in the builder that context does not exist.

Is there a supported way to detect that we are in the community builder so that we can serve up dummy data to give the admin a better preview? The only option I see is to look for the --sitepreview url portion but this seems like it might change in the future or have permutations I am not aware of with different My Domain setups. I know the Napili community app itself has a number of pseudo-documented properties like the search context so maybe this is one of them and I just can't find it?

We don't want to serve dummy data when the context is null because this could happen on the live site where we want to instead show an error.

Best Answer

Having not received any official feedback on this, here is what we implemented. This is not a future proof solution because it relies on the domain, but from my research it seems unlikely that the domain will change. Note that sitestudio is not included as the actual site is served in an iframe when in the builder.

/**
 * Get whether the current window is in the community builder or community site
 * preview.
 * @param  {String} url The url to check. If null, the current hostname will be
 *                        used.
 * @return {Boolean}    True if the current window is a preview.
 */
getIsSitePreview: function(url) {
  var urlToCheck = url;
  if(!urlToCheck) {
    urlToCheck = window.location.hostname;
  }
  urlToCheck = urlToCheck.toLowerCase();
  return urlToCheck.indexOf('sitepreview') >= 0 || urlToCheck.indexOf('livepreview') >= 0);
}

This has allowed us to meet our requirements for now. Hopefully Salesforce just adds a context varible to the community interface that allows us to get at this but in the meantime I think this is the best option.