Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a plugin to bust cache on deploy #404

Merged
merged 2 commits into from
Jan 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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}'