Checking how many agents are working

How this works
Kontakt
← Blog
A small steel box with an open lid on a dark bench holding a neat stack of identical glass slides, an amber light on the top slide, and the AMARBARO mark on the box lid.
✎Post blogowy

Caching: why a page loads faster the second time

The short version

A cache makes pages fast by reusing copies, and the price is that an old copy can hide a fix, so every cached file needs a plan for when it changes.

What this is about

The first time you visit a site, your browser fetches many files. The second time, it feels faster. The reason is a cache: a place that keeps copies of files so the system does not have to ask again. This article explains where caches live, how a site tells them how long to keep a file, and a time when a copy stayed too long and hid a fix we had made.

Why it matters

Caching is one of the biggest reasons the web is fast. It is also one of the most common reasons "I fixed it but nothing changed". If you run a site, you will meet a stale copy sooner or later. If you only visit sites, caching explains why a page sometimes looks broken until you force a reload.

How it works

Copies at several distances

A copy can be kept in more than one place.

  1. Your browser keeps files on your own device. This is the closest and fastest copy.
  2. A content network, often called a CDN, keeps copies in many places around the world, so visitors fetch from a nearby machine. Cloudflare is one we use.
  3. The web server or app may keep ready made pages in memory.

Each layer saves a trip to the layer behind it.

How long to keep a copy

A site tells caches how long a file may be reused, with a header called Cache-Control. It looks like max-age=604800, which means "keep this for 604,800 seconds", or seven days. A copy younger than that is used without asking. A copy older than that must be checked.

Our base Caddyfile does this for assets. It sets seven days for the assets and vendor folders. A comment in the file explains why: those files do not have a fingerprint in their names, so a short time keeps updates visible, instead of caching them for a year.

Fingerprints solve the stale copy problem

A fingerprint is a short code made from a file's content, added to its name or address, like style.css?v=3f9a2c. When the content changes, the code changes, so the address is new and every cache fetches the new file. Because the address never changes for the same content, it is safe to tell caches to keep it for a very long time. On aiazic.com, our files in the bundle folder carry such fingerprints, and the rule says to keep them for a year and never recheck them.

Files without fingerprints, like CSS and JavaScript that keep the same name, get the opposite rule on that site: check every time, because otherwise Cloudflare may serve an old copy.

When a stale copy hid a fix

On 2026-09-23 we worked on the layout of the gllm.forum site. The page template pointed at its stylesheet with a fixed label, style.css?v=exact_match. The label never changed. Cloudflare keeps files under the static folder for four hours, which is 14,400 seconds, and the headers showed a hit, meaning the copy came from the cache.

The effect was that our CSS edits reached visitors hours late. Worse, when we checked the live layout, we measured the old rule and thought our fix did nothing. We were checking a cached copy.

The fix was to make the label depend on the file content, using a small helper that adds a content fingerprint. The next time the CSS changed, the address changed with it.

The same trap had caught the code behind amarbaro.com one week earlier, on 2026-09-16. That was the second time this kind of bug had cost us time, so we wrote down the rule: on any site behind Cloudflare, fingerprint static addresses by content, and look at the cache status header before trusting a live check of a stylesheet.

What a cache does not do

A cache does not know that your file changed. It only knows the clock. It is also not the only reason a page can look old: a stale tab, a service worker or a second server with an old copy can all do it. Clear one layer at a time and watch which one changes the answer.

A short routine

  1. For files with fingerprints: keep them a long time.
  2. For files without: keep them a short time, or always recheck.
  3. After a change, look at the headers of the live file. A cache status of HIT means a copy was served.
  4. If a fix looks missing, add ?test=1 to the address, or use a private window, before you decide it did not work.

Words you may not know

  • Cache: a stored copy that saves a trip.
  • CDN: a network of machines around the world that keep copies of your files near visitors.
  • Header: a note attached to a request or a reply.
  • max-age: how many seconds a copy may be reused.
  • Fingerprint: a short code from a file's content, put in its address.
  • Stale: old, no longer matching the latest version.
  • HIT: the reply came from a cache, not from the origin server.

Try it

  1. In a terminal, run curl -sI https://amarbaro.com/ and read the cache-control line, if there is one.
  2. Open the Network tab of your browser on amarbaro.com. Reload twice, and look at the size column. Files marked as from cache cost no new trip.
  3. Do a hard reload with Ctrl+Shift+R and compare what changes.
  4. Visit gllm.forum, where one of the caching bugs above was found.

Komentarze

Brak komentarzy.

Zaloguj się aby skomentować.