Skip to content

Commit 13188a6

Browse files
committed
chore: use deno fmt
1 parent 2073dd8 commit 13188a6

20 files changed

Lines changed: 567 additions & 499 deletions

.changeset/README.md

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
11
# Changesets
22

3-
Hello and welcome! This folder has been automatically generated by `@changesets/cli`, a build tool that works
4-
with multi-package repos, or single-package repos to help you version and publish your code. You can
5-
find the full documentation for it [in our repository](https://github.com/changesets/changesets)
3+
Hello and welcome! This folder has been automatically generated by
4+
`@changesets/cli`, a build tool that works with multi-package repos, or
5+
single-package repos to help you version and publish your code. You can find the
6+
full documentation for it
7+
[in our repository](https://github.com/changesets/changesets)
68

7-
We have a quick list of common questions to get you started engaging with this project in
9+
We have a quick list of common questions to get you started engaging with this
10+
project in
811
[our documentation](https://github.com/changesets/changesets/blob/main/docs/common-questions.md)

.changeset/config.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,4 @@
88
"baseBranch": "main",
99
"updateInternalDependencies": "patch",
1010
"ignore": []
11-
}
11+
}

.prettierignore

Lines changed: 0 additions & 3 deletions
This file was deleted.

.prettierrc

Lines changed: 0 additions & 1 deletion
This file was deleted.

.vscode/settings.json

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,19 @@
11
{
2-
"typescript.tsdk": "node_modules/typescript/lib"
2+
"typescript.tsdk": "node_modules/typescript/lib",
3+
"deno.enable": true,
4+
"deno.config": "./deno.json",
5+
"deno.suggest.imports.hosts": {
6+
"https://deno.land": true,
7+
"https://jsr.io": true
8+
},
9+
"[typescript]": {
10+
"editor.defaultFormatter": "denoland.vscode-deno"
11+
},
12+
"[javascript]": {
13+
"editor.defaultFormatter": "denoland.vscode-deno"
14+
},
15+
"prettier.enable": false,
16+
"deno.enablePaths": [
17+
"packages"
18+
]
319
}

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
# cache-primitives
22

3-
This repository is a monorepo containing several packages related to CDN cache control.
3+
This repository is a monorepo containing several packages related to CDN cache
4+
control.
45

56
## Packages
67

78
- `cdn-cache-control`: Easy, opinionated CDN cache header handling.
89
- `cache-handlers`: Modern CDN cache primitives using web-standard middleware.
9-

deno.json

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,17 @@
11
{
2-
"workspace": ["packages/cdn-cache-control"]
2+
"workspace": ["packages/cdn-cache-control"],
3+
"imports": {
4+
"@std/assert": "jsr:@std/assert@^1.0.13"
5+
},
6+
"fmt": {
7+
"useTabs": true,
8+
"useBraces": "always",
9+
"singleBodyPosition": "nextLine",
10+
"exclude": [
11+
"**/*/package.json",
12+
"pnpm-lock.yaml",
13+
"**/*/CHANGELOG.md",
14+
"package.json"
15+
]
16+
}
317
}

package.json

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,12 @@
77
"lint": "pnpm -r lint",
88
"check": "pnpm -r check",
99
"version": "changeset version && pnpm -r version",
10-
"format": "pnpm -r format --write"
10+
"format": "deno fmt"
1111
},
1212
"devDependencies": {
1313
"@changesets/cli": "^2.27.7",
14-
"prettier": "^3.3.1",
1514
"tsdoc-markdown": "^0.6.0",
1615
"typescript": "^5.9.2"
1716
},
1817
"packageManager": "pnpm@8.14.0+sha1.bb42032ff80dba5f9245bc1b03470d2fa0b7fb2f"
19-
}
18+
}

packages/cdn-cache-control/README.md

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

66
Easy, opinionated CDN cache header handling.
77

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

1018
e.g.
1119

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

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

2534
```javascript
2635
import { CacheHeaders } from "jsr:@ascorbic/cdn-cache-control";
2736
```
2837

2938
## Usage
3039

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

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

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

3758
### Use cases
3859

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

4163
```javascript
4264
import { CacheHeaders } from "cdn-cache-control";
4365

4466
const headers = new CacheHeaders();
4567
```
4668

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

4977
#### stale-while-revalidate
5078

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):
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):
5281

5382
```javascript
5483
import { CacheHeaders } from "cdn-cache-control";
5584

5685
const headers = new CacheHeaders().swr();
5786
```
5887

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):
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):
6092

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

6799
#### Immutable content
68100

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

71105
```javascript
72106
import { CacheHeaders } from "cdn-cache-control";
73107
const headers = new CacheHeaders().immutable();
74108
```
75109

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

78113
#### Cache tags
79114

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

82119
```javascript
83120
import { CacheHeaders } from "cdn-cache-control";
84121
const headers = new CacheHeaders().tag(["blog", "blog:1"]);
85122
```
86123

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

89127
```typescript
90128
import { purgeCache } from "@netlify/functions";
@@ -95,32 +133,36 @@ export default async function handler(req: Request) => {
95133
});
96134
return new Response("Purged!", { status: 202 })
97135
};
98-
99136
```
100137

101138
#### Using the generated headers
102139

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

105146
```typescript
106147
import { CacheHeaders } from "cdn-cache-control";
107148

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

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

117160
```astro
118161
---
119162
import { CacheHeaders, ONE_HOUR } from "cdn-cache-control";
120163
121164
new CacheHeaders().swr(ONE_HOUR).copyTo(Astro.response.headers);
122165
---
123-
124166
```
125167

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

182224
- [Installation](#installation)
183225
- [Usage](#usage)
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)
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)
189231
- [API](#api)
190232
- [:wrench: Constants](#wrench-constants)
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)
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)
196238
- [:factory: CacheHeaders](#factory-cacheheaders)
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)
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)
210252

211253
#### :gear: tag
212254

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

215258
| Method | Type |
216259
| ------ | ------------------------------------------------------ |
@@ -222,37 +265,43 @@ Parameters:
222265

223266
#### :gear: swr
224267

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

228272
| Method | Type |
229273
| ------ | -------------------------- |
230274
| `swr` | `(value?: number) => this` |
231275

232276
Parameters:
233277

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

236281
#### :gear: immutable
237282

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

243290
| Method | Type |
244291
| ----------- | -------------------------- |
245292
| `immutable` | `(value?: number) => this` |
246293

247294
Parameters:
248295

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

251299
#### :gear: ttl
252300

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

257306
| Method | Type |
258307
| ------ | ------------------------- |

0 commit comments

Comments
 (0)