Skip to content

Commit b228fb5

Browse files
authored
chore(cloudflare): more restrictive cache conditions (#4669)
* chore(cloudflare): more restrictive cache conditions * changeset
1 parent fbb768b commit b228fb5

File tree

2 files changed

+11
-3
lines changed

2 files changed

+11
-3
lines changed

.changeset/honest-rice-cry.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@sveltejs/adapter-cloudflare": patch
3+
---
4+
5+
chore(cloudflare): more restrictive cache lookup & save conditions

packages/adapter-cloudflare/src/worker.js

+6-3
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,9 @@ const prefix = `/${manifest.appDir}/`;
1010
const worker = {
1111
async fetch(req, env, context) {
1212
try {
13-
let res = await Cache.lookup(req);
13+
// skip cache if "cache-control: no-cache" in request
14+
let pragma = req.headers.get('cache-control') || '';
15+
let res = !pragma.includes('no-cache') && (await Cache.lookup(req));
1416
if (res) return res;
1517

1618
let { pathname } = new URL(req.url);
@@ -57,8 +59,9 @@ const worker = {
5759
}
5860
}
5961

60-
// Writes to Cache only if allowed
61-
return Cache.save(req, res, context);
62+
// Writes to Cache only if allowed & specified
63+
pragma = res.headers.get('cache-control');
64+
return pragma ? Cache.save(req, res, context) : res;
6265
} catch (e) {
6366
return new Response('Error rendering route: ' + (e.message || e.toString()), { status: 500 });
6467
}

0 commit comments

Comments
 (0)