Skip to content
Merged
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 10 additions & 8 deletions src/content/docs/workers/runtime-apis/cache.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,17 @@ You may create and manage additional Cache instances via the [`caches.open`](htt
let myCache = await caches.open('custom:cache');
await myCache.match(request);
```
When using the cache API, avoid overriding the hostname in cache requests, as this can lead to unnecessary DNS lookups and cache inefficiencies. Always use the hostname that matches the domain associated with your Worker.

```js
async fetch(request, env, ctx) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Indentation on this JS is a bit off. Everything within the brackets should be indented once.

Though actually, this also isn't valid fetch code. You would need to return the response from myCache.match(request) in order for it to be valid.

But... that doesn't really make sense because you might have a cache miss and you'd have to handle that (see this full code - https://developers.cloudflare.com/workers/examples/cache-api/)

I think since we don't want to show all of that code in here, what we should do is just drop the fetch() bits completely? It might be a tad less clear where request is coming from in this case, but I think it's fine.

So I'd just do

// avoid overriding the URL in request or passing a new request into the cache API
request.url = "https://some-overridden-value.com/";

let myCache = await caches.open('custom:cache');
let response = await myCache.match(request);

If it'll never work then I'd add this above the last line "// will never work because URL doesn't match Worker's domain"

// avoid overriding the request URL or passing a new request into the cache API
request.url = "https://some-overridden-value.com";

let myCache = await caches.open('custom:cache');
await myCache.match(request);
},
```
***

## Headers
Expand Down Expand Up @@ -105,10 +115,6 @@ cache.put(request, response);


The `stale-while-revalidate` and `stale-if-error` directives are not supported when using the `cache.put` or `cache.match` methods.


:::

#### Parameters


Expand Down Expand Up @@ -152,10 +158,6 @@ cache.match(request, options);


The `stale-while-revalidate` and `stale-if-error` directives are not supported when using the `cache.put` or `cache.match` methods.


:::

#### Parameters


Expand Down
Loading