Sub topic 13
Lightning Web Security and component isolation
This chapter assumes you can build a basic Lightning web component and expose it to Experience Builder. It also assumes you know the difference between Aura and LWR sites from earlier chapters.
What Lightning Web Security does
Lightning Web Security (LWS) is the security architecture that isolates Lightning web components from one another. A site can run components from your org, managed packages, and other namespaces on the same page. LWS keeps each namespace's code from reading or changing another namespace's data and DOM.
LWS works by running each namespace's JavaScript in its own sandbox. Inside the sandbox, standard browser objects such as window and document are replaced with secure versions. These versions apply rules called distortions, which change or block API behavior that would break isolation.
LWS replaced Lightning Locker, the older isolation model. LWS supports more standard JavaScript and web APIs, and it works better with third-party libraries.
Which runtime uses it
| Site runtime | Isolation model |
|---|---|
| LWR site | Always Lightning Web Security |
| Aura site | Your choice of Lightning Locker or Lightning Web Security |
For an Aura site, the setting is in Experience Workspaces under Administration, Settings, Advanced. Changing it affects every component on the site. Test the whole site after you switch.
What isolation means in practice
- Components in the same namespace share one sandbox. They can share state more freely.
- Components in different namespaces cannot reach into each other's DOM or JavaScript objects. Communicate with public
@apiproperties, methods, and events. - Global changes are contained. If a component edits a built-in prototype, other namespaces do not see the change.
- You still need
lwc:dom="manual"to insert your own DOM nodes, as in any other LWC.
Loading third-party libraries
Libraries from static resources are the most common source of LWS problems. Load them with loadScript and handle errors, so a distortion that breaks a library shows up in the console.
import { LightningElement } from 'lwc';
import { loadScript } from 'lightning/platformResourceLoader';
import CHARTLIB from '@salesforce/resourceUrl/chartlib';
export default class ChartPanel extends LightningElement {
libLoaded = false;
async renderedCallback() {
if (this.libLoaded) {
return;
}
this.libLoaded = true;
try {
await loadScript(this, CHARTLIB);
} catch (error) {
console.error('Could not load chartlib', error);
}
}
}Common errors
- A library works in a sandbox but fails on the site. The library probably uses an API that LWS distorts. Check the browser console for the blocked call, then try a newer version of the library.
- A component finds another namespace's element with
querySelectorand gets nothing. This is isolation working as designed. Pass data through the public API instead. - A security change breaks components after you switch an Aura site to LWS. Retest each component, including managed package components.
Exercise
A page on an LWR site includes your component and a component from a managed package. Your code calls document.querySelector to find a button inside the package component, and it returns null. Why, and what is the correct way to interact with that component?
Show answer
LWR sites always use Lightning Web Security, which runs each namespace in its own sandbox. Your component cannot reach into another namespace's DOM, so the query finds nothing. Use the package component's public API: set its @api properties, call its @api methods, or listen for the events it dispatches.