Skip to content
This repository was archived by the owner on Mar 13, 2025. It is now read-only.

Commit f3b0dc8

Browse files
committed
Remove cacheTtl/cacheTtlByStatus support from Cache API
The Workers Cache API only supports the `cacheKey` property of the `request.cf` object. Miniflare incorrectly supported `cacheTtl` and `cacheTtlByStatus`. Refs: #37, #248, #337
1 parent 4064fbf commit f3b0dc8

File tree

2 files changed

+1
-80
lines changed

2 files changed

+1
-80
lines changed

packages/cache/src/cache.ts

Lines changed: 0 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ import { URL } from "url";
22
import {
33
Request,
44
RequestInfo,
5-
RequestInitCfProperties,
65
Response,
76
withStringFormDataFiles,
87
} from "@miniflare/core";
@@ -57,36 +56,11 @@ function getKey(req: BaseRequest | Request): string {
5756
}
5857
}
5958

60-
const cacheTtlByStatusRangeRegexp = /^(?<from>\d+)(-(?<to>\d+))?$/;
61-
6259
function getExpirationTtl(
6360
clock: Clock,
6461
req: BaseRequest | Request,
6562
res: BaseResponse | Response
6663
): number | undefined {
67-
// Check cf property first for expiration TTL
68-
// @ts-expect-error cf doesn't exist on BaseRequest, but it will just be
69-
// undefined if it doesn't
70-
const cf: RequestInitCfProperties | undefined = req.cf;
71-
if (cf?.cacheTtl) return cf.cacheTtl * 1000;
72-
if (cf?.cacheTtlByStatus) {
73-
for (const [range, ttl] of Object.entries(cf.cacheTtlByStatus)) {
74-
const match = cacheTtlByStatusRangeRegexp.exec(range);
75-
const fromString: string | undefined = match?.groups?.from;
76-
// If no match, skip to next range
77-
if (!fromString) continue;
78-
const from = parseInt(fromString);
79-
const toString: string | undefined = match?.groups?.to;
80-
// If matched "to" group, check range, otherwise, just check equal status
81-
if (toString) {
82-
const to = parseInt(toString);
83-
if (from <= res.status && res.status <= to) return ttl * 1000;
84-
} else if (res.status === from) {
85-
return ttl * 1000;
86-
}
87-
}
88-
}
89-
9064
// Cloudflare ignores request Cache-Control
9165
const reqHeaders = normaliseHeaders(req.headers);
9266
delete reqHeaders["cache-control"];

packages/cache/test/cache.spec.ts

Lines changed: 1 addition & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import assert from "assert";
22
import { URL } from "url";
33
import { Cache, CacheError, CachedMeta } from "@miniflare/cache";
4-
import { Request, RequestInitCfProperties, Response } from "@miniflare/core";
4+
import { Request, Response } from "@miniflare/core";
55
import {
66
EXTERNAL_SUBREQUEST_LIMIT_BUNDLED,
77
RequestContext,
@@ -147,59 +147,6 @@ test("Cache: respects cache key", async (t) => {
147147
t.is(await match1?.text(), "value1");
148148
t.is(await match2?.text(), "value2");
149149
});
150-
test("Cache: put respects cf cacheTtl", async (t) => {
151-
const { clock, cache } = t.context;
152-
await cache.put(
153-
new Request("http://localhost/test", { cf: { cacheTtl: 1 } }),
154-
new BaseResponse("value")
155-
);
156-
t.not(await cache.match("http://localhost/test"), undefined);
157-
clock.timestamp += 500;
158-
t.not(await cache.match("http://localhost/test"), undefined);
159-
clock.timestamp += 500;
160-
t.is(await cache.match("http://localhost/test"), undefined);
161-
});
162-
test("Cache: put respects cf cacheTtlByStatus", async (t) => {
163-
const { clock, cache } = t.context;
164-
const cf: RequestInitCfProperties = {
165-
cacheTtlByStatus: { "200-299": 2, "? :D": 99, "404": 1, "500-599": 0 },
166-
};
167-
const headers = { "Cache-Control": "max-age=5" };
168-
const req200 = new Request("http://localhost/200", { cf });
169-
const req201 = new Request("http://localhost/201", { cf });
170-
const req302 = new Request("http://localhost/302", { cf });
171-
const req404 = new Request("http://localhost/404", { cf });
172-
const req599 = new Request("http://localhost/599", { cf });
173-
await cache.put(req200, new BaseResponse(null, { status: 200, headers }));
174-
await cache.put(req201, new BaseResponse(null, { status: 201, headers }));
175-
await cache.put(req302, new BaseResponse(null, { status: 302, headers }));
176-
await cache.put(req404, new BaseResponse(null, { status: 404, headers }));
177-
await cache.put(req599, new BaseResponse(null, { status: 599, headers }));
178-
179-
// Check all but 5xx responses cached
180-
t.not(await cache.match("http://localhost/200"), undefined);
181-
t.not(await cache.match("http://localhost/201"), undefined);
182-
t.not(await cache.match("http://localhost/302"), undefined);
183-
t.not(await cache.match("http://localhost/404"), undefined);
184-
t.is(await cache.match("http://localhost/599"), undefined);
185-
186-
// Check 404 response expires after 1 second
187-
clock.timestamp += 1000;
188-
t.not(await cache.match("http://localhost/200"), undefined);
189-
t.not(await cache.match("http://localhost/201"), undefined);
190-
t.not(await cache.match("http://localhost/302"), undefined);
191-
t.is(await cache.match("http://localhost/404"), undefined);
192-
193-
// Check 2xx responses expire after 2 seconds
194-
clock.timestamp += 1000;
195-
t.is(await cache.match("http://localhost/200"), undefined);
196-
t.is(await cache.match("http://localhost/201"), undefined);
197-
t.not(await cache.match("http://localhost/302"), undefined);
198-
199-
// Check 302 response expires after 5 seconds
200-
clock.timestamp += 3000;
201-
t.is(await cache.match("http://localhost/302"), undefined);
202-
});
203150

204151
test("Cache: put increments subrequest count", async (t) => {
205152
const { cache } = t.context;

0 commit comments

Comments
 (0)