Skip to content

Commit bbddefe

Browse files
committed
revert changes for cdn cache control package
1 parent 704de8c commit bbddefe

8 files changed

Lines changed: 434 additions & 478 deletions

File tree

packages/cdn-cache-control/README.md

Lines changed: 55 additions & 104 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,7 @@
55

66
Easy, opinionated CDN cache header handling.
77

8-
Modern CDNs allow very fine-grained control over the cache. This is particularly
9-
useful for server-side rendering of web content, as it allows you to manually
10-
handle the invalidation of content, ensuring it stays fast and fresh. This
11-
package provides a subclass of the `Headers` class that makes it easier to set
12-
cache control headers for content served through a modern CDN. It provides a
13-
simple, chainable API with sensible defaults for common use cases. It works by
14-
setting the `Cache-Control` and `CDN-Cache-Control` headers to the appropriate
15-
values. If run on a supported platform it will use the more specific header for
16-
that CDN. e.g. on Netlify it will use the `Netlify-CDN-Cache-Control` header.
8+
Modern CDNs allow very fine-grained control over the cache. This is particularly useful for server-side rendering of web content, as it allows you to manually handle the invalidation of content, ensuring it stays fast and fresh. This package provides a subclass of the `Headers` class that makes it easier to set cache control headers for content served through a modern CDN. It provides a simple, chainable API with sensible defaults for common use cases. It works by setting the `Cache-Control` and `CDN-Cache-Control` headers to the appropriate values. If run on a supported platform it will use the more specific header for that CDN. e.g. on Netlify it will use the `Netlify-CDN-Cache-Control` header.
179

1810
e.g.
1911

@@ -28,67 +20,43 @@ const headers = new CacheHeaders().ttl(ONE_MINUTE).swr();
2820
npm install cdn-cache-control
2921
```
3022

31-
It is also available in [jsr](https://jsr.io) as `@ascorbic/cdn-cache-control`.
32-
If using Deno, you can import it directly without installing:
23+
It is also available in [jsr](https://jsr.io) as `@ascorbic/cdn-cache-control`. If using Deno, you can import it directly without installing:
3324

3425
```javascript
3526
import { CacheHeaders } from "jsr:@ascorbic/cdn-cache-control";
3627
```
3728

3829
## Usage
3930

40-
The module exports a single class, `CacheHeaders`, which is a subclass of the
41-
fetch [`Headers`](https://developer.mozilla.org/en-US/docs/Web/API/Headers)
42-
class. It provides a chainable API for setting cache headers. By default it sets
43-
the `Cache-Control` and `CDN-Cache-Control` headers to sensible defaults for
44-
content that should be cached by the CDN and revalidated by the browser.
31+
The module exports a single class, `CacheHeaders`, which is a subclass of the fetch [`Headers`](https://developer.mozilla.org/en-US/docs/Web/API/Headers) class. It provides a chainable API for setting cache headers. By default it sets the `Cache-Control` and `CDN-Cache-Control` headers to sensible defaults for content that should be cached by the CDN and revalidated by the browser.
4532

46-
It can be instantiated with a `HeadersInit` value, which lets you base it on an
47-
existing `Headers` object, or an object or array with existing header values. In
48-
that case it will default to using existing `s-maxage` directives if present.
33+
It can be instantiated with a `HeadersInit` value, which lets you base it on an existing `Headers` object, or an object or array with existing header values. In that case it will default to using existing `s-maxage` directives if present.
4934

50-
You can pass a `cdn` value as the second argument to set the CDN cache control
51-
header. In some cases this will enable targeted cache header names. e.g. for
52-
Netlify it will use the `Netlify-CDN-Cache-Control` header. Currently supported
53-
values are `netlify`, `vercel`, `cloudflare` and `akamai`. If you don't pass a
54-
value, or pass an unsupported one it will use the generic `CDN-Cache-Control`
55-
header. It will also attempt to detect the platform automatically on Vercel and
56-
Netlify.
35+
You can pass a `cdn` value as the second argument to set the CDN cache control header. In some cases this will enable targeted cache header names. e.g. for Netlify it will use the `Netlify-CDN-Cache-Control` header. Currently supported values are `netlify`, `vercel`, `cloudflare` and `akamai`. If you don't pass a value, or pass an unsupported one it will use the generic `CDN-Cache-Control` header. It will also attempt to detect the platform automatically on Vercel and Netlify.
5736

5837
### Use cases
5938

60-
If you have content that you want to have the CDN cache until it is manually
61-
revalidated or purged with a new deploy, you can use the default values:
39+
If you have content that you want to have the CDN cache until it is manually revalidated or purged with a new deploy, you can use the default values:
6240

6341
```javascript
6442
import { CacheHeaders } from "cdn-cache-control";
6543

