Skip to content

Commit

Permalink
Added docs for cache: no-store
Browse files Browse the repository at this point in the history
  • Loading branch information
tewaro committed Nov 11, 2024
1 parent 26d34a4 commit 62a4ca4
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 3 deletions.
39 changes: 39 additions & 0 deletions src/content/compatibility-dates/cache-no-store.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
---
_build:
publishResources: false
render: never
list: never

name: "Enable `cache: no-store` HTTP standard API"
sort_date: "2024-11-11"
enabled_date: "2024-11-11"
enable_flag: "cache_option_enabled"
disable_flag: "cache_option_disabled"
---

When you enable the `cache_option_enabled` compatibility flag, you can specify a value for the `cache` property of the Request interface.
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
'RequestInitializerDict is not implemented.`

For example, 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/):

The only cache option enabled with `cache_option_enabled` is `'no-store'`.
Any other value will cause the Workers runtime to throw a `TypeError` with the message `Unsupported cache mode: <the-mode-you-specified>`.

When `no-store` is specified:
* For a cross-zone request the headers `Pragma: no-cache` and `Cache-Control: no-cache` are sent to the origin.

* For subrequests to origins not hosted by Cloudflare, `no-store` bypasses Cloudflare's cache in addition to specifying the above headers.

Examples using `cache: 'no-store'`.

```js
const response = await fetch("https://example.com", { cache: 'no-store'});
```

You can also create a Response object to pass around:

```js
const request = new Response("https://example.com", { cache: 'no-store'});
const response = await fetch(request);
```
9 changes: 9 additions & 0 deletions src/content/docs/workers/examples/cache-using-fetch.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -466,3 +466,12 @@ async function handleRequest(request) {
```
</TabItem> </Tabs>
## Using the HTTP Cache API
Currently we only support the `no-store` header for controlling cache.
When `no-store` is supplied the cache is bypassed both on the way to the origin and on the way back.
```js
fetch(request, { cache: 'no-store'});
```
8 changes: 7 additions & 1 deletion src/content/docs/workers/runtime-apis/fetch.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,12 @@ async function eventHandler(event) {
* [`resource`](https://developer.mozilla.org/en-US/docs/Web/API/fetch#resource) Request | string | URL

* `options` options
* `cache` `undefined | 'no-store'` optional
* Standard HTTP `cache` header. Only `cache: 'no-store'` is supported.
Any other `cache` header will result in a `TypeError` with the message `Unsupported cache mode: <attempted-cache-mode>`.
* For cross-zone requests this simply forwards the `Pragma: no-cache` and `Cache-Control: no-cache` headers to the origin.
This is because the cache is on the origin side for cross-zone requests.
* For requests to origins not hosted by Cloudflare, `no-store` bypasses the use of Cloudflare's caches.
* An object that defines the content and behavior of the request.


Expand Down Expand Up @@ -121,4 +127,4 @@ export default {
* [Example: Fetch HTML](/workers/examples/fetch-html/)
* [Example: Fetch JSON](/workers/examples/fetch-json/)
* [Example: cache using Fetch](/workers/examples/cache-using-fetch/)
* Write your Worker code in [ES modules syntax](/workers/reference/migrate-to-module-workers/) for an optimized experience.
* Write your Worker code in [ES modules syntax](/workers/reference/migrate-to-module-workers/) for an optimized experience.
7 changes: 5 additions & 2 deletions src/content/docs/workers/runtime-apis/request.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,10 @@ let request = new Request(input, options)

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

* `cache` `undefined | 'no-store'` optional

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

* `cf` RequestInitCfProperties optional

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

:::caution

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.
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.
:::

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

0 comments on commit 62a4ca4

Please sign in to comment.