Salesforce Lightning Web Components (LWC) is a modern way of building fast and scalable web components that can run on any platform. LWC can also be used to create public sites that can be accessed by anyone on the internet without requiring authentication.  In this blog, we will demonstrate how to create a public site using Visualforce Pages in Salesforce.  But before we start, let’s understand what a public site in Salesforce is.

What is a Public Site in Salesforce?

A public site in Salesforce is a website that can be accessed by anyone on the internet without requiring authentication. A public site can be used to display information, collect data, or provide services to external users. A public site can be hosted on a custom domain or a subdomain of force.com.

Steps to creating a public site in Salesforce with LWC

Step 1: Create a VF page 

 

A VF page is a Visualforce page that can be used to display LWC components on a public site. To create a VF page, go to Setup > Develop > Pages and click New. Enter a name for the VF page, such as MyLwcSite, and click OK. In the code editor, enter the following code:

This code loads the LWC component using lightning out, which is a feature that enables you to use LWC components outside of the Lightning Experience. The lightning out script loads the Lightning runtime and renders the component in the div element with the id of lwcContainer. You need to specify the namespace and component name of the LWC component you want to display, such as c:myLwcComponent.

Step 2: Create a Lightning Application

 A Lightning application is a container for your LWC component that defines its dependencies and access level. To create a Lightning application, go to Setup > Develop > Lightning Components and click New. Enter a name for the Lightning application, such as myLwcComponentApp, and click Submit. In the code editor, enter the following code:

This code creates a Lightning application that extends ltng:outApp, which is a base application that enables you to use lightning out. The aura:dependency tag defines the dependencies for your LWC component, such as c:myLwcComponent. You also need to set the access attribute to GLOBAL, which means that your Lightning application can be accessed by any Visualforce page.

 

Step 3: Create an LWC Component 

 

An LWC component is a custom web component that contains HTML, CSS, and JavaScript code. 

To create an LWC component, go to Setup > Develop > Lightning Components and click New. Enter a name for the LWC component, such as myLwcComponent, and click Submit. In the code editor, enter the following code:

This code creates an LWC component that displays some content using standard HTML tags and custom Lightning Web Components tags, such as lightning-button. 

The JavaScript file defines a handler function for the button click event that displays an alert message. The template also displays a message after following steps 1 and 2.

Step 4: Create a Public Site

A public site is a website that can be accessed by anyone on the internet without requiring authentication. 

To create a public site, go to Setup > Develop > Sites and click New. Enter a name and a label for the public site, such as MyLwcSite, and click Save. In the Site Details page, click Edit under Site Visualforce Pages.

In the Available Visualforce Pages section, select the VF page you created in Step 1, such as MyLwcSite, and click Add. Click Save. On the Site Details page, click Activate to activate your public site.

Step 5: Test Your Public Site 

To test your public site, go to Setup > Develop > Sites and click the name of your public site, such as MyLwcSite. In the Site Details page, click Preview As Admin to open your public site in a new tab. 

You should see your LWC component displayed on your public site. You can also copy the URL of your public site and paste it into another browser or device to test it without logging in to Salesforce.

Output: LWC public site link

Conclusion:

There you have it! By creating a public site in Salesforce with LWC, you can leverage the power and performance of LWC to build engaging and interactive web pages that can be accessed by anyone on the internet. Be sure to try out the solution and stay tuned for more interesting Salesforce development services!

Related Articles
Integrating-Salesforce-Lightning-Map-with-Lead-Object-A-Step-by-Step-Guide-to-Adding-Address-Markers

Lightning Web Components (LWC) is a modern JavaScript framework used to build responsive, dynamic applications on the Salesforce platform. LWC leverages web standards, allowing developers to utilize the latest JavaScript features and methodologies. Compared to older Salesforce frameworks like Aura and Visualforce, LWC is lightweight, fast, secure, and more efficient in terms of performance. Additionally, […]

Read More
Step-by-Step Guide to Creating a Custom Agentforce Agent

A custom Agentforce Agent is imperative to increase the efficiency and effectiveness of your business operations. Do you know why? In this blog, we will walk you through the key steps that can help you create a custom Agentforce Agent: What is Agentforce? Agentforce is an AI support service designed to assist agents in providing […]

Read More
Steps to Develop Prompt Template with Data Using Flow or Apex

In the world of Salesforce development, prompt templates are like the secret ingredient that powers some of the coolest features in Einstein 1 Studio. They’re the behind-the-scenes magicians who make things happen, from drafting personalized emails to summarizing records and beyond. But here’s the thing: to make these templates truly shine, you’ve got to ground […]

Read More
A Complete Guide to Second Generation Managed Packages

Second Generation Managed Packaging (2GP) is a must-have modern approach that helps in building and managing packages on Salesforce. It offers a myriad of tools for developing, distributing, and managing apps and metadata to AppExchange partners.   With the help of Managed 2GP packaging, you can utilize your custom Apex code, interact with your version control […]

Read More
Unlocking the Future Salesforce's Biometrics Service for Mobile Apps

Salesforce’s cutting-edge offering, Biometrics Service, is about to revolutionize mobile utility safety with its Spring ’24 release. This new carrier lets builders seamlessly integrate biometric authentication functions, including fingerprint and facial recognition, without delay into Lightning Net components (LWCs). This capability is important for defensive touchy records on mobile gadgets, ensuring unauthorized entry is thwarted […]

Read More
creating-dynamic-pagination-in-salesforce-using-lightning-web-components.jpg

Dynamic Pagination is a useful technique in Salesforce used for splitting large datasets into smaller and more manageable pages. The main purpose of “Dynamic Pagination” is to boost the user experience while dealing with a large amount of information. So, creating a Dynamic Pagination is important! In this blog, we will show you how to […]

Read More
Our Location worldwide
Indian Flag India
3rd Floor, A-10, Pegasus Tower, Sector 68, Noida, Uttar Pradesh 201301 +91-1203117884
United States of America Flag USA
333 West Brown Deer Road Unit G – 366 Milwaukee WI, USA 53217 +1(262) 310-7818
United Kingdom Flag UK
7 Bell Yard, London, WC2A 2JR +44 20 3239 9428
Canada Canada
HIC Global Solutions INC
43 Lafferty Lane, Richmond Hill, L4C 3N8, CA +1(262) 310-7818