Sub topic 2
How LWS isolates code by namespace
This chapter assumes you can read a Lightning web component (HTML template, JavaScript class, metadata file), that you know modern JavaScript, and that you know the browser's same-origin policy. It also assumes you know what a Salesforce namespace is. It covers the isolation boundary LWS draws. Later chapters cover what you can do inside that boundary.
The problem LWS solves
Every Lightning component on a page runs in the same browser origin. The browser's same-origin policy therefore treats all of them as one trusted party. Without extra protection, a component from one package could read the DOM of another, overwrite its globals, or call its internal functions.
LWS adds a boundary the browser does not provide. It uses the component's namespace to decide which code is trusted together.
One sandbox per namespace
A namespace is a prefix that identifies who owns a component. Your own org's custom components use c. Managed packages use their own namespace, and Salesforce's base components use lightning.
LWS gives each namespace its own JavaScript sandbox. A sandbox is an isolated copy of the JavaScript environment, including its own global objects such as window. The rules are:
- Components in the same namespace share one sandbox and can see the same globals.
- Components in different namespaces run in different sandboxes and cannot see each other's globals.
- Values that cross a boundary go through a wrapper that LWS controls. Raw references to another namespace's objects are not handed out.
LWS draws the line at the namespace, not at the individual component. Two c components can share state through a global. A c component and a managed-package component cannot.
What isolation looks like in code
The clearest way to see the boundary is with a global variable. Suppose a component in the c namespace stores a value on window.
import { LightningElement } from 'lwc';
export default class GlobalWriter extends LightningElement {
connectedCallback() {
// Stored on this namespace's own copy of window.
window.sharedSetting = 'compact';
}
}A second c component reads the value:
import { LightningElement } from 'lwc';
export default class GlobalReader extends LightningElement {
connectedCallback() {
// Same namespace, same sandbox: prints "compact".
console.log('Same namespace:', window.sharedSetting);
}
}A component in a managed package namespace that runs the same console.log prints undefined. It is looking at its own window, not the one that c wrote to.
The same rule applies in reverse. A package cannot change the globals your components see. A change a package makes to a built-in, such as a patched Array.prototype method, stays inside the package's sandbox.
How this connects to the DOM
Namespace isolation also limits DOM access. Inside a component, you query your own rendered elements through this.template:
import { LightningElement } from 'lwc';
export default class FocusInput extends LightningElement {
handleClick() {
const input = this.template.querySelector('lightning-input');
if (input) {
input.focus();
}
}
}this.template.querySelector finds elements inside your own template. A global lookup such as document.querySelector is a different matter, and the DOM access chapter explains why.
Common mistakes
- Using a global as shared state across namespaces. It works in a
c-only org and fails once a managed package component needs the value. Pass the value as a public property or send it in an event. - Assuming a library is visible to everyone. A library you load into the
csandbox withloadScriptis not available to a package component. Each namespace loads its own copy. - Testing in one namespace only. Code that works in a scratch org with only
ccomponents can break when a package supplies a child component.
Exercise
Component A (namespace c) sets window.theme = 'dark'. Component B is also in namespace c. Component C comes from a managed package with namespace acme. What does each of B and C see when they read window.theme, and why?
Show answer
B sees 'dark', because B and A are in the same namespace and share one sandbox and one window. C sees undefined, because the acme namespace has its own sandbox with its own copy of window. To share the theme with C, A should pass it through a public @api property or a custom event.