Skip to content

Commit 3bff0cf

Browse files
committed
use waitUntil for non-blocking cache
1 parent 19deed2 commit 3bff0cf

File tree

2 files changed

+4
-3
lines changed

2 files changed

+4
-3
lines changed

src/cache.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -73,8 +73,6 @@ export class ResponseCache {
7373
response: Response;
7474
ttl: number | null;
7575
}): Promise<void> => {
76-
// TODO: might be able to do ctx.waitUntil:
77-
// https://developers.cloudflare.com/workers/examples/cache-api
7876
const body = await response.clone().text();
7977
const responseObject = {
8078
status: response.status,

src/proxy.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ interface HandleProxyOpts {
1212
export const handleProxy = async ({
1313
request,
1414
env,
15+
ctx,
1516
ttl,
1617
pathname,
1718
}: HandleProxyOpts): Promise<Response> => {
@@ -50,11 +51,13 @@ export const handleProxy = async ({
5051

5152
if (response.ok) {
5253
console.log('Writing 2xx response to cache: ', { cacheKey, ttl });
53-
await responseCache.write({
54+
const writeCachePromise = responseCache.write({
5455
cacheKey,
5556
ttl,
5657
response,
5758
});
59+
// https://developers.cloudflare.com/workers/runtime-apis/fetch-event/#waituntil
60+
ctx.waitUntil(writeCachePromise);
5861
} else {
5962
console.log('Not caching error or empty response.');
6063
}

0 commit comments

Comments
 (0)