Skip to content

Commit 80b2c1b

Browse files
authored
Added docs for cache: no-store (#17926)
1 parent aa06b12 commit 80b2c1b

File tree

4 files changed

+60
-5
lines changed

4 files changed

+60
-5
lines changed
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
---
2+
_build:
3+
publishResources: false
4+
render: never
5+
list: never
6+
7+
name: "Enable `cache: no-store` HTTP standard API"
8+
sort_date: "2024-11-11"
9+
enabled_date: "2024-11-11"
10+
enable_flag: "cache_option_enabled"
11+
disable_flag: "cache_option_disabled"
12+
---
13+
14+
When you enable the `cache_option_enabled` compatibility flag, you can specify a value for the `cache` property of the Request interface.
15+
When this compatibility flag is not enabled, or `cache_option_disabled` is set, the Workers runtime will throw an `Error` saying `The 'cache' field on
16+
'RequestInitializerDict' is not implemented.`
17+
18+
When this flag is enabled you can instruct Cloudflare not to cache the response from a subrequest you make from your Worker using the [`fetch()` API](/workers/runtime-apis/fetch/):
19+
20+
The only cache option enabled with `cache_option_enabled` is `'no-store'`.
21+
Specifying any other value will cause the Workers runtime to throw a `TypeError` with the message `Unsupported cache mode: <the-mode-you-specified>`.
22+
23+
When `no-store` is specified:
24+
* All requests have the headers `Pragma: no-cache` and `Cache-Control: no-cache` are set on them.
25+
26+
* Subrequests to origins not hosted by Cloudflare bypasses Cloudflare's cache.
27+
28+
Examples using `cache: 'no-store'`:
29+
30+
```js
31+
const response = await fetch("https://example.com", { cache: 'no-store'});
32+
```
33+
34+
The cache value can also be set on a `Request` object.
35+
36+
```js
37+
const request = new Request("https://example.com", { cache: 'no-store'});
38+
const response = await fetch(request);
39+
```

src/content/docs/workers/examples/cache-using-fetch.mdx

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -466,3 +466,13 @@ async function handleRequest(request) {
466466
```
467467
468468
</TabItem> </Tabs>
469+
470+
## Using the HTTP Cache API
471+
472+
The `cache` mode can be set in `fetch` options.
473+
Currently Workers only support the `no-store` mode for controlling the cache.
474+
When `no-store` is supplied the cache is bypassed on the way to the origin and the request is not cacheable.
475+
476+
```js
477+
fetch(request, { cache: 'no-store'});
478+
```

src/content/docs/workers/runtime-apis/fetch.mdx

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -75,10 +75,13 @@ async function eventHandler(event) {
7575
* [`resource`](https://developer.mozilla.org/en-US/docs/Web/API/fetch#resource) Request | string | URL
7676

7777
* `options` options
78+
* `cache` `undefined | 'no-store'` optional
79+
* Standard HTTP `cache` header. Only `cache: 'no-store'` is supported.
80+
Any other `cache` header will result in a `TypeError` with the message `Unsupported cache mode: <attempted-cache-mode>`.
81+
* For all requests this forwards the `Pragma: no-cache` and `Cache-Control: no-cache` headers to the origin.
82+
* For requests to origins not hosted by Cloudflare, `no-store` bypasses the use of Cloudflare's caches.
7883
* An object that defines the content and behavior of the request.
7984

80-
81-
8285
***
8386

8487
## How the `Accept-Encoding` header is handled
@@ -121,4 +124,4 @@ export default {
121124
* [Example: Fetch HTML](/workers/examples/fetch-html/)
122125
* [Example: Fetch JSON](/workers/examples/fetch-json/)
123126
* [Example: cache using Fetch](/workers/examples/cache-using-fetch/)
124-
* Write your Worker code in [ES modules syntax](/workers/reference/migrate-to-module-workers/) for an optimized experience.
127+
* Write your Worker code in [ES modules syntax](/workers/reference/migrate-to-module-workers/) for an optimized experience.

src/content/docs/workers/runtime-apis/request.mdx

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,10 @@ let request = new Request(input, options)
6262

6363
An object containing properties that you want to apply to the request.
6464

65+
* `cache` `undefined | 'no-store'` optional
6566

67+
* Standard HTTP `cache` header. Only `cache: 'no-store'` is supported.
68+
Any other cache header will result in a `TypeError` with the message `Unsupported cache mode: <attempted-cache-mode>`.
6669

6770
* `cf` RequestInitCfProperties optional
6871

@@ -180,7 +183,7 @@ All properties of an incoming `Request` object (the request you receive from the
180183

181184
:::caution
182185

183-
If the response is a redirect and the redirect mode is set to `follow` (see below), then all headers will be forwarded to the redirect destination, even if the destination is a different hostname or domain. This includes sensitive headers like `Cookie`, `Authorization`, or any application-specific headers. If this is not the behavior you want, you should set redirect mode to `manual` and implement your own redirect policy. Note that redirect mode defaults to `manual` for requests that originated from the Worker's client, so this warning only applies to `fetch()`es made by a Worker that are not proxying the original request.
186+
If the response is a redirect and the redirect mode is set to `follow` (see below), then all headers will be forwarded to the redirect destination, even if the destination is a different hostname or domain. This includes sensitive headers like `Cookie`, `Authorization`, or any application-specific headers. If this is not the behavior you want, you should set redirect mode to `manual` and implement your own redirect policy. Note that redirect mode defaults to `manual` for requests that originated from the Worker's client, so this warning only applies to `fetch()`es made by a Worker that are not proxying the original request.
184187
:::
185188

186189
* `method` string read-only
@@ -410,4 +413,4 @@ Using any other type of `ReadableStream` as the body of a request will result in
410413
* [Examples: Modify request property](/workers/examples/modify-request-property/)
411414
* [Examples: Accessing the `cf` object](/workers/examples/accessing-the-cloudflare-object/)
412415
* [Reference: `Response`](/workers/runtime-apis/response/)
413-
* Write your Worker code in [ES modules syntax](/workers/reference/migrate-to-module-workers/) for an optimized experience.
416+
* Write your Worker code in [ES modules syntax](/workers/reference/migrate-to-module-workers/) for an optimized experience.

0 commit comments

Comments
 (0)