Sub topic 12
Troubleshooting LWS errors
This chapter assumes you know how LWS isolates code by namespace, how distortions change standard APIs, and how to load static resources with loadScript. It gives you a repeatable way to find the cause of an LWS failure and decide whether to change your code or replace a library.
Start by confirming LWS is the cause
Many failures that look like security errors have other causes. Before you change code, check three things.
- Open the browser developer console and read the first error, not the last. Later errors are often side effects of the first one.
- Check whether the org has LWS enabled in Session Settings. Behavior differs between Locker and LWS, so a fix for one may not apply to the other.
- Reproduce the problem with the library or component alone. If it fails on its own, the cause is probably not another namespace.
A diagnostic flow
The diagram shows the order to work through. Each step narrows the cause before you touch the code.
Common errors and fixes
The table lists symptoms you are likely to see and the usual remedy.
| Symptom | Likely cause | Fix |
|---|---|---|
| `querySelector` returns `null` | The query runs before render, or targets an element outside the component's template | Query inside `renderedCallback` and only through `this.template` |
| `window.SomeLibrary is undefined` after `loadScript` | The script did not run, or it attached to a different global than you expect | Await `loadScript`, then check the global the library exposes |
| Library works in one org but not another | One org uses Locker and the other uses LWS | Confirm the LWS setting in each org and test in both |
| Script blocked in the console | Content Security Policy does not allow the source | Add the trusted site or serve the file as a static resource |
| Data from another component is missing or stale | Code reads another namespace's DOM or globals | Pass data with `@api` properties and custom events |
Make failures visible
A library that fails silently is hard to debug. Wrap loading and initialization in try/catch and log the error object, so the console shows the cause instead of 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 libraryLoaded flag matters. renderedCallback runs after every render, so without the guard the script loads repeatedly.
DOM queries that fail
Under LWS, a component reaches its own elements through this.template. Using document.querySelector to find elements that belong to your component is unreliable, because the component's DOM is encapsulated.
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();
}
}
}The if (input) check keeps a missing element from turning into a second error that hides the first.
Cross-namespace problems
If two components from different namespaces need to share data, do not read each other's DOM or globals. Each namespace runs in its own sandbox, so a value one sets on a global is not visible to the other. Use a public property to pass data down and a custom event to pass it up.
// 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' } })
);
}
}Change the code or replace the library
Use these criteria to decide.
- Change your code when the error comes from your own DOM access, global use, or cross-namespace calls. These are fixed with
this.template,@api, and events. - Replace or update the library when its own code depends on a global, a DOM API, or a browser feature that LWS restricts. Check the vendor's release notes for a newer version first.
- Isolate the library when only a small part fails. Test whether the feature you use works on its own before abandoning the whole library.
Exercise
A component loads a charting library with loadScript. The page shows an empty canvas and the console shows nothing. What two changes help you find the cause, and where in the code do you make them?
Show answer
Wrap the loadScript call and the library initialization in try/catch and log the error with console.error, so a load or sandbox failure becomes visible. Also guard renderedCallback with a flag so the script loads only once. After that, read the first error in the console, and check whether the stack trace points to the static resource.
Exercise
A component in one namespace sets a value on window and a component in another namespace tries to read it. The second component gets undefined. Why, and what should you use instead?
Show answer
Each namespace runs in its own sandbox, so the global set in one namespace is not shared with the other. Pass the value through an @api property on the receiving component, or send it with a CustomEvent.