Accessing Cache API Typescript

In the cache API documentation (https://developers.cloudflare.com/workers/runtime-apis/cache/#accessing-cache), it says to access the cache via a global variable. However the examples show a third parameter in the fetch method. This parameter doesn't exist in my Typescript definition that wrangler generated. Which is the correct way to access the Cache API? Also if I use the Cache API should I disable edge caching via page rules for the route?
Cache · Cloudflare Workers docs
The Cache API allows fine grained control of reading and writing from the Cloudflare global network cache.
8 Replies
Cyb3r-Jak3
Cyb3r-Jak310mo ago
With functions it should look something like
export async function onRequest({ request, env }) {
const cache = caches.default;
let possible_cache = await cache.match(request)
}
export async function onRequest({ request, env }) {
const cache = caches.default;
let possible_cache = await cache.match(request)
}
Tay
Tay10mo ago
why do these examples have a third parameter? https://developers.cloudflare.com/workers/examples/cache-api/
Using the Cache API · Cloudflare Workers docs
Documentation for Cloudflare Workers, a serverless execution environment that allows you to create entirely new applications or augment existing ones …
Tay
Tay10mo ago
my typescript definition doesn't show a third argument nor an object?
declare interface ExportedHandler<
Env = unknown,
QueueHandlerMessage = unknown,
CfHostMetadata = unknown
> {
fetch?: ExportedHandlerFetchHandler<Env, CfHostMetadata>;
tail?: ExportedHandlerTailHandler<Env>;
trace?: ExportedHandlerTraceHandler<Env>;
scheduled?: ExportedHandlerScheduledHandler<Env>;
test?: ExportedHandlerTestHandler<Env>;
email?: EmailExportedHandler<Env>;
queue?: ExportedHandlerQueueHandler<Env, QueueHandlerMessage>;
}
declare interface ExportedHandler<
Env = unknown,
QueueHandlerMessage = unknown,
CfHostMetadata = unknown
> {
fetch?: ExportedHandlerFetchHandler<Env, CfHostMetadata>;
tail?: ExportedHandlerTailHandler<Env>;
trace?: ExportedHandlerTraceHandler<Env>;
scheduled?: ExportedHandlerScheduledHandler<Env>;
test?: ExportedHandlerTestHandler<Env>;
email?: EmailExportedHandler<Env>;
queue?: ExportedHandlerQueueHandler<Env, QueueHandlerMessage>;
}
Cyb3r-Jak3
Cyb3r-Jak310mo ago
There are more parameters on the context https://developers.cloudflare.com/pages/platform/functions/api-reference/#eventcontext and I just made it simpler. The docs you link are workers and not functions through
Tay
Tay10mo ago
Ah my bad I meant workers. For workers would i access it via the global variable?
Cyb3r-Jak3
Cyb3r-Jak310mo ago
I think you could make const cache = cache.default as a global variable or you can have it for each request.
Tay
Tay10mo ago
Got it. Is the doc I linked outdated then?
Cyb3r-Jak3
Cyb3r-Jak310mo ago
I don't think so. Unfortunately my knowledge of typescript types is very limited but it is a global object not a global variable.