Sub topic 4
Lightning Locker compared with LWS
This chapter assumes you know the basic structure of a Lightning web component, modern JavaScript (modules, classes, async/await), the same-origin policy, and what a Salesforce namespace and a static resource are. It also assumes you have read the earlier chapters on namespace isolation and on sandboxes and distortions.
Two answers to the same problem
Lightning Locker and Lightning Web Security (LWS) both stop component code from one namespace from reading or changing the data and DOM of another namespace. They differ in how they do it, and that difference decides which code and libraries work.
- Locker wraps the objects your code touches. When you read
documentor an element, you get a secure wrapper (a proxy object that filters what you can see), not the real object. - LWS runs each namespace's code in its own JavaScript sandbox. Inside the sandbox, your code uses the real standard APIs. LWS applies distortions (small changes to specific APIs) only where isolation requires them.
How the mechanisms differ
Because Locker returns wrappers, an object your code holds is not the same object a library expects. Libraries that check types, compare identities, or walk the DOM often fail in ways that are hard to trace. LWS keeps objects closer to the standard, so more code runs unchanged.
Practical differences
| Area | Lightning Locker | Lightning Web Security |
|---|---|---|
| Isolation unit | Wrapped objects per namespace | JavaScript sandbox per namespace |
| What your code sees | Secure wrappers around DOM and globals | Standard APIs, with distortions where needed |
| Third-party libraries | Often need changes or special builds | More libraries work as shipped |
| Works with | Lightning web components and Aura components | Lightning web components and Aura components |
| Error style | Wrapper-related type or access errors | Distortion or sandbox-related errors |
Both mechanisms still enforce the rule that you query only your own template. The next example works the same way under either one, and it is the recommended pattern.
import { LightningElement } from 'lwc';
export default class FocusInput extends LightningElement {
handleClick() {
// Query only inside this component's own template.
const input = this.template.querySelector('lightning-input');
if (input) {
input.focus();
}
}
}What does not change
Moving to LWS does not remove the isolation rules. You still cannot reach into another namespace's DOM, and global objects you add may be visible only inside your namespace's sandbox. Components in different namespaces still communicate through public @api properties and custom events. LWS makes ordinary code run more often. It does not make cross-namespace access legal.
// child component (any namespace)
import { LightningElement, api } from 'lwc';
export default class ItemPicker extends LightningElement {
@api label;
handleSelect() {
this.dispatchEvent(
new CustomEvent('select', { detail: { id: '001' } })
);
}
}Common errors when comparing the two
- Assuming a Locker workaround is still needed. Code written to avoid wrappers, such as copying objects to plain data before passing them to a library, may now be unnecessary. Keep it until you have tested, then remove it deliberately.
- Assuming LWS allows everything. A library that depends on shared globals across namespaces, or on reaching another namespace's elements, can still fail.
- Testing in one mode only. An org setting decides which mechanism runs. A component that passes in a Locker org may behave differently after the switch, so test in the mode you will ship.
Deciding which to rely on
Choose LWS for new work when your org supports it. It runs more third-party libraries and standard APIs without special handling. If you maintain components that still rely on Locker-specific behavior, keep them working under both until your migration is complete. The migration chapter covers that plan.
Exercise
A library works in your sandbox only when you copy objects to plain data before passing them in. Why did Locker force this, and what should you do after moving to LWS?
Show answer
Locker passes secure wrappers instead of the real objects, and the library may not recognize or handle those wrappers. Under LWS, objects stay closer to the standard, so the copy step may no longer be needed. Test without it in an LWS org, then remove it if the library works. Keep it if the library still fails.
Exercise
Name one thing that stays the same under both Locker and LWS when a component needs to talk to a component in another namespace.
Show answer
Communication goes through public @api properties and custom events. Neither mechanism lets you reach into the other namespace's DOM or globals.