Sub topic 2
Why Cached Data Goes Stale
This chapter assumes you can wire a cacheable Apex method into a component, read @AuraEnabled methods and basic SOQL, and use promises with async/await. Here you learn why a wired component can keep showing old data after the records have changed. The next chapters fix it with refreshApex.
What the cache does
A method marked @AuraEnabled(cacheable=true) lets the platform store its result on the client. When a component wires the method, the wire service looks for a cached result that matches the method and its parameters. If it finds one, the component gets that value without waiting for a server round trip. The result is faster rendering and fewer server calls.
The cache is keyed by the method and its parameter values. Two components that wire getContacts with the same accountId can share one cached entry. Different accountId values get separate entries.
The cache has no knowledge of your data model. It stores a result, not a query that watches records. Nothing tells it that a Contact row changed.
How data becomes stale
Data is stale when the cached result no longer matches the database. These situations cause it:
- Your own component saves a change through a separate Apex method, so the wired list still holds the old values.
- A different component on the page updates the same records.
- Another user, an automation, or an integration changes the records after your component loaded.
- A record is created or deleted, so the cached list has a missing or extra row.
The first case surprises developers most, because the change comes from your own code. The save is a separate server call, and the wire does not watch it.
import { LightningElement, api, wire } from 'lwc';
import getContacts from '@salesforce/apex/ContactController.getContacts';
import updateEmail from '@salesforce/apex/ContactController.updateEmail';
export default class StaleContactList extends LightningElement {
@api recordId;
@wire(getContacts, { accountId: '$recordId' })
contacts;
async handleSave(event) {
const { contactId, email } = event.detail;
// The email is saved in the database.
await updateEmail({ contactId, email });
// Nothing here tells the wire to fetch again,
// so this.contacts still holds the old email.
}
}After handleSave finishes, the database has the new email. The contacts property still holds the value the wire delivered earlier. The template keeps rendering the old email until something forces a new fetch.
The flow of events
The diagram shows the sequence for the component above.
What is not a bug
Stale data here is not a defect in your wire or your Apex. It follows from two design choices:
- A cacheable method must be read-only. The platform needs that guarantee to reuse a result safely. A later chapter covers why updates need a separate non-cacheable method.
- The cache and the save are independent. The wire service cannot infer which cached results depend on the rows you changed.
Because the platform does not know when your data changed, you have to say so. refreshApex is the way to ask the wire to discard its cached value and fetch again.
Quick checks for staleness
When a screen shows old values, check these before you change code:
- Is the Apex method
cacheable=trueand called through@wire? If you call it imperatively, the cache does not apply in the same way. - Did the change happen outside the wire, such as through another method or another user?
- Does a full page reload show the new value? If it does, the server data is correct and the problem is the client cache.
Exercise
A component wires a cacheable method getContacts. A button calls a separate non-cacheable method that updates a contact's email, and the save succeeds. The list still shows the old email, and a browser reload shows the new one. Why does the list show the old value, and what does the reload tell you?
Show answer
The wire service holds a cached result for getContacts, and the save is a separate call that does not notify it, so the component keeps the old value. The reload shows the database has the new email, so the data is correct on the server and only the client cache is stale.