-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add a plugin to bust cache on deploy
- Loading branch information
Showing
5 changed files
with
44 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
|
||
[[context.production.plugins]] | ||
package = "/plugins/cache-bust" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
# cache bust plugin | ||
|
||
name: cache-bust |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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}' |