question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Implement cache revalidation

See original GitHub issue

Today, cacheable, unchanged resources are not stored in the browser cache - regardless of the Cache-Control response headers that are applied in Workers. In other words, it’s impossible to get a 304 Not Modified response (it is, however, possible to get Chrome to return the resource from memory cache).

There are two reasons for this:

  1. Resources stored in Cache API do not include a Last-Modified or Etag response header
  2. The cacheKey tied to each resource would strip the If-Modified-Since or If-None-Match header before querying the cache API (right now, the browser wouldn’t even send these headers because of the first reason)

Solutions Coming soon

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Reactions:6
  • Comments:12 (8 by maintainers)

github_iconTop GitHub Comments

5reactions
shagamemnoncommented, Feb 15, 2020

Thanks @budparr! Appreciate the input.

I do think that the options parameter should support etag generation. As @victoriabernard92 mentioned (and you observed) cache.match() already supports conditional revalidation, permitted an etag is provided by an origin server.

A solution here needs to consider the following:

  • There needs to be a collision-resistant digest created for every version of file (e.g. the etag)
  • On cache insertion, two objects will need to be created – the 304 Not Modified response and the 200 OK response with the cache body
  • We need to choose whether the etags should be strong or weak
  • Resources will need to have a different etag for each content-encoding, assuming strong etags are used

Here is my plan:

  • Workers supports the WebCrypto API, which includes a digest() method that can be used to hash the actual content bodies. So we can kill two birds with one stone here by hashing the response body contents upon cache insertion like digest('SHA-1', '<html>...'). This ensures a new version of a file will always have a different etag than its predecessor
  • The etag will need to be dropped in scenarios where the size of the file being inserted into cache is too large for Workers to compute in the 5ms CPU limit. I’m estimating that this limit will be somewhere between 2MB and 6MB
  • If an etag is sent from an origin server, a new etag will not be generated in the Worker
  • Strong e-tags will be used for two reasons: 1) Cache API allows (and encourages) the creation of unique objects that are derived (but not identical to) the origin response and 2) we want to support Range-Requests. Range Requests aren’t fulfilled when the etag is weak
  • Each content tag will be in the format SHA1(HASH-OF-BODY)-{content-encoding} such that content-encoding differences are handled properly
3reactions
shagamemnoncommented, Apr 27, 2020

Hey @EatonZ - thanks for flagging this. It got de-prioritized over the last month or so.

I have a preliminary solution complete but need to talk to @EverlastingBugstopper because it does not make use of the wrangler-generated manifest. Should be able to make a PR by end of week. With respect to your comment about CPU: of course, those are the only considerations here 😃

Read more comments on GitHub >

github_iconTop Results From Across the Web

Keeping things fresh with stale-while-revalidate - web.dev
stale-while-revalidate helps developers balance between immediacy—loading cached content right away—and freshness—ensuring updates to the cached ...
Read more >
Cache Control Headers and Revalidation - Traffic Control
When a response is expired it usually doesn't get deleted from the cache, but, when a request comes in that would have hit...
Read more >
Stale-while-revalidate Data Fetching with React Hooks: A Guide
A stale cache is a cache that is not suitable for use as it contains outdated data. In the context of HTTP, this...
Read more >
UX Patterns: Stale-While-Revalidate - InfoQ
stale-while-revalidate helps developers balance between immediacy—loading cached content right away—and freshness—ensuring updates to the cached ...
Read more >
Staleness and revalidation - Fastly Developer Hub
Background revalidation process flow. When a cache lookup results in a stale object that is within a stale-while-revalidate period (scenario 2 in the...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found