Sub topic 1
Experience Cloud sites and where LWC fits
Prerequisites
This chapter assumes you already know the following:
- Basic Lightning web component structure: an HTML template, a JavaScript class, and a meta file.
- JavaScript fundamentals: ES modules, classes, and decorators such as
@apiand@wire. - Basic Apex and SOQL.
- Salesforce data model basics: objects, profiles, and sharing.
- How to use the Salesforce CLI in a Salesforce DX project.
You do not need prior experience with Experience Cloud. This chapter gives the overview that the rest of the tutorial builds on.
What an Experience Cloud site is
An Experience Cloud site is a branded web site that sits on top of your Salesforce org. It gives people outside your company, or people who do not use the main Salesforce UI, controlled access to org data and processes. Typical uses are:
- A customer help center where people open and track cases.
- A partner portal where resellers manage leads and opportunities.
- A public marketing or knowledge site that anyone can read.
- A member community with discussions and shared files.
The site uses the same objects, Apex code, and sharing rules as the rest of your org. It differs in who signs in and how they sign in. Site visitors are usually not full Salesforce license holders, so the platform limits what they can see and do.
Who uses a site
The type of visitor decides which security rules apply. You will need these terms in later chapters.
| Visitor type | How they arrive | Typical access |
|---|---|---|
| Guest user | Not logged in | Only what you explicitly share with the site's guest profile |
| External user | Logged in with a customer, partner, or member license | Data allowed by their profile, permission sets, and sharing |
| Internal user | A regular Salesforce user viewing the site | Normal org access, plus site membership rules |
Guest users are the most restricted. Many data leaks in public sites come from granting a guest profile too much access. Later chapters cover this in detail. For now, remember that every component you write may run for someone who is not logged in.
Where LWC fits
A site is built from pages. Each page is a layout of components. Some components are standard ones that Salesforce provides, such as a record detail, a list view, or a banner. Others are components you write. Lightning web components are the main way to write custom components for a site.
You write a component in the usual way, then mark it as available to the site page editor. A site builder, who may not write code, drags the component onto a page and sets its properties. This gives you a clear split of work:
- Developers build reusable components, with Apex when needed.
- Site builders arrange pages and configure components.
- Administrators control which data each visitor type can reach.
The diagram below shows how these parts connect.
The pieces you will work with
A site project involves several parts. Each has its own chapter later. This list gives you the map.
- Runtime. Every site runs on one of two runtimes, Aura or Lightning Web Runtime (LWR). They support different features, so you choose one when you create the site.
- Experience Builder. This is the visual editor where site builders create pages, add components, and publish.
- Targets in the meta file. A component appears in Experience Builder only if its meta file lists the right targets.
- Properties. The meta file can also define values that builders edit in the page editor.
- Context imports. Modules such as
@salesforce/user/isGuesttell your component who is viewing it. - Data access. Components read data through wire adapters or imperative Apex calls.
- Security. Sharing, guest profile settings, and Apex access modes decide what data is returned.
A first look at a site component
Here is a small component that you will build in pieces across this tutorial. It lists the current user's cases. Do not worry about every detail yet. Notice how the parts divide the work.
The meta file makes the component visible in Experience Builder and defines one editable property.
<?xml version="1.0" encoding="UTF-8"?>
<LightningComponentBundle xmlns="http://soap.sforce.com/2006/04/metadata">
<apiVersion>60.0</apiVersion>
<isExposed>true</isExposed>
<masterLabel>Case List</masterLabel>
<targets>
<target>lightningCommunity__Page</target>
<target>lightningCommunity__Default</target>
</targets>
<targetConfigs>
<targetConfig targets="lightningCommunity__Default">
<property name="maxRows" type="Integer" default="5" label="Maximum rows" />
</targetConfig>
</targetConfigs>
</LightningComponentBundle>The lightningCommunity__Page target lets a builder place the component on a page. The lightningCommunity__Default target lets the component expose properties in the editor.
The JavaScript class receives that property, checks whether the visitor is a guest, and wires an Apex method.
import { LightningElement, api, wire } from 'lwc';
import isGuest from '@salesforce/user/isGuest';
import getMyCases from '@salesforce/apex/CaseListController.getMyCases';
export default class CaseList extends LightningElement {
@api maxRows = 5;
isGuest = isGuest;
@wire(getMyCases, { maxRows: '$maxRows' })
cases;
get hasCases() {
return Boolean(this.cases && this.cases.data && this.cases.data.length);
}
}The $ prefix in '$maxRows' makes the wire reactive. When the builder changes the property, the wire runs again.
The template handles three states: guest, has records, and no records.
<template>
<template lwc:if={isGuest}>
<p>Log in to see your cases.</p>
</template>
<template lwc:elseif={hasCases}>
<ul>
<template for:each={cases.data} for:item="c">
<li key={c.Id}>{c.CaseNumber}: {c.Subject}</li>
</template>
</ul>
</template>
<template lwc:else>
<p>No cases found.</p>
</template>
</template>Finally, the Apex class does the real data protection.
public with sharing class CaseListController {
@AuraEnabled(cacheable=true)
public static List<Case> getMyCases(Integer maxRows) {
return [
SELECT Id, CaseNumber, Subject, Status
FROM Case
WITH USER_MODE
ORDER BY CreatedDate DESC
LIMIT :maxRows
];
}
}with sharing applies record-level sharing rules. WITH USER_MODE also enforces object and field permissions for the running user. The guest check in the template is only a convenience for the visitor. It is not a security control, because client code can be bypassed. The Apex class is where access is enforced.
Common errors at this stage
These are the mistakes people make most often when they first put LWC on a site:
- The component does not appear in Experience Builder. The meta file is missing
isExposedset totrue, or the targets list lackslightningCommunity__Page. Deploy again after fixing it. - The component works for you but fails for guests. Your user can read the object, but the guest profile cannot. The Apex class may also not be enabled for the guest profile. Test as the real visitor type.
- Properties do not show in the editor. The
targetConfigmust namelightningCommunity__Default, and the property must also be declared with@apiin the JavaScript class. - A feature works in one site but not another. The two runtimes support different features. Check which runtime the site uses before debugging your code.
Where this tutorial goes next
The next chapter compares the Aura and LWR runtimes, because that choice affects almost every later decision. After that, you will work in Experience Builder, expose components, add properties, read context, fetch data, secure it, navigate, style, test, and deploy.
Exercise
A site builder reports that your Case List component shows "Log in to see your cases." to guests, but a developer worries that guests could still read case data by calling the Apex method directly. Which part of the component prevents that, and why is the template check not enough?
Show answer
The Apex class prevents it. with sharing and WITH USER_MODE make the query respect the running user's record, object, and field access, and the guest profile must also be granted access to the class. The template check only changes what the page displays. A guest can still call an Apex method from outside the page, so the server must enforce access.
Exercise
Your component's meta file has isExposed set to true and lists only the lightningCommunity__Default target. The component does not appear in the Experience Builder component list. What do you change?
Show answer
Add the lightningCommunity__Page target to the targets list. That target makes the component available to drag onto a page. The lightningCommunity__Default target only supplies the editable properties.