7

I have a Lightning Components and need to know if it is running inside of a Community or in Lightning Experience.

Is there a way to detect the running context?

Right now I'm checking document.location against a hardcoded Community-path but I need a general solution.

Best Idea I have so var to make it dynamic is to check for '/s/' in the URL. But I'm not sure if this is reliable for all possible Community pages. Also it feels like an hack.

After detecting the context, I also need to get the URL Prefix from the community. Again I could cut it out of the path, but would prefer otherwise.

Googling around didn't brought much. So far I found some context-aware unanswered questions here:

5 Answers 5

10

You can try to use the SessionManagement Class's getCurrentSession() method, which will return a map of attributes for the users current session. Amongs these, you will find SessionType, which will indicate the context. Here are some that I tested:

Lightning Communities:

  • Preview & Builder mode: SitePreview or LivePreview
  • Live Site: ChatterNetworks

Lightning Experience:

  • LEX App page: Aura

further details can be found at Understanding Session Types

You will have to do this in an Apex class and call your method to get the context in your clientside controller.

10
  • what happens in case of hyprid compos with implements="flexipage:availableForAllPageTypes,forceCommunity:availableForAllPageTypes"? Will the result be different in LEX vs. Community?
    – Uwe Heim
    Commented Sep 28, 2017 at 18:51
  • flexipage:availableForAllPageTypes -> will only return true in LEX, forceCommunit‌​y:availableForAllPag‌​eTypes-> will only return true in lightning communities (ex. napili template)
    – glls
    Commented Sep 28, 2017 at 19:01
  • cool, thats exactly what i need. will test it tomorrow.
    – Uwe Heim
    Commented Sep 28, 2017 at 20:48
  • implements="force:appHostable,flexipage:availableForAllPageTypes,forceCommunity:availableForAllPageTypes" . I am checking following line of code cmp.isInstanceOf("forceCommunity:availableForAllPageTypes") it's returning true in both of the cases. Commented Jan 10, 2018 at 8:55
  • 1
    FYI, I get "LivePreview" in the Digital Experience page builder.
    – Keith C
    Commented May 31, 2022 at 18:23
4

Since @glls answer is not working, as a workaround we are decomposing the URL. I don't like this approach, but it looks like that Salesforce is leaving us alone on this issue.

We have put the following code inside a js utility-libray we are using via ltng:require in almost all of our components

utility = {
        community : {
            context: (new RegExp('.*?\/s\/','g')).exec(window.location.href) != null,
            is : function() {
                return this.context;
            }, 
            path : function() {
                let path = (new RegExp(':\/\/(.*\.com)\/([A-Za-z0-9]*\/)?s\/','g')).exec(window.location.href); 
                return this.context != null ? path[2] == undefined ? '' : path[2] : null;
            }
        },
}

utility.community.is will be dynamically true in case a hybrid component is running inside Community-context and false if running inside LEX-context.

To do something really useful, we need also the path of the community (can be either blank or a string). You need it e.g. to dynamically compose image-urls, attachment-urls, links, etc. so that a compo is working on ANY community AND in LEX without config or any kind of adjustment.

2
  • fyi: the answer was updated a few weeks ago with a valid approach
    – glls
    Commented Apr 21, 2018 at 21:20
  • thanks glls, I will stick to the URL solution, because I still need to detect the community name to compose dynamic paths to files and images. As said above the APEX-way might not be dynamically enough to cover hybrid usages of session Ids. Salesforce should provide a 100% dynamic solution for context-detecion as part of the JS framework.
    – Uwe Heim
    Commented Apr 22, 2018 at 7:55
0

Site.getName() will return null if NOT in a community

https://developer.salesforce.com/docs/atlas.en-us.apexref.meta/apexref/apex_classes_sites.htm

0

I had a similar use case where I needed to detect Lightning page or Community page so that I could make certain functionalities available in the Lightning page and not in Community page. I achieved this adding a property, like "isHostCommunitySite." In the Community Site, I checked this box so that when this property is true in the component, those functionlities are not available. In aura, same thing can be achieved adding a property in Design file. This is not a native way, but I think it is clean and works great.

0
<aura:component implements="force:hasRecordId, force:appHostable, flexipage:availableForAllPageTypes, forceCommunity:availableForAllPageTypes" controller="CustomController">
    <aura:handler name="init" value="{!this}" action="{!c.init}"/>
</aura:component>




({
    init : function(component, event, helper) {
        var action = component.get("c.isCommunity");
        action.setCallback(this, function(response) {
            var isCommunity = response.getReturnValue(); // do any operation needed here
        });
        $A.enqueueAction(action);
    }
})

Apex Code

public with sharing class CustomController {
    @AuraEnabled
    public static boolean isCommunity(){
        Id siteId = Site.getSiteId(); // take a look at the apex class Site, you may find more useful method concerning site/community
        if (siteId != null) {
            return true;
        }
        return false;
    }
}
2
  • Thank you for contributing to the Stack Exchange community. This may be a correct answer, but it’d be really useful to explain your code further so developers can understand your reasoning. This is especially useful for new developers who aren’t as familiar with the syntax or are struggling to understand the concepts. Would you kindly edit your answer to include additional details to benefit the community? Commented Aug 21 at 4:12
  • Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.
    – Community Bot
    Commented Aug 21 at 4:12

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .