Sub topic 16
Performance and caching
This chapter assumes you can build a Lightning web component that calls Apex with @wire, as in the earlier chapters on data access and Apex controllers. It covers how to keep site pages fast by reducing server calls and controlling the cache.
Where the time goes
On a site page, most delay comes from three places:
- Round trips to the server for Apex calls.
- Large queries that return more data than the page shows.
- Too many components that each request their own data on load.
Visitors on mobile networks feel each of these, so fix the number and size of calls first.
Cacheable Apex
Marking an Apex method @AuraEnabled(cacheable=true) lets the platform store its result on the client. A later call with the same parameters can be answered from the cache without a server trip. The getMyCases method from the Apex chapter does this:
public with sharing class CaseListController {
@AuraEnabled(cacheable=true)
public static List<Case> getMyCases(Integer maxRows) {
return [
SELECT Id, CaseNumber, Subject, Status
FROM Case
WITH USER_MODE
ORDER BY CreatedDate DESC
LIMIT :maxRows
];
}
}A cacheable method cannot perform DML (insert, update, or delete). It is also the only kind of Apex method that @wire can call. Use cacheable methods for reads and non-cacheable methods for writes.
Refreshing stale data
Cached data can be out of date after the user changes a record. Call refreshApex with the wired property to discard the cached result and fetch it again:
import { LightningElement, api, wire } from 'lwc';
import { refreshApex } from '@salesforce/apex';
import getMyCases from '@salesforce/apex/CaseListController.getMyCases';
export default class CaseList extends LightningElement {
@api maxRows = 5;
@wire(getMyCases, { maxRows: '$maxRows' })
cases;
handleRefresh() {
return refreshApex(this.cases);
}
}Pass the whole wired property (this.cases), not this.cases.data. If you destructure the wire result into separate fields, keep a reference to the full result so you can pass it to refreshApex.
Keep queries small
A cached call still pays full price the first time. Reduce that cost in the controller:
- Select only the fields the template uses.
- Always use
LIMIT, and consider cappingmaxRowsin Apex so a page editor can't request thousands of rows. - Filter in SOQL rather than in JavaScript.
- Avoid queries inside loops.
Keep pages light
- Load data only when a component is visible. Use
lwc:ifto render a component only after the user opens a tab or section, so its wire adapters don't run earlier. - Combine data needs where it makes sense. Several components that each call a different Apex method cost several round trips.
- Prefer a reactive parameter such as
'$maxRows'to calling Apex repeatedly by hand. The wire service reruns only when the value changes. - Keep images and large files in static resources and size them for the page.
Common mistakes
| Mistake | Effect | Fix |
|---|---|---|
| Using a non-cacheable method with `@wire` | Deployment or runtime error | Add `cacheable=true`, or call the method imperatively |
| Expecting new data after a save | Component shows old results | Call `refreshApex` |
| Selecting every field | Slow queries and large payloads | Select only needed fields |
| No `LIMIT` | Unbounded result size | Apply a limit |
Exercise
A component uses a wired, cacheable Apex method to show cases. A user closes a case in another component, but the list still shows it as open. What should you add to fix this, and what do you pass to it?
Show answer
Call refreshApex (imported from @salesforce/apex) after the change, passing the wired property itself, for example refreshApex(this.cases). This clears the cached result and requests fresh data from the server.