# How do you integrate third-party JavaScript libraries into LWC using platformResourceLoader?

**Topic:** LWC · **Difficulty:** Medium · **Tags:** LWC, Static Resources, Third-Party Libraries

Read it on the site: https://www.forcechallenges.com/interview-questions/how-do-you-integrate-third-party-javascript-libraries-into-lwc-using-platformresourceloader

## Answer

**Basics:** Third-party code (Chart.js, D3, moment, custom CSS) cannot just be `<script>`-tagged in LWC. You upload it as a static resource, import its URL with `@salesforce/resourceUrl/name`, and load it from JavaScript.

**How it works:** Import `loadScript`, `loadStyle` and the resource. Call them with `this` and the full URL (for a zip resource, append the file path). They return Promises; usually run them from `renderedCallback` with a guard flag so they run once, then initialize the library (for a chart, onto a `canvas` obtained from `this.template.querySelector`). Use `lwc:dom="manual"` on a container when the library injects its own DOM.

**Advanced / deep dive:** A script is loaded once per page per URL, so repeated component instances reuse it. Libraries must work under the security layer: Locker blocked many; Lightning Web Security is far more compatible, but test each library. Content Security Policy blocks arbitrary external CDNs, so use static resources (or CSP Trusted Sites where allowed). Static resources are limited (about 5 MB each and 250 MB per org; verify in current release notes), so use minified builds and zip files. Cache-busting follows resource versioning, and public caching of static resources applies. Use `Promise.all([loadScript(...), loadStyle(...)])` to parallelize. In Jest, the module is stubbed to resolve immediately, but global library objects must be mocked. For heavy libraries, load lazily only when the feature is shown. Prefer native base components or SVG where possible.

**Code / config example:**
```javascript
import { LightningElement } from 'lwc';
import { loadScript, loadStyle } from 'lightning/platformResourceLoader';
import CHARTJS from '@salesforce/resourceUrl/chartjs';

export default class RevenueChart extends LightningElement {
    loaded = false;
    chart;

    async renderedCallback() {
        if (this.loaded) return;
        this.loaded = true;
        try {
            await Promise.all([loadScript(this, CHARTJS + '/chart.umd.js')]);
            const ctx = this.template.querySelector('canvas').getContext('2d');
            this.chart = new window.Chart(ctx, { type: 'bar', data: { labels: ['Q1'], datasets: [{ data: [10] }] } });
        } catch (e) {
            console.error('Chart load failed', e);
        }
    }
}
```

**Hidden gotchas & must-remember points:**
- Without a guard flag, `renderedCallback` reloads on every render.
- The library attaches to `window`; access it through `window` (or the sandboxed global).
- Static resource caching can show old code after upload; version the file name.
- External CDN URLs are blocked by CSP unless trusted.
- Not every library works under Locker; LWS is more permissive.
- Do not forget error handling; a failed load otherwise leaves a blank component.

**Best practice:** Bundle a minified, versioned library as a static resource, load it once in `renderedCallback`, and destroy chart instances in `disconnectedCallback`.

---
Source: ForceChallenges · last updated 2026-11-02
