Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix page publish cache purge #331

Merged
merged 3 commits into from
Feb 24, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 12 additions & 1 deletion cms-cache-purger/src/cms_webhook_types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,16 @@ export type WebhookPayload = {
api: string
id: string
type: string
contents: any
contents: {
old: Content | null
new: Content
}
}

export type Content = {
id: string
status: ('PUBLISH' | 'DRAFT' | 'CLOSED')[]
draftKey: string | null
publishValue: any
draftValue: any
}
34 changes: 26 additions & 8 deletions cms-cache-purger/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { type Request as WorkerRequest, type ExecutionContext, type KVNamespace } from '@cloudflare/workers-types'
import crypto from 'node:crypto'
import { WebhookPayload } from './cms_webhook_types'
import { Content, WebhookPayload } from './cms_webhook_types'
import { generateContentKey, generateInfoKey, generateTagsKey } from 'cms-cache-key-gen'

export interface Env {
Expand Down Expand Up @@ -41,14 +41,25 @@ export default {
if (bodyJSON.type === 'new') {
// ブログのメインコンテンツに新規作成があった場合
// contentsのキャッシュを削除する
console.log('start deleteContentsCache')
await deleteContentsCache(env)
if (bodyJSON.contents.new.status.includes('PUBLISH')) {
console.log('start deleteContentsCache')
await deleteContentsCache(env)
} else {
console.log('status is not PUBLISH')
}
} else if (bodyJSON.type === 'edit') {
// ブログのメインコンテンツに編集があった場合
// 対象のIDのコンテンツのキャッシュを削除する
console.log('start deleteContentCache')
console.log('id: ' + bodyJSON.id)
await deleteContentCache(env, bodyJSON.id)
if (isDraftToPublish(bodyJSON.contents.old, bodyJSON.contents.new)) {
// 下書きから公開に変更された場合
// contentsのキャッシュを削除する
console.log('start deleteContentsCache')
await deleteContentsCache(env)
} else {
// ブログのメインコンテンツに編集があった場合
// 対象のIDのコンテンツのキャッシュを削除する
console.log('start deleteContentCache')
console.log('id: ' + bodyJSON.id)
await deleteContentCache(env, bodyJSON.id)
}
} else if (bodyJSON.type === 'delete') {
// ブログのメインコンテンツに削除があった場合
// contentsのキャッシュと対象のIDのコンテンツのキャッシュを削除する
Expand Down Expand Up @@ -104,3 +115,10 @@ async function deleteCache(env: Env, cacheKey: string) {
}
await env.CMS_CACHE.delete(cacheKey)
}

function isDraftToPublish(old: Content | null, newContent: Content): boolean {
if (!old) {
return false
}
return old.status.includes('DRAFT') && newContent.status.includes('PUBLISH')
}