Sub topic 4
Exposing a component to Experience Builder
This chapter assumes you can build a basic Lightning web component (HTML template, JavaScript class, meta file), use @api in JavaScript, and deploy with the Salesforce CLI from a Salesforce DX project. You don't need Apex knowledge yet, but you will see it in later chapters.
A component you deploy is not automatically available to site builders. Experience Builder lists only the components whose meta file says they can be placed on a site page. This chapter covers that meta file: what each part does, how it connects to your JavaScript, and what goes wrong when it is incomplete.
How a component reaches the page editor
The meta file (caseList.js-meta.xml) is the only place Experience Builder looks to decide whether to show your component. The JavaScript and HTML are not consulted until a builder drops the component on a page.
The meta file
Here is the meta file for a caseList component.
<?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>Each element has a specific job:
| Element | Purpose |
|---|---|
| `isExposed` | Master switch. When it is `false` or missing, the component cannot be used outside other components. |
| `masterLabel` | The name shown in the Components panel. Without it, Experience Builder falls back to the component's API name. |
| `targets` | Lists the places the component may be used. |
| `targetConfigs` | Holds settings for individual targets, including editable properties. |
The two Experience Cloud targets
The two targets do different things, and you usually need both.
lightningCommunity__Pagemakes the component draggable onto a page in Experience Builder. Without it, the component does not appear in the Components panel.lightningCommunity__Defaultmakes the component configurable. It tells Experience Builder that atargetConfigwith this target describes the properties to show in the property editor.
A component with only lightningCommunity__Page can be placed but has no editable settings. A component with only lightningCommunity__Default is not listed in the panel at all.
Two other Experience Cloud targets cover special cases. lightningCommunity__Page_Layout is for components that define the regions of a page layout. lightningCommunity__Theme_Layout is for components that wrap every page, such as a header or footer. Use them only when you build those layout components. Ordinary content components use the two targets above.
Connecting a property to JavaScript
A <property> in the meta file is only a declaration for the page editor. The component needs a matching public property in its class, marked with @api.
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 name in the meta file (maxRows) must match the @api property name exactly, including case. When a builder types a value into the property editor, Experience Builder sets maxRows on your component, the same way a parent component would set it in its template.
The @wire parameter '$maxRows' is reactive. When the builder changes the value, the wire re-runs and fetches data again. Later chapters cover the wire adapter and the Apex method in detail.
A few rules for properties:
- The
typeattribute in the meta file sets the editor control. Common types areString,Integer,Boolean, andColor. Pick the type your JavaScript expects. defaultin the meta file is what a newly placed component receives from the editor. Set the same default in JavaScript so the component also behaves sensibly in other contexts, such as a unit test.labelis the text shown beside the field. Without it, the editor shows the property name.- Treat values that arrive through
@apias read-only inside the component. If you need a changed copy, store it in a separate field.
The next chapter, on configurable properties, covers more property attributes and types.
Using the properties in the template
The template reads the same class members as any other component.
<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>Nothing in the template is specific to Experience Builder. The exposure rules live in the meta file only. This is why a component that works in a Lightning app page can often be reused on a site by changing the meta file.
Deploying and seeing the component
Deploy the bundle, then open Experience Builder for your site.
sf project deploy start --source-dir force-app/main/default/lwc/caseListIn Experience Builder, open the Components panel and look under the custom components section for "Case List". Drag it onto a page. Select it, and the property editor shows "Maximum rows" with the value 5.
Changes to a deployed component do not reach visitors until the site is published. Experience Builder shows your latest deployed code in the editor, but the live site serves the last published version. Publishing is covered in the deployment chapter.
Common errors
| Symptom | Likely cause |
|---|---|
| Component missing from the Components panel | `isExposed` is not `true`, `lightningCommunity__Page` is missing, or the editor tab was open before deployment and needs a refresh. |
| Component appears but the property editor is empty | `lightningCommunity__Default` is missing from `targets`, or the `targetConfig` names a different target. |
| Property appears but the component ignores the value | The `@api` property is missing or its name differs from the meta file. |
| Deployment fails on the meta file | An element is out of order or has a typo, or a property uses an unsupported `type`. |
| Value works in the editor but not for visitors | The site has not been republished. |
Exercises
Exercise
A teammate deploys a component whose meta file has isExposed set to true and only the lightningCommunity__Default target. It does not appear in the Components panel. What should they change, and what will they gain?
Show answer
Add lightningCommunity_Page to the targets list. That target makes the component draggable onto a page. Having lightningCommunity_Default alone only describes editable properties for a component that can already be placed.
Exercise
The meta file defines a property named maxRows, but the class declares "@api maxrows = 5;". The editor shows the field, yet changing it has no effect. Why, and how do you fix it?
Show answer
Property names are case-sensitive, so the editor sets maxRows while the class exposes maxrows. Rename the class property to maxRows so the names match exactly, then redeploy and refresh Experience Builder.
Exercise
You change the default for maxRows from 5 to 10 in the meta file and deploy. A component already placed on a page still shows 5 rows. Why?
Show answer
The default applies when a component is newly placed. A component already on the page stores the value the builder saved, so it keeps 5 until the builder changes it in the property editor. Publish the site afterward so visitors see the update.