Sub topic 3
Sandboxes and distortions: how LWS changes what code can do
This chapter assumes you can write a basic Lightning web component and know modern JavaScript (classes, modules, async/await). It also assumes you know that LWS isolates code by namespace, which the previous chapter covered. Here you will see the mechanism that enforces that isolation and how it changes what your code can do.
The sandbox model
Lightning Web Security (LWS) gives each namespace its own JavaScript sandbox. A sandbox is an isolated copy of the global environment, including window, document, and the built-in objects your code sees. Your component runs inside the sandbox for its namespace. A component from a different namespace runs in a different sandbox.
Your code still looks like normal JavaScript. You write window.location or document.title as usual. LWS sits between your code and the real browser objects and decides what each call returns.
LWS uses two tools to do this:
- Sandboxed globals. Each namespace gets its own global object. Values you add to
windowin one namespace do not appear in another. - Distortions. A distortion is a replacement for a standard JavaScript or web API. When your code calls the API, LWS runs the distortion instead of, or around, the original behavior.
What a distortion can do
A distortion can change an API in one of three ways:
- Allow it unchanged. Most standard APIs work as you expect.
- Modify it. The API still works, but its behavior is limited or adjusted. For example, an API that would reveal an element outside your namespace's view returns only what your namespace may see.
- Block it. The API throws an error or does nothing, because it would let your code break isolation.
Distortions are why code that works in a plain web page sometimes behaves differently in Salesforce. The browser API is the same, but your code is talking to the sandbox's version of it.
Seeing the sandbox at work
Your own window is a sandboxed object. A global variable you set is visible to your namespace only. This example sets a value and reads it back.
import { LightningElement } from 'lwc';
export default class SandboxProbe extends LightningElement {
connectedCallback() {
// This value lives in the sandbox for this component's namespace.
window.myNamespaceFlag = 'set';
console.log(window.myNamespaceFlag); // 'set'
}
}A component in another namespace that reads window.myNamespaceFlag gets undefined. You cannot use window as a shared channel between namespaces. The next chapters cover the supported alternatives.
Distortions and the DOM
DOM access is the most visible place where distortions apply. Your component can reach elements it owns. It cannot freely reach into elements owned by another namespace. The recommended pattern is to query through this.template, which limits the search to your own rendered content.
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();
}
}
}Avoid document.querySelector for your own elements. It searches the whole page, and the distortion may hide results or return null for content you do not own.
Common errors
These problems usually come from a distortion, not from a bug in your logic.
- A global returns
undefined. You set it in one namespace and read it in another. Pass the value through a public property or an event instead. - A DOM query returns
null. You searched outside your own template. Usethis.template.querySelector. - A library fails only in Salesforce. The library assumes it can change shared globals or reach any element. Check the console for a sandbox-related error before you change your own code.
- Code works in a browser console but not in a component. The console runs against the real page. Your component runs against the sandbox.
When you see an unexpected null, undefined, or a thrown error from a standard API, ask whether a distortion is involved before you assume your logic is wrong.
Summary of the model
| Concept | What it is | Effect on your code |
|---|---|---|
| Sandbox | An isolated global environment per namespace | Globals you set stay in your namespace |
| Distortion | A replacement for a standard API | The API may be allowed, modified, or blocked |
| `this.template` | Your component's own rendered content | The supported scope for DOM queries |
Exercise
A component in namespace acme sets window.selectedId = '001'. A component in namespace globex on the same page reads window.selectedId and gets undefined. Why does this happen, and what is the supported way to share the value?
Show answer
Each namespace runs in its own LWS sandbox with its own global window, so a property set in the acme sandbox does not exist in the globex sandbox. To share the value, pass it through a public @api property or send it in a custom event, which are the supported cross-namespace channels.
Exercise
Why is this.template.querySelector('lightning-input') preferred over document.querySelector('lightning-input') inside a component?
Show answer
this.template.querySelector searches only the component's own rendered content, which is within what the sandbox allows. document.querySelector searches the whole page, and distortions may hide elements owned by other namespaces, so it can return null or unexpected results.