Concept 12 of 14Build and scale

Developing for sites: LWC, Apex, security and headless options

Custom LWCs for sites, Apex controllers with sharing and user mode, Lightning Web Security and CSP, calling external APIs, navigation and record context, and headless approaches.

Your confidence:Not rated

Simple explanation

Custom components: Lightning Web Components exposed to Experience Builder (lightningCommunity__Page and lightningCommunity__Default targets), with design properties for admins.

Server logic: Apex controllers that

  • run with sharing and user mode (WITH USER_MODE, as user DML),
  • verify access to IDs passed from the browser,
  • return minimal fields, and
  • use Named Credentials for callouts (through middleware for back ends).

Security: Lightning Web Security (LWR), CSP Trusted Sites only for needed domains, no secrets in client code, and guest-accessible methods reviewed.

Data access patterns: Lightning Data Service and UI API wire adapters where possible (they respect permissions and cache), Apex for complex queries.

Headless options: custom front ends (React, mobile) calling Salesforce APIs with the site as identity provider, and newer headless capabilities announced in Winter '27 (for example the Headless Experience Layer) βš‘ verify scope.

Real-world analogy

Developing for a site is building fittings for a public building: they must be easy for the public to use, survive heavy traffic, and never give access to the staff-only areas behind them.

Worked example

// orderStatus.js (LWC on an LWR site)
import { LightningElement, wire } from 'lwc';
import getOpenOrders from '@salesforce/apex/PortalOrderController.getOpenOrders';

export default class OrderStatus extends LightningElement {
    @wire(getOpenOrders)
    orders;
}
public with sharing class PortalOrderController {
    @AuraEnabled(cacheable=true)
    public static List<Order> getOpenOrders() {
        // Sharing sets already limit orders to the user's account; user mode enforces CRUD and FLS too.
        return [
            SELECT Id, OrderNumber, Status, EffectiveDate, TotalAmount
            FROM Order
            WHERE Status != 'Cancelled'
            WITH USER_MODE
            ORDER BY EffectiveDate DESC
            LIMIT 20
        ];
    }
}

Diagram

Rendering diagram…

Key points

  • LWCs with community targets and admin-friendly properties.
  • Apex: with sharing, user mode, verify IDs, minimal data.
  • LDS / UI API where possible; Apex for complex logic.
  • Lightning Web Security, minimal CSP Trusted Sites.
  • Callouts via Named Credentials and middleware.
  • Headless front ends use the site as identity provider.

Limits and gotchas

  • cacheable=true responses are cached on the client; don't use them for rapidly changing or permission-sensitive data without care.
  • Guest-accessible controllers are internet-facing APIs.
  • LWR publishing freezes component versions; deploy and republish together.

🎀 How to say this in an interview

Treat every site controller as a public API: show user-mode access, input validation and minimal returns, plus how back-end calls are secured.

Common follow-ups

Why prefer Lightning Data Service when possible?

It enforces sharing and FLS, caches records across components, and needs no Apex to maintain.

How do you call an external API from a site component?

Through an Apex controller using a Named Credential (often to middleware), with timeouts and minimal data returned; never directly from the browser with secrets.