Skip to content

Commit

Permalink
chore(cloudflare): more restrictive cache conditions (#4669)
Browse files Browse the repository at this point in the history
* chore(cloudflare): more restrictive cache conditions

* changeset
  • Loading branch information
lukeed authored Apr 20, 2022
1 parent fbb768b commit b228fb5
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 3 deletions.
5 changes: 5 additions & 0 deletions .changeset/honest-rice-cry.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@sveltejs/adapter-cloudflare": patch
---

chore(cloudflare): more restrictive cache lookup & save conditions
9 changes: 6 additions & 3 deletions packages/adapter-cloudflare/src/worker.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@ const prefix = `/${manifest.appDir}/`;
const worker = {
async fetch(req, env, context) {
try {
let res = await Cache.lookup(req);
// skip cache if "cache-control: no-cache" in request
let pragma = req.headers.get('cache-control') || '';
let res = !pragma.includes('no-cache') && (await Cache.lookup(req));
if (res) return res;

let { pathname } = new URL(req.url);
Expand Down Expand Up @@ -57,8 +59,9 @@ const worker = {
}
}

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

0 comments on commit b228fb5

Please sign in to comment.