6644
const headers = new CacheHeaders();
6745
```
6846

69-
This sets the `CDN-Cache-Control` header to
70-
`public,s-maxage=31536000,must-revalidate`, which tells the CDN to cache the
71-
content for a year. It sets `Cache-Control` to
72-
`public,max-age=0,must-revalidate`, which tells the browser to always check with
73-
the CDN for a fresh version. You should combine this with an `ETag` or
74-
`Last-Modified` header to allow the CDN to serve a `304 Not Modified` response
75-
when the content hasn't changed.
47+
This sets the `CDN-Cache-Control` header to `public,s-maxage=31536000,must-revalidate`, which tells the CDN to cache the content for a year. It sets `Cache-Control` to `public,max-age=0,must-revalidate`, which tells the browser to always check with the CDN for a fresh version. You should combine this with an `ETag` or `Last-Modified` header to allow the CDN to serve a `304 Not Modified` response when the content hasn't changed.
7648

7749
#### stale-while-revalidate
7850

79-
You can enable `stale-while-revalidate` with the `swr` method, optionally
80-
passing a value for the time to serve stale content (defaults to one week):
51+
You can enable `stale-while-revalidate` with the `swr` method, optionally passing a value for the time to serve stale content (defaults to one week):
8152

8253
```javascript
8354
import { CacheHeaders } from "cdn-cache-control";
8455

8556
const headers = new CacheHeaders().swr();
8657
```
8758

88-
This tells the CDN to serve stale content while revalidating the content in the
89-
background. Combine with the `ttl` method to set the time for which the content
90-
will be considered fresh (default is zero, meaning the CDN will always
91-
revalidate):
59+
This tells the CDN to serve stale content while revalidating the content in the background. Combine with the `ttl` method to set the time for which the content will be considered fresh (default is zero, meaning the CDN will always revalidate):
9260

9361
```javascript
9462
import { CacheHeaders, ONE_HOUR } from "cdn-cache-control";
@@ -98,31 +66,25 @@ const headers = new CacheHeaders().swr().ttl(ONE_HOUR);
9866

9967
#### Immutable content
10068

101-
If you are serving content that is guaranteed to never change then you can set
102-
it as immutable. You should only do this for responses with unique URLs, because
103-
there will be no way to invalidate it from the browser cache if it ever changes.
69+
If you are serving content that is guaranteed to never change then you can set it as immutable. You should only do this for responses with unique URLs, because there will be no way to invalidate it from the browser cache if it ever changes.
10470

10571
```javascript
10672
import { CacheHeaders } from "cdn-cache-control";
10773
const headers = new CacheHeaders().immutable();
10874
```
10975

110-
This will set the CDN and browser caches to expire in 1 year, and add the
111-
immutable directive.
76+
This will set the CDN and browser caches to expire in 1 year, and add the immutable directive.
11277

11378
#### Cache tags
11479

115-
Some CDNs support the use of cache tags, which allow you to purge content from
116-
the cache in bulk. The `tag()` function makes it simple to add tags. You can
117-
call it with a string or array of strings.
80+
Some CDNs support the use of cache tags, which allow you to purge content from the cache in bulk. The `tag()` function makes it simple to add tags. You can call it with a string or array of strings.
11881

11982
```javascript
12083
import { CacheHeaders } from "cdn-cache-control";
12184
const headers = new CacheHeaders().tag(["blog", "blog:1"]);
12285
```
12386

124-
You can then purge the tagged items from the cache using the CDN API. e.g. for
125-
Netlify the API is:
87+
You can then purge the tagged items from the cache using the CDN API. e.g. for Netlify the API is:
12688

12789
```typescript
12890
import { purgeCache } from "@netlify/functions";
@@ -133,36 +95,32 @@ export default async function handler(req: Request) => {
13395
});
13496
return new Response("Purged!", { status: 202 })
13597
};
98+
13699
```
137100

138101
#### Using the generated headers
139102

140-
The headers object can be used anywhere that accepts a `fetch` `Headers` object.
141-
This includes most serverless hosts. It can also be used directly in many
142-
framework SSR functions. Some APIs need a plain object rather than a `Headers`
143-
object. For these you can use the `toObject()` method, which returns a plain
144-
object with the header names and values.
103+
The headers object can be used anywhere that accepts a `fetch` `Headers` object. This includes most serverless hosts. It can also be used directly in many framework SSR functions. Some APIs need a plain object rather than a `Headers` object. For these you can use the `toObject()` method, which returns a plain object with the header names and values.
145104

