Home > Software design >  Does CacheStorage (Web Cache) API care about headers like Content-Encoding?
Does CacheStorage (Web Cache) API care about headers like Content-Encoding?

Time:10-02

I am using the newly added CacheStorage API in Deno 1.26. My understanding is that this basically functions as a Map<Request, Response>. I question what aspects of the Request object factor into determining whether or not it's a match. For instance, is only the URL taken into account? I began to think about what would happen if a request came in for a particular URL with Accept-Encoding: gzip, deflate, br and then the response contained a gzip compressed body with header Content-Encoding: gzip. What then would happen if the same URL was requested by a client whose request contains e.g. Accept-Encoding: deflate. Would the CacheStorage API match this based off the URL alone and return the response with the gziped body?

Well I tested this scenario in Deno and it appears to be the case. Deno appears to only match based off the URL and doesn't care about the Content-Encoding header. So my question is whether this is a bug in Deno's implementation or whether or not its simply an oversite in the web API. Perhaps I'm overthinking this and in reality its moot because virtualy all HTTP clients in use today support gzip compressed responses.

CodePudding user response:

The only header considered in the Request matches cached item algorithm is the Vary header.

External clients don't call the Cache API itself. Your application code is able to manage those calls completely.

Here are some ideas on making an application that uses the Cache API and supports both compressed and uncompressed responses to its clients:

  • internally use a specific compression with the Cache API
  • require clients to support the encoding or add support for on-demand decompression (see next item)
  • when serving a Response from the Cache to a client…
    • if the client specifies that it accepts the same encoding then return the compressed response directly from the Cache
    • if the client specifies that it accepts other encodings but not the one your application is using internally then decompress and possibly re-compress using a different algorithm to return the desired Response
    • if the client specifies no encodings that it accepts then decompress the Response from Cache to return to the client
  • Related