Skip to content

Commit

Permalink
Merge pull request #404 from MuckRock/385-cache-bust
Browse files Browse the repository at this point in the history
Add a plugin to bust cache on deploy
  • Loading branch information
eyeseast authored Jan 9, 2024
2 parents 0cad429 + 637fa37 commit ad2ccb6
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ public/assets/
.netlify

# Local test env file
.env.secret
.env.test
.env.sentry-build-plugin
playwright-report
Expand Down
3 changes: 3 additions & 0 deletions netlify.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@

[[context.production.plugins]]
package = "/plugins/cache-bust"
30 changes: 30 additions & 0 deletions plugins/cache-bust/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
// bust cache after a production deploy succeeds

const { CLOUDFLARE_ID, CLOUDFLARE_TOKEN } = process.env;
const PURGE_URL = `https://api.cloudflare.com/client/v4
/zones/${CLOUDFLARE_ID}/purge_cache`;

export async function onSuccess() {
if (!CLOUDFLARE_ID) {
console.error("CLOUDFLARE_ID is not set.");
return;
}

const resp = await fetch(PURGE_URL, {
method: "POST",
headers: {
authorization: `Bearer ${CLOUDFLARE_TOKEN}`,
"content-type": "application/json",
},
body: JSON.stringify({ purge_everything: true }),
});

if (!resp.ok) {
console.error("Error purging cache.");
console.error(`${resp.status}: ${resp.statusText}`);
}

const result = await resp.json();

console.log(JSON.stringify(result, null, 2));
}
3 changes: 3 additions & 0 deletions plugins/cache-bust/manifest.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# cache bust plugin

name: cache-bust
7 changes: 7 additions & 0 deletions utility/purge.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#!/usr/bin/env sh

curl --request POST \
--url "https://api.cloudflare.com/client/v4/zones/${CLOUDFLARE_ID}/purge_cache" \
--header 'Content-Type: application/json' \
--header "Authorization: Bearer ${CLOUDFLARE_TOKEN}" \
--data '{"purge_everything": true}'

0 comments on commit ad2ccb6

Please sign in to comment.