What is caching?
Caching is why your second visit to a site is faster than the first, and why you sometimes see yesterday's version. The layers, the expiry question, and how to cache without serving stale data.

Key takeaways
- A cache stores a copy of something expensive to fetch or compute so the next request is served faster.
- Web apps cache at several layers: the browser, the CDN, the app's data layer and sometimes the server.
- Every cache needs a rule for when its copy goes stale; fingerprinted file names and short lifetimes for live data handle most cases.
At its core, caching means keeping a copy of something that is slow or expensive to get (a file, a database result, a computed answer) somewhere faster, so the next request can use the copy instead of doing the work again. It is the reason a site loads faster on your second visit, and also the reason you sometimes see an old version right after an update.
An everyday example: the carpenter's apron
A carpenter keeps the toolbox across the room but carries a hammer, a pencil and a tape measure in an apron pocket. Ninety percent of the time, the needed tool is already at hand. Occasionally the pocket holds the wrong size screwdriver and a trip is unavoidable. That is a cache: a small, fast store of the things used most, backed by a bigger, slower source. A request served from the cache is a hit; one that has to go to the source is a miss.
The layers of caching in a web app
- 01Browser cache: scripts, styles, fonts and images saved on the visitor's device. Controlled by the `Cache-Control` header the server sends.
- 02CDN cache: copies of your files on servers near each visitor. See what is a CDN.
- 03App data cache: the app remembers results it already fetched. Open a product, go back, open it again, and it appears instantly while a fresh copy loads quietly. Libraries like React Query do this in React and React Native apps.
- 04Server or database cache: expensive results (a leaderboard, a daily report) computed once and stored, often in a key-value store, for a minute or an hour.
How long to cache: the hard part
There is an old programming joke that the two hardest problems are naming things and cache invalidation. The question is always "when is the copy too old?" Reasonable starting points:
- Fingerprinted build files (names like `index-4f8a2c.js`): cache for a year. Each new build creates new names, so visitors never get a stale script. Vite does this by default.
- The HTML page itself: cache briefly or revalidate on each visit, because it points at the latest file names.
- A global leaderboard: 30 to 60 seconds is usually fine and saves thousands of identical queries.
- A user's own cart, balance or messages: do not serve stale data. Refetch on focus, or update the cache the moment the user changes something.
Why it matters when building with Startbase
Some caching is handled for you. Startbase builds a snapshot of your project when you publish, and the live site only changes when you republish; while a republish builds, the current version stays live (publishing docs). Web projects start from Vite, which fingerprints file names, and mobile projects start from Expo with React Query included (Expo template docs). The decisions left to you are about your data: which screens can show slightly old information and which must always be fresh.
The home screen refetches everything every time the user opens it. Cache the product catalog for 10 minutes and show the cached list instantly, but always refetch the cart and stock levels when the screen opens. After the user adds to cart, update the cart badge immediately without waiting for a refetch.
For a related pattern, see realtime features explained, which pushes fresh data instead of waiting for a cache to expire.
Common confusions
- "Clear your cache" is a symptom, not a fix. If users must clear caches to see updates, the cache rules need changing.
- A cache is not storage. It can be emptied at any time; the real data must live somewhere permanent.
- Caching private data in shared caches is a leak. Responses for signed-in users must never be stored by a CDN and served to someone else.
Frequently asked questions
Why do I see the old version of my site after publishing?
Usually the browser kept the old HTML page. A hard refresh fetches the new one. If it keeps happening for visitors, the HTML is being cached too long.
Does caching make data unsafe?
Not by itself. The risk is caching a signed-in user's response somewhere shared. Keep personal responses out of shared caches and let only the user's own device store them.

