Lesson 2 · Unit 02
Caching
Reuse a copy, check a copy, and name files so both are safe
What you practise
Cache-Control, validation withETag, hashed file names cached forever, a CDN near the user
Try a question from this lesson
Pick one
A returning visitor needs logo.png again. Which way of getting it is fastest?
Correct answer
Reuse the stored copy without contacting anyone
Why
Every other option needs at least one round trip to a server, and a round trip costs time even for a tiny answer. A copy the browser already holds and is allowed to trust costs nothing. The whole of caching is about earning that permission.
The ideas
Concept cards are short on purpose. In the app they appear between questions, after you have met the idea.
Concept
Fresh, stale, revalidate
Cache-Control: max-age=N makes a stored response fresh for N seconds. A fresh copy is reused with no request at all.
After that the copy is stale, but not useless. The browser asks the server whether its version is still current, quoting the ETag, a tag the server gave that version. If nothing changed, the server says so in a short reply with no body and the copy is fresh again. Otherwise it sends the new file.
Concept
Names that change with the content
Build tools put a short hash of a file's content into its name. Change one character and the name changes, so the new version is a new URL that no cache has seen.
A hashed file can then be cached for a year and marked immutable: its content can never differ from its name.
The HTML keeps one stable URL and names the current files, so it has to be checked on every visit.
app.js -> app.3f9a.js
edit one line -> app.c81d.js
index.html:
<script src="app.c81d.js">10 more exercises in the app
- 4× multiple choice
- 2× fill the blanks
- 2× type the answer
- 1× match the pairs
- 1× true or false
Bitpath is an iOS app in development. Join the waitlist and you get one email when it is ready to try.