Skip to content

Commit 3207a69

Browse files
committed
Add another worker
1 parent 949bfa6 commit 3207a69

File tree

1 file changed

+29
-7
lines changed

1 file changed

+29
-7
lines changed

README.md

Lines changed: 29 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# Cache Tag
22

3-
Cloudflare has the ability to index cached resources by _tag_ which allows those resources be
3+
[Cloudflare](https://www.cloudflare.com/) has the ability to index cached resources by _tag_ which allows those resources be
44
[purged by tag](https://developers.cloudflare.com/cache/how-to/purge-cache/purge-by-tags/). However, this feature is
55
[only available for Enterprise customers](https://developers.cloudflare.com/cache/how-to/purge-cache/purge-by-tags/#:~:text=Note%20that%20Tag%2C%20Hostname%20and%20Prefix%20purges%20are%20only%20available%20for%20Enterprise%20customers.).
66

@@ -22,22 +22,44 @@ This application is broken up into three [Workers](https://developers.cloudflare
2222

2323
#### [Watcher](./workers/watcher/)
2424

25-
This worker watches requests to the Cloudflare Cache / Origin, captures the tags, and sends them to the
26-
[Controller](./workers/controller/) in order to be persisted.
25+
This worker watches requests to the Cloudflare [Cache](https://developers.cloudflare.com/cache/) / Origin, captures the
26+
tags, and sends them to the [Controller](./workers/controller/) in order to be persisted.
2727

2828
> [!IMPORTANT]
2929
> By the time a response from an origin reaches a [Worker](https://developers.cloudflare.com/workers/), Cloudflare
30-
> has already swallowed the `Cache-Tag` header and it is not longer available. To get around this, the worker reads the
30+
> has already swallowed the `Cache-Tag` header and it is no longer available. To get around this, the worker reads the
3131
> custom `X-Cache-Tag` header instead.
3232
3333
The worker also exposes a `/.cloudflare/purge` endpoint that allows tags to be purged. This endpoint matches the
3434
[interface of the Cloudflare endpoint](https://developers.cloudflare.com/api/operations/zone-purge#purge-cached-content-by-tag-host-or-prefix), but only allows `tags`. The tags that are purged will be scoped
35-
to the zone in which the request is made too. For example, a purge request to `https://example.com/.cloudflare/purge`
36-
would only purge resources from the `example.com` zone.
35+
to the [zone](https://developers.cloudflare.com/fundamentals/setup/accounts-and-zones/#zones) in which the request is
36+
made too. For example, a purge request to `https://example.com/.cloudflare/purge` would only purge resources from the
37+
`example.com` [zone](https://developers.cloudflare.com/fundamentals/setup/accounts-and-zones/#zones).
3738

3839
#### [Controller](./workers/controller/)
3940

40-
_@todo_
41+
A [Worker](https://developers.cloudflare.com/workers/) is an
42+
[account-level resource](https://developers.cloudflare.com/fundamentals/setup/accounts-and-zones/#accounts), but
43+
[Cache](https://developers.cloudflare.com/cache/) is a
44+
[zone-level resource](https://developers.cloudflare.com/fundamentals/setup/accounts-and-zones/#zones). Because of this,
45+
there is no way to know what zone a resource is being cached in from a
46+
[Worker](https://developers.cloudflare.com/workers/).
47+
48+
To mitigate this problem, we can leverage the
49+
[CF-Worker](https://developers.cloudflare.com/fundamentals/reference/http-request-headers/#cf-worker) which gets added
50+
to outbound requests from a [Worker](https://developers.cloudflare.com/workers/). Unfortunately this header does not
51+
exist when using [Service Bindings](https://developers.cloudflare.com/workers/runtime-apis/bindings/service-bindings/).
52+
The only way to retrieve the header is by making a request to the [Worker](https://developers.cloudflare.com/workers/)
53+
on the provided [workers.dev subdomain](https://developers.cloudflare.com/workers/configuration/routing/workers-dev/).
54+
55+
The [Controller](./workers/controller/) exists primarily as an intermediary between [Watcher](./workers/watcher/) and [Handler](./workers/handler/) to collect zone information. It is **not** included as a part of [Handler](./workers/handler/) in order to ensure that the [Worker](https://developers.cloudflare.com/workers/) is collocated in the
56+
[same data center](https://developers.cloudflare.com/workers/configuration/smart-placement/) as
57+
[Watcher](./workers/watcher/).
58+
59+
The worker also exposes a `/purge` endpoint that allows tags to be purged. This endpoint matches the
60+
[interface of the Cloudflare endpoint](https://developers.cloudflare.com/api/operations/zone-purge#purge-cached-content-by-tag-host-or-prefix), but only allows `tags`. Since no
61+
[zone](https://developers.cloudflare.com/fundamentals/setup/accounts-and-zones/#zones) information is provided, matching
62+
resources from **all** zones will be purged.
4163

4264
#### [Handler](./workers/handler/)
4365

0 commit comments

Comments
 (0)