nextstrain / nextstrain.org

The Nextstrain website
https://nextstrain.org
GNU Affero General Public License v3.0
87 stars 49 forks source link

Add Cache-Control header to Charon API endpoints #707

Closed tsibley closed 5 months ago

tsibley commented 10 months ago

Current responses are caching-eligible but don't define acceptable cache behaviour via Cache-Control. This leaves the decision up to browsers' heuristics and can cause understandable user confusion if they upload a new dataset but don't see that reflected in their browser because it's got a fresh cache entry for the /charon/getDataset request. This happened recently to Katie.

We should tell browsers what fresh means to us using Cache-Control.

tsibley commented 10 months ago

I think our best option is likely:

Cache-Control: no-cache

which permits caching by browsers (like now) but instructs them to always check for updates (revalidate) before using the cached response (unlike now). This doesn't save a request/response cycle, but it does save response transfer time when a cached response can be used. It also forbids using the cached response when the browser is offline, though I'm not sure we care about that use case (and not sure other bits of nextstrain.org support it, like the Auspice client).

Alternatively, we could use something like:

Cache-Control: max-age=0, stale-while-revalidate=300

to permit using a cached response for up to 5 minutes (300s) while the browser tries to revalidate it. The wouldn't save a request/response cycle, but it would move it out of the foreground and into the background for probably marginally-faster data load times compared to the first option. It also means users might have to reload several times before they saw an update to the cached copy. This supports offline use cases, but again, that may not matter.

tsibley commented 10 months ago

Also, Auspice could, either instead of or additionally to the above, make the requests to /charon/getDataset with Cache-Control: no-cache.

jameshadfield commented 10 months ago

Cache-Control: no-cache for charon responses seems good to me

corneliusroemer commented 5 months ago

Bumping this as I independently thought about it not realizing there's already is an issue! https://bedfordlab.slack.com/archives/C01LCTT7JNN/p1705507737089689 I think any of the options proposed are better than the current state - we could iterate if necessary