Sub topic 11
Migrating from Locker to LWS
This chapter assumes you know basic Lightning web component structure, modern JavaScript, and what a namespace sandbox is. It also assumes you have read the earlier chapters on how Lightning Locker and Lightning Web Security (LWS) differ and how to enable LWS. The goal here is a safe, staged migration plan.
Why migration is a process, not a switch
Enabling LWS is a single org setting, but it changes how every Lightning web component and Aura component in the org runs. Locker wraps objects in secure wrappers. LWS instead runs each namespace's code in a sandbox and applies distortions to a small set of APIs. Code that worked around Locker's wrappers, or that relied on Locker-specific behavior, can behave differently.
For that reason, treat migration as testing work. You are checking that existing behavior still holds. You are not rewriting everything.
A migration plan
The stages below move from lowest to highest risk.
- Inventory what runs in the org. List custom components, managed package components, and static resource libraries.
- Create or refresh a sandbox org. Never trial the change first in production.
- Enable LWS in the sandbox.
- Test the key user flows and record every console error.
- Fix or replace code that fails.
- Enable LWS in production during a low-traffic window, and keep a rollback plan.
What to look for while testing
Most problems fall into a few groups.
| Area | What to check |
|---|---|
| DOM access | Code that reaches outside `this.template`, or uses `document.querySelector` to find elements it does not own |
| Third-party libraries | Libraries loaded with `loadScript` that assume global access or patch built-in objects |
| Cross-namespace code | Components that read another namespace's DOM or globals instead of using `@api` properties and events |
| Locker-specific workarounds | Code written only to satisfy Locker wrappers, such as unwrapping objects |
| Managed packages | Components from vendors that may not yet support LWS |
Fix DOM access first
The most common fix is to limit DOM queries to your own template. This works under both Locker and LWS, so you can make the change before you switch.
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();
}
}
}Make library failures visible
Libraries are the next most likely source of trouble. Wrap loading and initialization in try/catch so a sandbox failure shows up in the console instead of leaving a blank component.
import { LightningElement } from 'lwc';
import { loadScript } from 'lightning/platformResourceLoader';
import CHART_JS from '@salesforce/resourceUrl/chartjs';
export default class ChartHost extends LightningElement {
libraryLoaded = false;
async renderedCallback() {
if (this.libraryLoaded) {
return;
}
this.libraryLoaded = true;
try {
await loadScript(this, CHART_JS);
const canvas = this.template.querySelector('canvas');
this.chart = new window.Chart(canvas, {
type: 'bar',
data: { labels: ['A'], datasets: [{ data: [1] }] }
});
} catch (error) {
// Surface sandbox or load failures instead of failing silently.
console.error('Chart library failed to load or initialize', error);
}
}
}The library runs in the same namespace sandbox as the component. If it fails, first check whether a newer version supports LWS. Change your own code only if the library cannot be updated.
Replace cross-namespace reach-ins
If a component reads another namespace's elements directly, move to the supported contract: public @api properties in, custom events out.
// 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 mistakes
- Testing only in a developer's own sandbox view. Test with the profiles and permission sets real users have.
- Fixing a failing library by editing its internals. Prefer a version that supports LWS.
- Ignoring managed packages. Check each vendor's LWS support before production.
- Treating a clean console on one page as proof. Exercise every page and flow that loads the components.
Exercises
Exercise
Why should you enable LWS in a sandbox first, and what do you do with the console errors you find?
Show answer
LWS changes how all components in the org run, so a sandbox lets you find breakage without affecting users. Each console error points to code to fix, such as DOM queries outside this.template, a library that needs an update, or a cross-namespace reach-in that should become an @api property or event. Fix them, retest, and enable LWS in production only when the key flows run cleanly.
Exercise
A component reads a child element from another namespace with document.querySelector and breaks under LWS. What is the supported replacement?
Show answer
Stop reaching into the other namespace's DOM. Pass data in through a public @api property on the child, and receive results through a CustomEvent that the child dispatches.