Range requests and the cache API

I’m trying to figure out how to use the cache API in workers with ranged requests.

The documentation is a bit confusing.

First it says that caching a response with a 206 status is considered invalid and will result in an error.

cache.put throws an error if… the response passed is a status of 206 Partial Content

And then it says:

Our implementation of the Cache API respects the following HTTP headers on the request passed to match() :

  • Range: Results in a 206 response if a matching response is found. Your Cloudflare cache always respects range requests, even if an Accept-Ranges header is on the response.

Isn’t this conflicting information?

1 Like

@pier I don’t believe this is conflicting. An error is thrown if the response from a fetch is a 206 and you try to cache it. But if it’s not a 206 (i.e. not a range response), then you can cache it and future requests that ask for a range from the cached content will succeed. I am curious, however, how Cloudflare recommends taking a range request for uncached content, asking the origin for, caching the full content from the origin, and also responding to that request with a range at the same time.

1 Like

I’d like to know too.

AFAIK once you’ve cached something, you can only return it as-is after doing match(). At least that’s what all the Cache API examples do.

Currently we’re handling it like this, but I don’t think that’s the best. We’ve reached out to Cloudflare for assistance with this.

1 Like