Sub topic 5
Enabling LWS in your org
This chapter assumes you can build a basic Lightning web component (HTML template, JavaScript class, metadata file). It also assumes you know what a namespace is and have used static resources. You should have read the earlier chapters on namespace isolation, sandboxes and distortions, and the comparison with Lightning Locker.
What you are switching
Lightning Web Security (LWS) is a org-wide setting. It does not apply per component. When you turn it on, Lightning web components and Aura components in that org run in LWS sandboxes. Turning it off returns them to Lightning Locker.
Salesforce enables LWS by default in new orgs and sandboxes. In older orgs, an admin has to turn it on. Check your org first, because it may already be on.
Turn on LWS in Setup
You need a user with permission to change session settings, usually a system administrator.
- Open Setup.
- In Quick Find, enter Session Settings and open it.
- Find the Lightning Web Security option. It is labeled "Use Lightning Web Security for Lightning web components and Aura components".
- Select the checkbox.
- Click Save.
Menu labels can change between releases. If you cannot find the option, search Setup for "Lightning Web Security" and check the release notes for your org's version.
Saving the setting does not change code. It changes which security layer the browser applies to your components. Users may need to reload the page, and cached sessions can take a moment to pick up the change.
Enable it in a scratch org
For a scratch org, put the setting in the scratch org definition file so every new org starts with LWS on. Salesforce DX reads the lightningExperienceSettings-style settings from the definition. The session setting for LWS is configured like this:
{
"orgName": "LWS Test Org",
"edition": "Developer",
"settings": {
"securitySettings": {
"sessionSettings": {
"enableLWS": true
}
}
}
}Verify the exact setting name against the Metadata API reference for your API version. Setting names are the most likely thing to differ from what you remember.
Confirm that LWS is active
Do not assume the checkbox worked. Check in the browser.
- Open a page that contains one of your components.
- Open the browser developer tools and go to the Console.
- Look at the stack traces of any errors. Under LWS, errors from sandboxed code mention the LWS runtime rather than Locker's
SecureObjectwrappers. - Compare an object that Locker would wrap. Under Locker,
windowand DOM objects are secure wrappers. Under LWS, they are the real browser objects with distortions applied behind them.
This second difference is the practical test. If a library that checks instanceof on DOM nodes started working, LWS is probably active.
Rollout plan
The following diagram shows a safe order of work.
Follow the same order each time:
- Refresh a full or partial sandbox from production so it has your real components and data.
- Enable LWS there and exercise every page that hosts custom components, including managed package components.
- Fix failures. Chapters later in this tutorial cover DOM access, web APIs, and third-party libraries in detail.
- Enable LWS in production at a time with few active users.
- Watch for errors for several days.
Common errors after enabling
| Symptom | Likely cause | First step |
|---|---|---|
| Component renders blank, console shows a distortion or sandbox error | Code uses a global or API that LWS restricts | Read the error text and find the line that touches the global |
| Static resource library throws on load | Library assumes it can modify browser globals or reach another namespace's objects | Wrap `loadScript` in `try/catch` and check the library's LWS support |
| Managed package component breaks | The package is not compatible with LWS | Contact the package vendor and check the version |
| Setting is on but behavior looks like Locker | Page cached, or the component is in a context that does not use LWS | Hard reload and recheck |
Reverting
The checkbox is reversible. Clear it and save to return to Locker. Treat this as an emergency step, not a long-term plan. Locker is on a path to retirement, so unresolved LWS failures become work you still have to do.
Exercise
Your team wants to enable LWS in production on Friday afternoon. Name two things you should do first, and give the quickest way to undo the change if users report broken pages.
Show answer
First, enable LWS in a sandbox refreshed from production and test every page with custom or managed package components, fixing failures. Second, schedule the production change for a low-traffic period and plan to watch the console and user reports afterward. To undo it, open Session Settings in Setup, clear the Lightning Web Security checkbox, and save. That returns the org to Lightning Locker.
Exercise
After enabling LWS, how can you tell in the browser that it is active, without relying on the Setup checkbox?
Show answer
Open developer tools on a page with your component. Under LWS, DOM objects and window are the real browser objects with distortions behind them, not Locker's secure wrappers. Errors from sandboxed code also reference the LWS runtime. A library that checks instanceof on DOM nodes and previously failed under Locker may now work, which is another sign.