Skip to content

Commit

Permalink
fix(plugin-cloud): purge cache for all sizes
Browse files Browse the repository at this point in the history
  • Loading branch information
denolfe committed Mar 12, 2024
1 parent c038acb commit 73e745e
Showing 1 changed file with 22 additions and 3 deletions.
25 changes: 22 additions & 3 deletions packages/plugin-cloud/src/hooks/uploadCache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,13 @@ interface Args {
endpoint: string
}

type GenericUpload = {
url?: string
sizes?: Record<string, { url?: string }>
}

export const getCacheUploadsAfterChangeHook =
({ endpoint }: Args): CollectionAfterChangeHook =>
({ endpoint }: Args): CollectionAfterChangeHook<GenericUpload> =>
async ({ doc, operation, req }) => {
if (!req || !process.env.PAYLOAD_CLOUD_CACHE_KEY) return doc

Expand Down Expand Up @@ -37,12 +42,17 @@ export const getCacheUploadsAfterDeleteHook =
}

type PurgeRequest = {
doc: any
doc: GenericUpload
endpoint: string
operation: string
req: PayloadRequest
}

type GenericUpload = {
url?: string
sizes?: Record<string, { url: string }>
}

async function purge({ doc, endpoint, operation, req }: PurgeRequest) {
const filePath = doc.url

Expand All @@ -56,11 +66,20 @@ async function purge({ doc, endpoint, operation, req }: PurgeRequest) {
return
}

const filepaths = [filePath]
if (Object.keys(doc.sizes).length) {
const urls = Object.values(doc.sizes)
.map((size) => size?.url)
.filter(Boolean)
filepaths.push(...urls)
}

const body = {
cacheKey: process.env.PAYLOAD_CLOUD_CACHE_KEY,
filepath: doc.url,
filepaths,
projectID: process.env.PAYLOAD_CLOUD_PROJECT_ID,
}

req.payload.logger.debug({
filepath: doc.url,
msg: 'Attempting to purge cache',
Expand Down

0 comments on commit 73e745e

Please sign in to comment.