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}`);
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue