Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
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.`

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'`.
Specifying 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:
* All requests have the headers `Pragma: no-cache` and `Cache-Control: no-cache` are set on them.

* Subrequests to origins not hosted by Cloudflare bypasses Cloudflare's cache.

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

```js
Copy link
Contributor

Choose a reason for hiding this comment

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

Maybe add a little text about this one specifically, so it's similar to to the one below.

Maybe just: "The 'cache' have can be passed into the options for fetch"

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

The cache value can also be set on a `Request` object.

```js
const request = new Request("https://example.com", { cache: 'no-store'});
const response = await fetch(request);
```
10 changes: 10 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,13 @@ async function handleRequest(request) {
```

</TabItem> </Tabs>

## Using the HTTP Cache API

The `cache` mode can be set in `fetch` options.
Currently Workers only support the `no-store` mode for controlling the cache.
When `no-store` is supplied the cache is bypassed on the way to the origin and the request is not cacheable.

```js
fetch(request, { cache: 'no-store'});
```
9 changes: 6 additions & 3 deletions src/content/docs/workers/runtime-apis/fetch.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -75,10 +75,13 @@ 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 all requests this forwards the `Pragma: no-cache` and `Cache-Control: no-cache` headers to the origin.
* 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.



***

## How the `Accept-Encoding` header is handled
Expand Down Expand Up @@ -121,4 +124,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.
Loading