Overview
ThemiscStaticResource component demonstrates how to reference and use static resources in Lightning Web Components, including individual files and files within archive packages.
Source Component
- miscStaticResource - Displays images from static resources
Key Features
- Import static resources by name
- Reference individual files
- Access files within archive packages (ZIP)
- Type-safe resource URLs
Implementation
Importing Static Resources
Import static resources using the@salesforce/resourceUrl module:
Using in Templates
Reference the imported URLs in your template:Static Resource Types
Single Files
For individual files (images, CSS, JS):Archive Files (ZIP)
For files within a ZIP archive:When uploading a ZIP file as a static resource, maintain a clear directory structure. This makes it easier to reference files and organize assets logically.
Creating Static Resources
Via Setup UI
- Go to Setup > Custom Code > Static Resources
- Click New
- Enter a name (e.g.,
company_logo) - Upload your file or ZIP archive
- Set Cache Control to
Public(recommended) - Click Save
Via VS Code
- Create a directory:
force-app/main/default/staticresources/ - Add your file:
company_logo.png - Create a metadata file:
company_logo.resource-meta.xml
- Deploy to your org
Common Use Cases
Images
CSS Files
JavaScript Libraries
Archive Structure Example
For a ZIP file namedtrailhead_characters.zip:
Best Practices
- Use descriptive names for static resources
- Set appropriate cache control settings
- Organize related files in ZIP archives
- Use
loadStyle()andloadScript()fromlightning/platformResourceLoader - Include alt text for images for accessibility
- Compress images to reduce file size
- Use SVG for icons when possible
Static resource names in Salesforce are converted to API names automatically. Spaces become underscores, and special characters are removed. The import name should match the API name.
Cache Control
- Public - Cached by browser and CDN (recommended for most assets)
- Private - Cached by browser only, not by CDN
Size Limits
- Maximum individual file size: 5 MB
- Total static resource storage per org varies by edition
Source Files
force-app/main/default/lwc/miscStaticResource/
