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:

4 Answers 4


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.

  • what happens in case of hyprid compos with implements="flexipage:availableForAllPageTypes,forceCommunity:availableForAllPageTypes"? Will the result be different in LEX vs. Community?
    – Uwe Heim
    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
    Sep 28, 2017 at 19:01
  • cool, thats exactly what i need. will test it tomorrow.
    – Uwe Heim
    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. Jan 10, 2018 at 8:55
  • 1
    FYI, I get "LivePreview" in the Digital Experience page builder.
    – Keith C
    May 31, 2022 at 18:23

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.

  • fyi: the answer was updated a few weeks ago with a valid approach
    – glls
    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
    Apr 22, 2018 at 7:55

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



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.

You must log in to answer this question.

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