146105
```typescript
147106
import { CacheHeaders } from "cdn-cache-control";
148107

149108
export default async function handler(request: Request): Promise<Response> {
150-
const headers = new CacheHeaders().swr();
151-
// The `Response` constructor accepts the object directly
152-
return new Response("Hello", { headers });
109+
const headers = new CacheHeaders().swr();
110+
// The `Response` constructor accepts the object directly
111+
return new Response("Hello", { headers });
153112
}
154113
```
155114

156-
Some frameworks use a readonly `Response` object, so you need to use an existing
157-
`headers` object. In this case you can use the `copyTo` method to copy the
158-
headers to the response:
115+
Some frameworks use a readonly `Response` object, so you need to use an existing `headers` object. In this case you can use the `copyTo` method to copy the headers to the response:
159116

160117
```astro
161118
---
162119
import { CacheHeaders, ONE_HOUR } from "cdn-cache-control";
163120
164121
new CacheHeaders().swr(ONE_HOUR).copyTo(Astro.response.headers);
165122
---
123+
166124
```
167125

168126
## API
@@ -223,37 +181,36 @@ Number of seconds in one year
223181

224182
- [Installation](#installation)
225183
- [Usage](#usage)
226-
- [Use cases](#use-cases)
227-
- [stale-while-revalidate](#stale-while-revalidate)
228-
- [Immutable content](#immutable-content)
229-
- [Cache tags](#cache-tags)
230-
- [Using the generated headers](#using-the-generated-headers)
184+
- [Use cases](#use-cases)
185+
- [stale-while-revalidate](#stale-while-revalidate)
186+
- [Immutable content](#immutable-content)
187+
- [Cache tags](#cache-tags)
188+
- [Using the generated headers](#using-the-generated-headers)
231189
- [API](#api)
232190
- [:wrench: Constants](#wrench-constants)
233-
- [:gear: ONE_MINUTE](#gear-one_minute)
234-
- [:gear: ONE_HOUR](#gear-one_hour)
235-
- [:gear: ONE_DAY](#gear-one_day)
236-
- [:gear: ONE_WEEK](#gear-one_week)
237-
- [:gear: ONE_YEAR](#gear-one_year)
191+
- [:gear: ONE\_MINUTE](#gear-one_minute)
192+
- [:gear: ONE\_HOUR](#gear-one_hour)
193+
- [:gear: ONE\_DAY](#gear-one_day)
194+
- [:gear: ONE\_WEEK](#gear-one_week)
195+
- [:gear: ONE\_YEAR](#gear-one_year)
238196
- [:factory: CacheHeaders](#factory-cacheheaders)
239-
- [Methods](#methods)
240-
- [:gear: tag](#gear-tag)
241-
- [:gear: swr](#gear-swr)
242-
- [:gear: immutable](#gear-immutable)
243-
- [:gear: ttl](#gear-ttl)
244-
- [:gear: toObject](#gear-toobject)
245-
- [:gear: copyTo](#gear-copyto)
246-
- [:gear: getCdnCacheControl](#gear-getcdncachecontrol)
247-
- [:gear: setCdnCacheControl](#gear-setcdncachecontrol)
248-
- [:gear: getCacheControl](#gear-getcachecontrol)
249-
- [:gear: setCacheControl](#gear-setcachecontrol)
250-
- [:gear: getCacheTags](#gear-getcachetags)
251-
- [:gear: setCacheTags](#gear-setcachetags)
197+
- [Methods](#methods)
198+
- [:gear: tag](#gear-tag)
199+
- [:gear: swr](#gear-swr)
200+
- [:gear: immutable](#gear-immutable)
201+
- [:gear: ttl](#gear-ttl)
202+
- [:gear: toObject](#gear-toobject)
203+
- [:gear: copyTo](#gear-copyto)
204+
- [:gear: getCdnCacheControl](#gear-getcdncachecontrol)
205+
- [:gear: setCdnCacheControl](#gear-setcdncachecontrol)
206+
- [:gear: getCacheControl](#gear-getcachecontrol)
207+
- [:gear: setCacheControl](#gear-setcachecontrol)
208+
- [:gear: getCacheTags](#gear-getcachetags)
209+
- [:gear: setCacheTags](#gear-setcachetags)
252210

253211
#### :gear: tag
254212

255-
Adds a cache tag to the cache tags header. Cache tags are used to invalidate the
256-
cache for a URL.
213+
Adds a cache tag to the cache tags header. Cache tags are used to invalidate the cache for a URL.
257214

258215
| Method | Type |
259216
| ------ | ------------------------------------------------------ |
@@ -265,43 +222,37 @@ Parameters:
265222

266223
#### :gear: swr
267224

268-
Sets stale-while-revalidate directive for the CDN cache. By default the browser
269-
is sent a must-revalidate directive to ensure that the browser always
270-
revalidates the cache with the server.
225+
Sets stale-while-revalidate directive for the CDN cache. By default the browser is sent a must-revalidate
226+
directive to ensure that the browser always revalidates the cache with the server.
271227

272228
| Method | Type |
273229
| ------ | -------------------------- |
274230
| `swr` | `(value?: number) => this` |
275231

276232
Parameters:
277233

278-
- `value`: The number of seconds to set the stale-while-revalidate directive to.
279-
Defaults to 1 week.
234+
- `value`: The number of seconds to set the stale-while-revalidate directive to. Defaults to 1 week.
280235

281236
#### :gear: immutable
282237

283-
Sets cache headers for content that should be cached for a long time and never
284-
revalidated. The CDN cache will cache the content for the specified time, and
285-
the browser will cache the content indefinitely without revalidating. Do not use
286-
this unless the URL is fingerprinted or otherwise unique. Otherwise, the browser
287-
will cache the content indefinitely and never check for updates, including for
288-
new deploys.
238+
Sets cache headers for content that should be cached for a long time and never revalidated.
239+
The CDN cache will cache the content for the specified time, and the browser will cache the content
240+
indefinitely without revalidating. Do not use this unless the URL is fingerprinted or otherwise unique.
241+
Otherwise, the browser will cache the content indefinitely and never check for updates, including for new deploys.
289242

290243
| Method | Type |
291244
| ----------- | -------------------------- |
292245
| `immutable` | `(value?: number) => this` |
293246

294247
Parameters:
295248

296-
- `value`: The number of seconds to set the CDN cache-control s-maxage directive
297-
to. Defaults to 1 year.
249+
- `value`: The number of seconds to set the CDN cache-control s-maxage directive to. Defaults to 1 year.
298250

299251
#### :gear: ttl
300252

301-
Sets the s-maxage for items in the CDN cache. This is the maximum amount of time
302-
that the CDN will cache the content. If used with swr, the content will
303-
revalidate in the background after the max age has passed. Otherwise, the
304-
content will be removed from the cache after the max age has passed.
253+
Sets the s-maxage for items in the CDN cache. This is the maximum amount of time that the CDN will cache the content.
254+
If used with swr, the content will revalidate in the background after the max age has passed. Otherwise, the content will be
255+
removed from the cache after the max age has passed.
305256

306257
| Method | Type |
307258
| ------ | ------------------------- |
Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
{
2-
"name": "@ascorbic/cdn-cache-control",
3-
"version": "1.3.1",
4-
"exports": "./src/index.ts",
5-
"license": "MIT",
6-
"publish": {
7-
"include": ["README.md", "src/index.ts"]
8-
}
2+
"name": "@ascorbic/cdn-cache-control",
3+
"version": "1.3.1",
4+
"exports": "./src/index.ts",
5+
"license": "MIT",
6+
"publish": {
7+
"include": ["README.md", "src/index.ts"]
8+
}
99
}

packages/cdn-cache-control/package.json

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -29,11 +29,13 @@
2929
}
3030
},
3131
"scripts": {
32-
"build": "tsdown",
32+
"build": "tsdown src/index.ts --format esm,cjs --dts --clean",
3333
"check": "pnpm run '/^check:.*/'",
3434
"check:tsc": "tsc --noEmit",
3535
"check:deno": "deno check src/index.ts",
3636
"check:jsr": "deno publish --dry-run --allow-dirty",
37+
"lint:types": "attw --pack .",
38+
"lint:package": "publint",
3739
"lint:prettier": "prettier --check src",
3840
"lint": "pnpm run '/^lint:.*/'",
3941
"test": "pnpm run test:node",
@@ -45,9 +47,9 @@
4547
"author": "Matt Kane <m@mk.gg>",
4648
"license": "MIT",
4749
"devDependencies": {
48-
"@arethetypeswrong/core": "^0.18.2",
50+
"@arethetypeswrong/cli": "^0.15.3",
4951
"@types/node": "^20.14.2",
50-
"publint": "^0.3.12",
51-
"tsdown": "^0.13.3"
52+
"publint": "^0.2.8",
53+
"tsdown": "^0.13.2"
5254
}
53-
}
55+
}

0 commit comments

Comments
 (0)