Skip to content

Commit 4c11cf0

Browse files
committed
Adds bunny cache purging to setup
1 parent e0903b9 commit 4c11cf0

File tree

2 files changed

+47
-0
lines changed

2 files changed

+47
-0
lines changed

Diff for: app/setup.js

+43
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,49 @@ function main(callback) {
126126
configureLocalBlogs();
127127
callback();
128128
},
129+
130+
// This is neccessary because when we deploy new dashboard or site code
131+
// the old container will continue to serve the old code until they are
132+
// replaced. So once the new code is deployed, we need to purge the CDN
133+
// at the end of each setup.
134+
async function () {
135+
if (!config.bunny.secret) return;
136+
137+
if (config.environment !== "production") return;
138+
139+
try {
140+
const cdnURL = require("documentation/tools/cdn-url-helper")({
141+
cacheID: new Date().getTime(),
142+
viewDirectory: config.views_directory,
143+
})();
144+
145+
const urls = [
146+
"/dashboard.min.css",
147+
"/dashboard.min.js",
148+
"/documentation.min.css",
149+
"/documentation.min.js",
150+
]
151+
.map((path) => cdnURL(path, (p) => p))
152+
.map((p) => encodeURIComponent(p));
153+
154+
for (const urlToPurge of urls) {
155+
const url = `https://api.bunny.net/purge?url=${urlToPurge}&async=false`;
156+
const options = {
157+
method: "POST",
158+
headers: { AccessKey: config.bunny.secret },
159+
};
160+
console.log("Purging Bunny CDN cache", url);
161+
const res = await fetch(url, options);
162+
if (res.status !== 200) {
163+
console.error("Failed to purge Bunny CDN cache", res.status);
164+
} else {
165+
console.log("Purged Bunny CDN cache", res.status);
166+
}
167+
}
168+
} catch (e) {
169+
console.error("Failed to run function to purge Bunny CDN cache", e);
170+
}
171+
},
129172
],
130173
callback
131174
);

Diff for: config/index.js

+4
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,10 @@ module.exports = {
140140
origin: BLOT_CDN,
141141
},
142142

143+
bunny: {
144+
secret: process.env.BUNNY_API_SECRET,
145+
},
146+
143147
session: {
144148
secret: process.env.BLOT_SESSION_SECRET,
145149
},

0 commit comments

Comments
 (0)