Sub topic 3
Navigating, Styling, and Shipping Experience Cloud Components
Assume you know basic LWC structure, JavaScript, Apex, SOQL, Salesforce permissions, and Salesforce CLI. This chapter focuses on the site-specific decisions that affect a component after it is added to a page.
Navigating between pages and records
Use NavigationMixin instead of hard-coded URLs. It creates links that work across site environments and respects Salesforce navigation rules.
import { LightningElement } from 'lwc';
import { NavigationMixin } from 'lightning/navigation';
export default class CaseLink extends NavigationMixin(LightningElement) {
handleClick(event) {
this[NavigationMixin.Navigate]({
type: 'standard__recordPage',
attributes: {
recordId: event.currentTarget.dataset.id,
objectApiName: 'Case',
actionName: 'view'
}
});
}
}For a site page, use type: 'comm__namedPage' and provide the page name in pageName. Test record and page navigation while logged in as the intended external user.
Styling components to match site branding
Keep component styles local in the component’s .css file. Prefer SLDS classes and CSS custom properties exposed by the site theme. Avoid fixed colors, fonts, and widths when the site administrator should control branding.
Using static resources and custom labels
Use static resources for packaged images, fonts, or third-party libraries. Import them with @salesforce/resourceUrl. Use custom labels for text that administrators may translate or change. Do not put site-specific text or secrets directly in JavaScript.
Lightning Web Security and component isolation
Lightning Web Security isolates components and restricts unsafe access to global objects and other namespaces. Treat data from Apex, URL parameters, and browser events as untrusted. Avoid dynamic script injection and direct DOM manipulation. Use template bindings and standard navigation APIs.
flowchart TD
A[Site visitor] --> B[LWC]
B --> C[Navigation API]
B --> D[Scoped CSS]
B --> E[Apex with sharing]
E --> F[User and record access]Testing and debugging site components
Test the JavaScript with Jest where possible. In a sandbox, test the published site as a guest user and as an authenticated external user. Check browser console errors, network requests, responsive layouts, and permission failures. A component that works in an internal Salesforce tab may still fail in Experience Cloud.
> callout warning > Never treat a successful test as proof that guest users can safely access the same records. Verify sharing, object permissions, field permissions, and Apex class access separately.
Deploying and publishing the site
Deploy the component bundle, Apex class, permissions, labels, and static resources together. Confirm the component’s .js-meta.xml file contains the required Experience Builder targets. After deployment, add or update the component in Experience Builder, preview it, and publish the site. Publishing is a separate step from deploying metadata.
Performance and caching
Request only the fields and rows the page needs. Use @wire with @AuraEnabled(cacheable=true) for read-only Apex methods when appropriate. Avoid repeated imperative calls and large static resources. Measure page loading with realistic guest and external-user sessions.
| Choice | Typical use |
|---|---|
| `@wire` | Reactive, cacheable reads |
| Imperative Apex | User-triggered or conditional actions |
| Static resource | Packaged files |
| Custom label | Translatable administrator text |
Exercise
Why should a record link use NavigationMixin instead of a manually constructed site URL?
Show answer
NavigationMixin adapts navigation to the site and Salesforce routing rules, so the link is more portable and reliable.