> ## Documentation Index
> Fetch the complete documentation index at: https://docs.median.co/llms.txt
> Use this file to discover all available pages before exploring further.

# Lightning Web Components and the Median JavaScript Bridge

> Access the JavaScript Bridge from Lightning Web Components under Lightning Web Security, set a mobile-aware user agent, and choose a runtime.

Lightning Web Components (LWC) run inside a sandboxed JavaScript environment that can prevent access to globally injected objects like `window.median`. This guide explains how the sandbox works, how to work around its constraints reliably, and how to configure your Salesforce org and Median app for correct mobile rendering.

This is part two of a four-part series:

1. [Getting started with Salesforce Experience Cloud](https://docs.median.co/docs/getting-started-with-salesforce-experience-cloud.md)
2. **Lightning Web Components and the JavaScript Bridge** (this page)
3. [Deep linking for Salesforce Experience Cloud](https://docs.median.co/docs/salesforce-deep-linking.md)
4. [Salesforce LWC component deployment and reference](https://docs.median.co/docs/salesforce-lwc-components.md)

## Step 4: Resolve Lightning Web Security sandbox constraints

Lightning Web Components run inside a sandboxed JavaScript environment called Lightning Web Security (LWS), or its predecessor Lightning Locker. This sandbox prevents components from accessing global variables outside their own namespace.

The practical effect for Median integrations is that a naive call to `window.median` inside an LWC will return `undefined`, even when the bridge has been injected correctly by the native shell.

### Recommended pattern for bridge access

Always access the bridge through the top-level window reference rather than the component's local `window` reference:

Median JavaScript Bridge

```javascript JavaScript theme={null}
get _median() {
    return window.median || window.Median || null;
}
```

Both `window.median` and `window.Median` are included to handle casing differences across Median SDK versions.

### Checking for LWS vs legacy locker

LWS provides broader support for cross-window communication than the legacy Locker Service and is the preferred sandbox for new orgs. To confirm which sandbox your org is using, navigate to **Setup > Session Settings** and look for the **Lightning Web Security** toggle. Enable LWS if it is not already active and your org supports it.

### Detecting whether the page is running inside the Median app

Always guard bridge calls with a presence check before invoking any native feature. This ensures the component degrades gracefully when loaded in a desktop browser, Experience Builder preview, or any context outside the Median native shell.

Median JavaScript Bridge

```javascript JavaScript theme={null}
get isInMedianApp() {
    return !!this._median;
}
```

Use this getter to disable bridge-dependent UI controls and display an informational banner in non-app contexts. Reference implementations are available in the [open-source component library](https://github.com/gonativeio/median-salesforce-components).

### Troubleshooting: `window.median` is undefined inside an LWC

If the bridge is not reachable after applying the pattern above, verify the following:

1. LWS is enabled in **Setup > Session Settings**.
2. The CSP is set to Relaxed in **Experience Builder > Settings > Security and Privacy** (see [Getting started with Salesforce Experience Cloud](https://docs.median.co/docs/getting-started-with-salesforce-experience-cloud.md)).
3. You are accessing the bridge via `window.median` using the `get _median()` accessor pattern, not a local `window` reference.
4. You are testing on a physical device inside the Median app, not in Experience Builder preview, which does not inject the bridge.

## Step 5: Configure a mobile-aware user agent

Salesforce detects the browser type from the User Agent string to determine which resources to serve. If the Median app sends a generic WebView User Agent, Salesforce may serve the desktop layout of your Experience Cloud site, which produces a degraded mobile experience.

**To configure a custom User Agent in Median App Studio:**

1. In your app settings, navigate to **Web Overrides > User Agent**.
2. Append a custom identifier such as `median` to the existing User Agent string. Do not replace the full User Agent string, as Salesforce uses parts of it to identify the browser engine.

![User Agent settings under Web Overrides in App Studio with a custom median identifier appended](https://mintcdn.com/median-co/TpSItjrYpkPK-i0N/images/docs/c42f46feeee78879673f6e45d1c5454a8a83d601e2e818d17178dae08b925ef4-Screenshot_2026-07-21_at_07.02.49.png?fit=max\&auto=format\&n=TpSItjrYpkPK-i0N\&q=85\&s=63726f720fb7ec4074e2266e8b8145f4)

App Studio: user agent configuration

**To apply mobile-specific overrides in Salesforce based on the User Agent:**

Once the custom string is in place, you can target it with Custom CSS in Median App Studio or with conditional logic in your components:

Median JavaScript Bridge

```javascript JavaScript theme={null}
// Detect the Median app from the User Agent in JavaScript
const isMedianApp = navigator.userAgent.includes('median');
```

This technique is useful for hiding elements that are not relevant on mobile, adjusting layout, or showing mobile-specific calls to action.

### Troubleshooting: the site loads in desktop layout inside the app

If your site renders the desktop layout inside the Median app, the User Agent string is not being recognized as a mobile browser. Append a mobile identifier to the User Agent in Median App Studio as described above, and verify that your Experience Cloud site has responsive templates or mobile-specific page variants configured.

## Step 6: Choose your Experience Cloud runtime

When building or migrating your Experience Cloud site, prefer **LWR (Lightning Web Runtime)** over the legacy **Aura** framework where possible.

LWR is built on web standards and delivers significantly faster page load times inside a mobile WebView. Faster initial load is particularly important for native app experiences because users have a higher expectation of performance compared to a mobile browser tab.

If your site currently runs on Aura and a full migration is not feasible, Median works with both runtimes. However, initial load performance and JavaScript execution behavior differ between them, so test thoroughly on physical devices rather than relying solely on desktop browser previews.


## Related topics

- [Salesforce LWC component deployment and reference](https://docs.median.co/docs/salesforce-lwc-components.md)
- [Salesforce overview](https://docs.median.co/docs/salesforce.md)
- [Deep linking for Salesforce Experience Cloud](https://docs.median.co/docs/salesforce-deep-linking.md)


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.