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-Jak316mo 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
TayOP16mo 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
TayOP16mo 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-Jak316mo 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
TayOP16mo ago
Ah my bad I meant workers. For workers would i access it via the global variable?
Cyb3r-Jak3
Cyb3r-Jak316mo ago
I think you could make const cache = cache.default as a global variable or you can have it for each request.
Tay
TayOP16mo ago
Got it. Is the doc I linked outdated then?
Cyb3r-Jak3
Cyb3r-Jak316mo 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.
Want results from more Discord servers?
Add your server