Salesforce Lightning Community Site URL Referencing

As Salesforce pushing with their young and new Lightning, Lightning bolt technologies to improve end-user’s experiences, more and more projects will be embrace this technology, despite the fact that there are many limitations with it at the moment.

With no exception, instead of working on Visual Force technologies, I got assigned to work with the new lightning experience. Mainly, the customer portal feature.

At my work, I came across a need for getting the community URL. The problem is, I need to retrieve a portal URL dynamically, because the URL for the portal builder is different to the published version.

For example:
Community builder URL:…
Preview URL:
Publish URL:

After digging around on the Internet, I cannot find any standard way to get the site URL with a standard way, so I decided to write my own helper function.

var currentUrl = window.location.href;
if (currentUrl.indexOf('/s/') === 0) {
    return window.location.origin;
var regex = new RegExp('(.*)\/s\/');
var regGroups = regex.exec(currentUrl);
return regGroups[1];

With this code, no matter which page in the community you landed on, you can always get the hosting domain with the site suffix.

So with the publish URL as an example given above, by applying this function, we can yield this result:

If you find a better way or a standard way to do this, please let me know by leaving your comment below.

Leave a Reply

Your email address will not be published. Required fields are marked *