Skip to content

Commit 73016fe

Browse files
authored
Merge pull request #83 from adobe/claude-2
feat: export CacheOverride API for cross-platform cache control
2 parents e3d7cf4 + 831a1ce commit 73016fe

File tree

7 files changed

+653
-22
lines changed

7 files changed

+653
-22
lines changed

.nycrc.json

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,8 @@
66
"check-coverage": true,
77
"lines": 20,
88
"branches": 80,
9-
"statements": 20
9+
"statements": 20,
10+
"exclude": [
11+
"test/fixtures/**"
12+
]
1013
}

docs/API.md

Lines changed: 154 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,158 @@
1-
<a name="main"></a>
1+
# API Documentation
22

3-
## main(name) ⇒ <code>string</code>
4-
This is the main function
3+
## CacheOverride
54

6-
**Kind**: global function
7-
**Returns**: <code>string</code> - a greeting
5+
The `CacheOverride` class provides a unified API for controlling cache behavior across both Fastly Compute and Cloudflare Workers platforms.
86

9-
| Param | Type | Description |
10-
| --- | --- | --- |
11-
| name | <code>string</code> | name of the person to greet |
7+
### Import
128

9+
```javascript
10+
import { CacheOverride, fetch } from '@adobe/fetch';
11+
```
12+
13+
### Constructor
14+
15+
#### `new CacheOverride(mode, init)`
16+
17+
Creates a new CacheOverride instance with the specified mode and options.
18+
19+
**Parameters:**
20+
21+
- `mode` (string): Cache override mode. One of:
22+
- `"none"`: Respect origin cache control headers (default behavior)
23+
- `"pass"`: Prevent caching regardless of origin headers
24+
- `"override"`: Apply custom cache settings
25+
- `init` (object, optional): Cache configuration options
26+
27+
**Alternative Signature:**
28+
29+
#### `new CacheOverride(init)`
30+
31+
Creates a new CacheOverride instance with `"override"` mode and the specified options.
32+
33+
**Parameters:**
34+
35+
- `init` (object): Cache configuration options
36+
37+
### Cross-Platform Options
38+
39+
The CacheOverride API only includes options that work on **both** Fastly and Cloudflare platforms to ensure true cross-platform compatibility:
40+
41+
| Option | Type | Description | Platform Mapping |
42+
|--------|------|-------------|------------------|
43+
| `ttl` | number | Time-to-live in seconds | Fastly: native `ttl`<br>Cloudflare: `cf.cacheTtl` |
44+
| `cacheKey` | string | Custom cache key | Fastly: native `cacheKey`<br>Cloudflare: `cf.cacheKey` |
45+
| `surrogateKey` | string | Space-separated surrogate keys for cache purging | Fastly: native `surrogateKey`<br>Cloudflare: `cf.cacheTags` (array) |
46+
47+
**Note:** Platform-specific options (like Fastly's `swr`, `pci`, `beforeSend`, `afterSend`) are intentionally excluded to maintain cross-platform compatibility. If you pass unsupported options, they will be ignored with a console warning.
48+
49+
### Usage Examples
50+
51+
#### Basic TTL Override
52+
53+
```javascript
54+
import { fetch, CacheOverride } from '@adobe/fetch';
55+
56+
const cacheOverride = new CacheOverride('override', {
57+
ttl: 3600 // Cache for 1 hour
58+
});
59+
60+
const response = await fetch('https://example.com/api', {
61+
cacheOverride
62+
});
63+
```
64+
65+
#### Prevent Caching
66+
67+
```javascript
68+
const cacheOverride = new CacheOverride('pass');
69+
70+
const response = await fetch('https://example.com/api', {
71+
cacheOverride
72+
});
73+
```
74+
75+
#### Advanced Configuration
76+
77+
```javascript
78+
const cacheOverride = new CacheOverride({
79+
ttl: 3600, // Cache for 1 hour
80+
cacheKey: 'my-key', // Custom cache key
81+
surrogateKey: 'api v1' // Surrogate keys for purging
82+
});
83+
84+
const response = await fetch('https://example.com/api', {
85+
cacheOverride
86+
});
87+
```
88+
89+
#### Conditional Caching by Path
90+
91+
```javascript
92+
import { fetch, CacheOverride } from '@adobe/fetch';
93+
94+
export async function main(request, context) {
95+
const url = new URL(request.url);
96+
let cacheOverride;
97+
98+
if (url.pathname.startsWith('/static/')) {
99+
// Long cache for static resources
100+
cacheOverride = new CacheOverride({ ttl: 86400 });
101+
} else if (url.pathname === '/') {
102+
// Short cache for homepage
103+
cacheOverride = new CacheOverride({ ttl: 60 });
104+
} else {
105+
// Respect origin cache headers
106+
cacheOverride = new CacheOverride('none');
107+
}
108+
109+
return fetch(url, { cacheOverride });
110+
}
111+
```
112+
113+
### Platform-Specific Behavior
114+
115+
#### Fastly Compute
116+
117+
On Fastly, `CacheOverride` uses the native `fastly:cache-override` module. Only cross-platform compatible options are passed through to ensure consistent behavior.
118+
119+
```javascript
120+
// On Fastly, this uses native CacheOverride with cross-platform options
121+
const override = new CacheOverride('override', {
122+
ttl: 3600,
123+
cacheKey: 'my-key',
124+
surrogateKey: 'homepage main'
125+
});
126+
```
127+
128+
#### Cloudflare Workers
129+
130+
On Cloudflare, `CacheOverride` options are automatically mapped to the `cf` object in fetch options:
131+
132+
| CacheOverride | Cloudflare cf object |
133+
|---------------|---------------------|
134+
| `mode: "pass"` | `cf: { cacheTtl: 0 }` |
135+
| `mode: "none"` | No cf options added |
136+
| `ttl: 3600` | `cf: { cacheTtl: 3600 }` |
137+
| `cacheKey: "key"` | `cf: { cacheKey: "key" }` |
138+
| `surrogateKey: "a b"` | `cf: { cacheTags: ["a", "b"] }` |
139+
140+
```javascript
141+
// On Cloudflare, this is converted to:
142+
// fetch(url, { cf: { cacheTtl: 3600, cacheKey: 'my-key', cacheTags: ['api', 'v1'] } })
143+
const override = new CacheOverride({
144+
ttl: 3600,
145+
cacheKey: 'my-key',
146+
surrogateKey: 'api v1'
147+
});
148+
149+
await fetch(url, { cacheOverride: override });
150+
```
151+
152+
### Notes
153+
154+
- **Cross-Platform Compatibility**: Only options supported on both platforms are included in this API
155+
- **Unsupported Options**: If you pass platform-specific options (like `swr`, `pci`, `beforeSend`, `afterSend`), they will be ignored with a console warning
156+
- **Cloudflare Enterprise**: The `cacheKey` feature requires a Cloudflare Enterprise plan
157+
- **Surrogate Keys**: On Cloudflare, the space-separated `surrogateKey` string is automatically split into an array for `cf.cacheTags`
158+
- **For Platform-Specific Features**: If you need platform-specific cache features, use platform detection and native APIs directly instead of the cross-platform `CacheOverride` API

package.json

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@
55
"main": "src/index.js",
66
"type": "module",
77
"scripts": {
8-
"test": "c8 mocha -i -g Integration",
9-
"integration-ci": "c8 mocha -g Integration",
8+
"test": "c8 --exclude 'test/fixtures/**' mocha -i -g Integration",
9+
"integration-ci": "c8 --exclude 'test/fixtures/**' mocha -g Integration",
1010
"lint": "eslint .",
1111
"semantic-release": "semantic-release",
1212
"semantic-release-dry": "semantic-release --dry-run --branches $CI_BRANCH",
@@ -26,7 +26,10 @@
2626
"require": "test/setup-env.js",
2727
"recursive": "true",
2828
"reporter": "mocha-multi-reporters",
29-
"reporter-options": "configFile=.mocha-multi.json"
29+
"reporter-options": "configFile=.mocha-multi.json",
30+
"exclude": [
31+
"test/fixtures/**"
32+
]
3033
},
3134
"devDependencies": {
3235
"@adobe/eslint-config-helix": "3.0.14",

0 commit comments

Comments
 (0)