Properly purge CDN cache.
This commit is contained in:
parent
378614e30f
commit
7ef2fd83a1
2 changed files with 11 additions and 13 deletions
|
@ -90,21 +90,21 @@ export async function uploadFile(filepath: string, contents: Uint8Array) {
|
|||
}
|
||||
}
|
||||
|
||||
export async function purgePath(filepath: string) {
|
||||
const cdnBaseUrlEnvName = 'BUNNY_CDN_BASE_URL';
|
||||
const cdnBaseUrl = Deno.env.get(cdnBaseUrlEnvName);
|
||||
export async function purgeCDNCache() {
|
||||
const pullZoneIdEnvName = 'BUNNY_PULL_ZONE_ID';
|
||||
const pullZoneId = Deno.env.get(pullZoneIdEnvName);
|
||||
const accessKeyEnvName = 'BUNNY_ACCESS_KEY';
|
||||
const accessKey = Deno.env.get(accessKeyEnvName);
|
||||
|
||||
if (cdnBaseUrl === undefined) {
|
||||
if (pullZoneId === undefined) {
|
||||
throw new Error(
|
||||
`Can't purge cache for '${filepath}' because we don't know the CDN base URL. Please set it by setting the environment variable '${cdnBaseUrlEnvName}'.`
|
||||
`Can't purge CDN cache for because we don't know the pull zone ID. Please set it by setting the environment variable '${pullZoneIdEnvName}'.`
|
||||
);
|
||||
}
|
||||
|
||||
if (accessKey === undefined) {
|
||||
throw new Error(
|
||||
`Can't purge cache for '${filepath}' because we don't have an API key. Please set it by setting the environment variable '${accessKeyEnvName}'.`
|
||||
`Can't purge CDN cache because we don't have an API key. Please set it by setting the environment variable '${accessKeyEnvName}'.`
|
||||
);
|
||||
}
|
||||
|
||||
|
@ -114,9 +114,7 @@ export async function purgePath(filepath: string) {
|
|||
accesskey: accessKey,
|
||||
};
|
||||
|
||||
const fetchUrl = new URL(`https://api.bunny.net/purge`);
|
||||
fetchUrl.searchParams.append('async', 'false');
|
||||
fetchUrl.searchParams.append('url', `${cdnBaseUrl}/${filepath}`);
|
||||
const fetchUrl = `https://api.bunny.net/pullzone/${pullZoneId}/purgeCache`;
|
||||
|
||||
const res = await fetch(fetchUrl.toString(), {
|
||||
method: 'POST',
|
||||
|
@ -125,6 +123,6 @@ export async function purgePath(filepath: string) {
|
|||
|
||||
if (!res.ok) {
|
||||
console.error(await res.text());
|
||||
throw new Error(`Failed to purge cache: ${res.statusText}`);
|
||||
throw new Error(`Failed to purge CDN cache: ${res.statusText}`);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
import { join } from '@std/path';
|
||||
import { deployScript, purgePath, uploadFile } from './bunny_api/main.ts';
|
||||
import { deployScript, purgeCDNCache, uploadFile } from './bunny_api/main.ts';
|
||||
|
||||
if (import.meta.main) {
|
||||
console.log(`Attempting to upload index.html`);
|
||||
|
@ -8,8 +8,8 @@ if (import.meta.main) {
|
|||
await uploadFile('index.html', indexContents);
|
||||
console.log(`Done!`);
|
||||
|
||||
console.log(`Attempting to purge the cache for index.html`);
|
||||
await purgePath('index.html');
|
||||
console.log(`Attempting to purge the CDN cache.`);
|
||||
await purgeCDNCache();
|
||||
console.log(`Done!`);
|
||||
|
||||
for (const dirEntry of Deno.readDirSync(
|
||||
|
|
Loading…
Add table
Reference in a new issue