Sub topic 12
Using static resources and custom labels
This chapter assumes you can build a basic Lightning web component and deploy it with the Salesforce CLI. It also assumes you have exposed a component in Experience Builder, as covered in earlier chapters. Here you add two kinds of site assets: files such as images and scripts (static resources), and translatable text (custom labels).
Static resources
A static resource is a file or a zip archive that you upload to your org. Typical contents are images, CSS, and JavaScript libraries. In a Salesforce DX project, each resource lives in force-app/main/default/staticresources as a file plus a .resource-meta.xml file.
You import a static resource by name. The import gives you its URL:
import { LightningElement } from 'lwc';
import BRAND_ASSETS from '@salesforce/resourceUrl/brandAssets';
export default class SiteBanner extends LightningElement {
logoUrl = `${BRAND_ASSETS}/images/logo.png`;
}<template>
<img src={logoUrl} alt="Company logo" />
</template>If the resource is a single file, use the import value directly. If it is a zip archive, append the path of the file inside the archive, as shown above.
Loading scripts and styles
To use a third-party library or a stylesheet from a static resource, use lightning/platformResourceLoader. Load the file once, in renderedCallback, and guard against loading it again:
import { LightningElement } from 'lwc';
import { loadScript, loadStyle } from 'lightning/platformResourceLoader';
import CHART_LIB from '@salesforce/resourceUrl/chartLib';
export default class SiteChart extends LightningElement {
libLoaded = false;
renderedCallback() {
if (this.libLoaded) {
return;
}
this.libLoaded = true;
Promise.all([
loadScript(this, `${CHART_LIB}/chart.min.js`),
loadStyle(this, `${CHART_LIB}/chart.css`)
]).catch((error) => {
console.error('Could not load chart library', error);
});
}
}Custom labels
A custom label is a named text value that you define in Setup under Custom Labels. You can translate each label into other languages, so one component can show the right text for each visitor. Use labels for any text you want to translate or change without redeploying code.
Import a label with the c namespace, or your own namespace if you use a managed package:
import { LightningElement } from 'lwc';
import WELCOME from '@salesforce/label/c.Site_Welcome';
import LOGIN_PROMPT from '@salesforce/label/c.Site_Login_Prompt';
export default class SiteWelcome extends LightningElement {
label = { WELCOME, LOGIN_PROMPT };
}<template>
<h2>{label.WELCOME}</h2>
<p>{label.LOGIN_PROMPT}</p>
</template>Group the imports in one label object. A template can only read properties of the component, so it cannot use the imported constants directly.
Common errors
| Problem | Cause | Fix |
|---|---|---|
| Deploy fails with an unknown resource or label | The resource or label is not in the target org yet | Deploy the static resource or `CustomLabels` metadata before, or together with, the component |
| Image shows as broken | Wrong path inside a zip resource | Check the folder names and letter case inside the archive |
| Library loads twice | `loadScript` runs on every render | Use a flag, as in the example |
| Label text does not change | The site language has no translation | Add a translation, or check the site's language settings |
Exercise
A component imports @salesforce/label/c.Site_Welcome and the template uses {Site_Welcome}, but nothing appears. What is wrong, and how do you fix it?
Show answer
The template can only read properties of the component class, and the imported value is not a property. Assign it in the class, for example label = { Site_Welcome };, and use {label.Site_Welcome} in the template.
Exercise
Why should loadScript be called in renderedCallback behind a flag instead of called without a guard?
Show answer
renderedCallback runs after every render, so an unguarded call would request and run the library again each time. A boolean flag makes it load only once.