Sub topic 1
Why Component Code Needs Isolation
Prerequisites
This chapter assumes you know the following:
- Basic Lightning web component structure: an HTML template, a JavaScript class, and a metadata file.
- Modern JavaScript, including modules, classes, and async/await.
- The browser's same-origin policy, at the level of "a page cannot freely read data from another origin."
- What a Salesforce namespace and a static resource are.
You do not need to know how Lightning Web Security (LWS) works yet. This chapter explains why it exists. Later chapters cover how it works.
The problem: many authors, one page
A Lightning page is not built by one team. It can mix components from several sources:
- Your own components.
- Components from managed packages installed from AppExchange.
- Components that Salesforce ships.
All of this code runs in the same browser tab and shares the same origin. The browser's same-origin policy separates one website from another. It does not separate one component from another on the same page.
Without extra protection, any component could do the following:
- Read the DOM that belongs to another component, including fields that show customer data.
- Change the global objects (
window,document, built-in prototypes) that every other component relies on. - Read tokens or session data that the page holds.
- Send what it finds to a server the author controls.
Salesforce needs to host code from many authors on one page. It also needs to stop that code from interfering with other code or with the user's data. That need is the reason for component isolation.
What isolation has to protect
Isolation addresses three separate risks.
| Risk | Example | What goes wrong |
|---|---|---|
| Data theft | A component queries another component's DOM and reads a contact's email address. | Confidential data leaves its intended scope. |
| Tampering | A component replaces `Array.prototype.map` or overrides `fetch`. | Other components behave incorrectly or can be spied on. |
| Accidental conflict | Two libraries both define `window.utils`. | One silently overwrites the other and the page breaks. |
The first two risks involve malicious code. The third involves ordinary mistakes. Isolation helps with all three, so it is useful even when you trust every author.
Why the browser alone is not enough
You might expect the browser to solve this. It has several security features, but none fits this case.
- Same-origin policy. It separates origins. All components on a page share one origin, so it does nothing between them.
- Iframes. An iframe gives a strong boundary. But one iframe per component would be slow and hard to lay out. Components could not easily share events or styles either.
- Content Security Policy (CSP). CSP restricts which scripts and resources a page may load. It does not stop code that is already running from reading other code's data. A later chapter covers how CSP and LWS work together.
So the platform adds its own layer inside the page. The browser does not provide this layer. Salesforce implements it in JavaScript.
How Salesforce approaches it
LWS is the current implementation of that layer. At a high level it uses three ideas. This chapter only names them, and the next chapters explain each in detail.
- Isolate by namespace. Code from one namespace runs in its own sandbox, separate from code in other namespaces.
- Alter the environment inside the sandbox. Built-in objects and APIs are adjusted. These adjustments are called distortions. They block or change operations that would break isolation.
- Allow controlled communication. Components in different namespaces cannot reach into each other. They exchange data through public properties and events, which the platform allows.
A concrete illustration
Consider a component that tries to read data it does not own. Under the isolation model, this kind of code is the target:
import { LightningElement } from 'lwc';
export default class Snooper extends LightningElement {
connectedCallback() {
// Reaching outside this component's own template.
const everything = document.querySelectorAll('input');
console.log('Inputs found:', everything.length);
}
}Without isolation, document.querySelectorAll could return inputs that belong to any component on the page. Under LWS, the call is limited. The component sees only elements that its own namespace is allowed to see. The result is not an error that crashes the page. The component simply cannot observe what it does not own.
The supported pattern is to query inside your own template:
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();
}
}
}This is the habit the rest of the tutorial builds on. Work within your own template, and use public properties and events to talk to other components.
What this means for you as a developer
- Isolation is the reason some code that works in a plain web page behaves differently in a Lightning component.
- Most well-written components are unaffected, because they already stay inside their own template.
- Problems appear mainly in code that depends on global state, reaches into other DOM trees, or comes from older libraries that assume unrestricted access.
Summary
Many authors share one origin on a Lightning page. The same-origin policy and CSP do not separate them. Salesforce therefore isolates code inside the page, by namespace, with controlled communication between namespaces. The next chapter shows how LWS builds those namespace boundaries.
Exercise
Why does the browser's same-origin policy not protect one Lightning component from another on the same page?
Show answer
All components on a page run in the same origin, and the same-origin policy only separates different origins. It has no effect between code that shares an origin, so the platform must add its own isolation layer.
Exercise
A component calls document.querySelectorAll('input') to collect values from the whole page. Which approach should it use instead, and why?
Show answer
It should use this.template.querySelector (or querySelectorAll) so it only reads elements in its own template. Isolation stops components from observing DOM they do not own, and to get data from other components it should receive it through public properties or events.